From 3b51e46a2ddbedb8cd333fb0b9c9edada03f1e58 Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Fri, 11 Jun 2021 22:23:54 -0400 Subject: Each and Table documentation --- docs/doc/index.html | 1 + docs/doc/map.html | 239 ++++++++++++++++++++++++++++++++++++++++++++++++ docs/doc/primitive.html | 4 +- 3 files changed, 242 insertions(+), 2 deletions(-) create mode 100644 docs/doc/map.html (limited to 'docs/doc') diff --git a/docs/doc/index.html b/docs/doc/index.html index d962f10e..513b2bb7 100644 --- a/docs/doc/index.html +++ b/docs/doc/index.html @@ -45,6 +45,7 @@
  • Join and Join To ()
  • Logical functions (∧∨¬)
  • Match (≡≢)
  • +
  • Mapping (¨⌜)
  • Ordering functions (∧∨⍋⍒)
  • Prefixes and Suffixes (↑↓)
  • Reverse and Rotate ()
  • diff --git a/docs/doc/map.html b/docs/doc/map.html new file mode 100644 index 00000000..ae5491ee --- /dev/null +++ b/docs/doc/map.html @@ -0,0 +1,239 @@ + + + + BQN: Mapping modifiers + + +

    Mapping modifiers

    +

    Mapping a function over an array means to call it on each element of that array, creating an array of results. It's also possible to map over two arrays, applying the function to various choices of one element from each, but there's no longer a single correct way to iterate over these elements.

    +

    BQN has two 1-modifiers to map over arrays: Each (¨) and Table (). On two arguments, Table applies its operand to all combinations of elements while Each creates a one-to-one or one-to-many matching. Since they apply to elements, these modifiers are different from Cells (˘) or its generalization Rank (), which apply the function to array cells. The modifier Depth () is a generalization of Each, so that ¨ is ¯1; however, it can't be used to implement Table without some additional array operations.

    +

    One-argument mapping

    + + + + Each/Table + + + + + + + + + x0 + x1 + x2 + x3 + x4 + 𝔽x0 + 𝔽x1 + 𝔽x2 + 𝔽x3 + 𝔽x4 + + + 𝕩 + 𝔽¨𝕩 + + + + + + + + + + +

    On one argument, Each and Table are identical. They apply the function 𝔽 to every element of 𝕩, and return an array with the same shape that contains each result.

    +↗️
         342
    +⟨ ⟨ 0 1 2 ⟩ ⟨ 0 1 2 3 ⟩ ⟨ 0 1 ⟩ ⟩
    +
    +    ¨ 22342
    +┌─                       
    +╵ ⟨ 0 1 2 ⟩ ⟨ 0 1 2 3 ⟩  
    +  ⟨ 0 1 ⟩   ⟨ 0 1 2 ⟩    
    +                        ┘
    +
    +

    A nice way to examine what's being applied here is to make an argument where each element is a string describing itself, and an operand that describes its own application: "𝔽" will place an 𝔽 in front of the argument, which is how functions are applied.

    +↗️
        "𝔽"¨ "0⊑𝕩""1⊑𝕩""2⊑𝕩"
    +⟨ "𝔽0⊑𝕩" "𝔽1⊑𝕩" "𝔽2⊑𝕩" ⟩
    +
    +    {('0'+𝕩)"⊑𝕩"} 3  # Making 𝕩 with mapping instead
    +⟨ "0⊑𝕩" "1⊑𝕩" "2⊑𝕩" ⟩
    +
    +

    The applications are performed in index order: index 00, then 01, 02 and so on, until 10. This can affect a program where the operand has side effects, such as the following one that appends its argument to o.

    +↗️
        o⟨⟩  {o<𝕩}¨ "index""order"  o
    +"indexorder"
    +
    +

    When an array is displayed, index order is the same as the top-to-bottom, left-to-right reading order of English. It's also the same as the ordering of Deshape's result, so that here o ends up being 𝕩. The dyadic cases described in the following sections will also have a defined evaluation order, but it's not easy to describe it in terms of the arguments: instead, the result elements are produced in index order.

    +

    Table

    + + + + Table + + + + + + + + + + + + + + + x0 + x1 + x2 + x3 + x4 + w0 + w0𝔽x0 + w0𝔽x1 + w0𝔽x2 + w0𝔽x3 + w0𝔽x4 + w1 + w1𝔽x0 + w1𝔽x1 + w1𝔽x2 + w1𝔽x3 + w1𝔽x4 + w2 + w2𝔽x0 + w2𝔽x1 + w2𝔽x2 + w2𝔽x3 + w2𝔽x4 + + + 𝕨 + 𝕩 + + 𝔽 + + + + + + + + + + + +

    The Table modifier applies its operand function to every possible combination of one element from 𝕨 and one from 𝕩, sort of like a structure-preserving and function-applying Cartesian product. Below, it combines a length-3 list and a length-5 list into a shape 35 table.

    +↗️
        "ABC"  "01234"
    +┌─                          
    +╵ "A0" "A1" "A2" "A3" "A4"  
    +  "B0" "B1" "B2" "B3" "B4"  
    +  "C0" "C1" "C2" "C3" "C4"  
    +                           ┘
    +
    +

    Its name comes from the "multiplication table" or "times table" often used to teach arithmetic, and with it you can easily make such a table, by repeating the same argument with Self (˜):

    +↗️
        ×⌜˜ 1+↕6
    +┌─                  
    +╵ 1  2  3  4  5  6  
    +  2  4  6  8 10 12  
    +  3  6  9 12 15 18  
    +  4  8 12 16 20 24  
    +  5 10 15 20 25 30  
    +  6 12 18 24 30 36  
    +                   ┘
    +
    +

    The arguments don't have to be lists (that is, rank 1). There's no restriction on their shapes at all! Much like the result shape is mn if 𝕨 is a list of length m and 𝕩 is a list of length n, the result shape for an array 𝕨 of shape r and 𝕩 of shape s is rs.

    +↗️
        "A ""B "  "the""first""row""and""the""second"
    +┌─                              
    +╎ "A the" "A first" "A row"     
    +  "A and" "A the"   "A second"  
    +                                
    +  "B the" "B first" "B row"     
    +  "B and" "B the"   "B second"  
    +                               ┘
    +
    +     "A ""B "  "the""first""row""and""the""second"
    +⟨ 2 2 3 ⟩
    +
    +

    Except for the more sophisticated shape, this result is exactly what you'd get if you deshaped each argument to a list. In each case, every element of 𝕨 is visited in turn, and each time the element is paired with every element of 𝕩.

    +

    Each

    + + + + + + + + + + Each + + + + + + + + + w0 + w1 + w2 + w3 + w4 + x0 + x1 + x2 + x3 + x4 + w0𝔽x0 + w1𝔽x1 + w2𝔽x2 + w3𝔽x3 + w4𝔽x4 + + + 𝕨 + 𝕩 + 𝕨𝔽¨𝕩 + + + + + + + + + + + + +

    Given two arguments of matching shapes, Each performs what's sometimes called a "zip", matching each element of 𝕨 to the corresponding element of 𝕩.

    +↗️
        "ABCD" ¨ "0123"
    +⟨ "A0" "B1" "C2" "D3" ⟩
    +
    +

    This makes for a lot fewer applications than Table. Only the diagonal elements from Table's result are seen, as we can check with Transpose.

    +↗️
        00  "ABCD"  "0123"
    +⟨ "A0" "B1" "C2" "D3" ⟩
    +
    +

    If the argument lengths don't match then Each gives an error. This contrasts with zip in many languages, which drops elements from the longer argument. This is rarely wanted in BQN, and having an error right away saves debugging time.

    +↗️
        "ABC" ¨ "01234"
    +ERROR
    +
    +

    Arguments can have any shape as long as the axis lengths match up. As with Table, the result elements don't depend on this shape but the result shape does.

    +↗️
        (>203010,504060) +¨ 210321
    +┌─                               
    +╵ ⟨ 20 21 ⟩    ⟨ 30 ⟩    ⟨⟩      
    +  ⟨ 50 51 52 ⟩ ⟨ 40 41 ⟩ ⟨ 60 ⟩  
    +                                ┘
    +
    +

    But arguments don't have to have exactly the same shape: just the same length along corresponding axes. These axes are matched up according to the leading axis convention, so that one argument's shape has to be a prefix of the other's. With equal ranks, the shapes do have to match as we've seen above.

    +↗️
         (026@) ¨ 010  # Too small
    +ERROR
    +     (026@) ¨ 020  # Just right
    +⟨ 0 2 6 ⟩
    +     (026@) ¨ 030  # Too large
    +ERROR
    +
    +

    Leading axis agreement is described further here.

    diff --git a/docs/doc/primitive.html b/docs/doc/primitive.html index c0afd5a9..5bd76b3c 100644 --- a/docs/doc/primitive.html +++ b/docs/doc/primitive.html @@ -494,13 +494,13 @@ ¨ -Each +Each Depth -Table +Table -- cgit v1.2.3