diff options
| author | Marshall Lochbaum <mwlochbaum@gmail.com> | 2022-04-15 12:27:10 -0400 |
|---|---|---|
| committer | Marshall Lochbaum <mwlochbaum@gmail.com> | 2022-04-15 12:27:10 -0400 |
| commit | e9eec52cc74713cfde1314319c2fc6c3b19144cf (patch) | |
| tree | e6cc20e348c767cba412dc9356ff3ecbf98f1e78 /doc | |
| parent | ef84cc428ec0f22aa2fcdc21c8669843c27c7727 (diff) | |
Documentation for Choose
Diffstat (limited to 'doc')
| -rw-r--r-- | doc/README.md | 1 | ||||
| -rw-r--r-- | doc/choose.md | 27 | ||||
| -rw-r--r-- | doc/primitive.md | 2 |
3 files changed, 29 insertions, 1 deletions
diff --git a/doc/README.md b/doc/README.md index 8acd5740..98c61d51 100644 --- a/doc/README.md +++ b/doc/README.md @@ -42,6 +42,7 @@ Primitives: - [Array dimensions](shape.md) (`≢=≠`) - [Assert and Catch](assert.md) (`!` and `⎊`) - [Atop and Over](compose.md) (`∘○`) +- [Choose](choose.md) (`◶`) - [Constant](constant.md) (`˙`) - [Deshape and Reshape](reshape.md) (`⥊`) - [Enclose](enclose.md) (`<`) diff --git a/doc/choose.md b/doc/choose.md new file mode 100644 index 00000000..4362b9f5 --- /dev/null +++ b/doc/choose.md @@ -0,0 +1,27 @@ +*View this file with results and syntax highlighting [here](https://mlochbaum.github.io/BQN/doc/choose.html).* + +# Choose + +The 2-modifier Choose (`◶`) applies one function from a list `𝕘`, based on a selection function `𝔽` that returns an index. It's a combinator form of [Pick](pick.md) (`⊑`), so that `{f←(𝕨𝔽𝕩)⊑𝕘 ⋄ 𝕨F𝕩}` is a complete definition. For example, the function below subtracts 1 from an argument if negative and adds 1 if positive. + + 0⊸≤◶⟨-⟜1, +⟜1⟩¨ 3‿¯1‿5 + +Here the selection function `𝔽` is `0⊸≤`, while `𝕘` is a list of two functions `⟨-⟜1, +⟜1⟩`. On the first argument, `3`, `𝔽3` is `0≤3`, or `1`, so the function `+⟜1` from `𝕘` is chosen. The use of array indices means "false" comes first in `𝕘` and "true" comes second, which is backwards relative to if-else constructs in most programming languages (including BQN's own predicates). When using a comparison for `𝔽` I strongly prefer to phrase it as `n⊸<` or `n⊸≤` so that smaller values go through the first one and larger functions go through the second. This doesn't apply so much when comparing two arguments since one is smaller but the other's larger, so I don't have an easy technique for that. + + 2 >◶⊣‿⊢ 6 # A minimum function (⌊) + +The advantage of using an index is that Choose works with any number of options. + + Fn ← (⊑"rtd"⊒⊏)◶⟨⌽, 1⊸↑, 1⊸↓, ⊢⟩ # Reverse, take 1, drop 1 + + Fn "r123" + + Fn "d123" + + Fn "123" # Default + +The selection function in `Fn` uses [Index of](search.md#index-of) (`⊒`) to find the index of the first character in the list `"rtd"`. An extra value in `𝕘` serves as a default function if it's none of those, since the result of `𝔽` is `3` in that case. A similar function that's often useful is [Bins](order.md#bins), for grouping inputs into intervals rather than by exact matching. + +Choose is necessary for [tacit](tacit.md) programming, but tacit programming is not necessary to be an effective BQN programmer! Consider using block features like [predicates](block.md#predicates) when Choose isn't working with your program's flow. + +Because Choose is based on [Pick](pick.md), it retains the features of negative, multidimensional, and multiple selection. Negative indexing might make sense if there's some special `¯1` value, and if the options naturally form an array, multidimensional indexing is pretty neat. Selecting multiple values from `𝕘`, which happens if the result of `𝔽` is an array of arrays, is never useful because the array result from `𝔽` acts as a constant function. It's much clearer to express it as `𝔽⊑𝕘˙`. diff --git a/doc/primitive.md b/doc/primitive.md index 318d360f..79d6120b 100644 --- a/doc/primitive.md +++ b/doc/primitive.md @@ -75,7 +75,7 @@ Glyph | Name(s) | Definition | Description `⟜` | After/Bind | `{(𝕨⊣𝕩)𝔽𝔾𝕩}` | `𝔽`'s right argument comes from `𝔾` `⌾` | Under | `{𝔾⁼∘𝔽○𝔾}` OR `{(𝔾𝕩)↩𝕨𝔽○𝔾𝕩⋄𝕩}` | Apply `𝔽` over `𝔾`, then undo `𝔾` `⊘` | [Valences](valences.md) | `{𝔽𝕩;𝕨𝔾𝕩}` | Apply `𝔽` if there's one argument but `𝔾` if there are two -`◶` | Choose | `{f←(𝕨𝔽𝕩)⊑𝕘 ⋄ 𝕨F𝕩}` | Select one of the functions in list `𝕘` based on `𝔽` +`◶` | [Choose](choose.md) | `{f←(𝕨𝔽𝕩)⊑𝕘 ⋄ 𝕨F𝕩}` | Select one of the functions in list `𝕘` based on `𝔽` Choose isn't really a combinator since it calls the function `⊑`, and Under is not a true combinator since it has an "undo" step at the end. This step might be implemented using the left operand's inverse (*computational* Under) or its structural properties (*structural* Under). |
