From e9eec52cc74713cfde1314319c2fc6c3b19144cf Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Fri, 15 Apr 2022 12:27:10 -0400 Subject: Documentation for Choose --- doc/README.md | 1 + doc/choose.md | 27 +++++++++++++++++++++++++++ doc/primitive.md | 2 +- docs/doc/choose.html | 30 ++++++++++++++++++++++++++++++ docs/doc/index.html | 1 + docs/doc/primitive.html | 2 +- docs/help/choose.html | 1 + help/choose.md | 1 + md.bqn | 2 +- 9 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 doc/choose.md create mode 100644 docs/doc/choose.html 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). diff --git a/docs/doc/choose.html b/docs/doc/choose.html new file mode 100644 index 00000000..2bcaa223 --- /dev/null +++ b/docs/doc/choose.html @@ -0,0 +1,30 @@ + + + + BQN: Choose + + +

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 (), 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¯15
+⟨ 4 ¯2 6 ⟩
+
+

Here the selection function 𝔽 is 0, while 𝕘 is a list of two functions -1, +1. On the first argument, 3, 𝔽3 is 03, 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 (⌊)
+2
+
+

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"
+"321r"
+
+    Fn "d123"
+"123"
+
+    Fn "123"  # Default
+"123"
+
+

The selection function in Fn uses 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, for grouping inputs into intervals rather than by exact matching.

+

Choose is necessary for tacit programming, but tacit programming is not necessary to be an effective BQN programmer! Consider using block features like predicates when Choose isn't working with your program's flow.

+

Because Choose is based on Pick, 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/docs/doc/index.html b/docs/doc/index.html index c3d6ea2b..66bdbfec 100644 --- a/docs/doc/index.html +++ b/docs/doc/index.html @@ -48,6 +48,7 @@
  • Array dimensions (≢=≠)
  • Assert and Catch (! and )
  • Atop and Over (∘○)
  • +
  • Choose ()
  • Constant (˙)
  • Deshape and Reshape ()
  • Enclose (<)
  • diff --git a/docs/doc/primitive.html b/docs/doc/primitive.html index db9140b6..aa3fd087 100644 --- a/docs/doc/primitive.html +++ b/docs/doc/primitive.html @@ -473,7 +473,7 @@ -Choose +Choose {f(𝕨𝔽𝕩)𝕘 𝕨F𝕩} Select one of the functions in list 𝕘 based on 𝔽 diff --git a/docs/help/choose.html b/docs/help/choose.html index f56496f0..5c0562a3 100644 --- a/docs/help/choose.html +++ b/docs/help/choose.html @@ -6,6 +6,7 @@

    Circle with Lower Right Quadrant ()

    𝔽𝕘 𝕩, 𝕨 𝔽𝕘 𝕩: Choose

    +

    →full documentation

    Apply 𝔽 to the arguments and use the result to pick () a function from list 𝕘. Apply the picked function to the arguments.

    ↗️
        F  +-÷×
     
    diff --git a/help/choose.md b/help/choose.md
    index 02ebfc23..b0ca6001 100644
    --- a/help/choose.md
    +++ b/help/choose.md
    @@ -3,6 +3,7 @@
     # Circle with Lower Right Quadrant (`◶`)
     
     ## `𝔽◶𝕘 𝕩`, `𝕨 𝔽◶𝕘 𝕩`: Choose
    +[→full documentation](../doc/choose.md)
     
     Apply `𝔽` to the arguments and use the result to [pick](first_pick.md#𝕨--𝕩-pick) (`⊑`) a function from list `𝕘`. Apply the picked function to the arguments.
     
    diff --git a/md.bqn b/md.bqn
    index d252c192..646dc1f3 100644
    --- a/md.bqn
    +++ b/md.bqn
    @@ -216,7 +216,7 @@ Markdown ← {filename𝕊𝕩:
         # Don't show assignment results by default
         ShowRslt ← {
           depth ← +` "(){}⟨⟩" (⊣(≠⊸>ׯ1⋆2|⊢)⊐) 𝕩
    -      𝕩 /˜↩ ¬ ∨`⌾⌽ (0=depth) ∧ 𝕩∊"⋄,"  # Just the last statement
    +      𝕩 /˜↩ ¬ ∨`⌾⌽ (0=depth) ∧ (∧`𝕩≠'#') ∧ 𝕩∊"⋄,"  # Just the last statement
           g ← 𝕩∊"←↩"
           (⊑g⊐1) (<⟜(≠g))◶⟨1,¬(" "∾∾idChars)∧´∘∊˜↑⟩ 𝕩
         }
    -- 
    cgit v1.2.3