diff options
| -rw-r--r-- | doc/README.md | 1 | ||||
| -rw-r--r-- | doc/map.md | 165 | ||||
| -rw-r--r-- | doc/primitive.md | 4 | ||||
| -rw-r--r-- | docs/doc/index.html | 1 | ||||
| -rw-r--r-- | docs/doc/map.html | 239 | ||||
| -rw-r--r-- | docs/doc/primitive.html | 4 |
6 files changed, 410 insertions, 4 deletions
diff --git a/doc/README.md b/doc/README.md index f1311104..7b3217f1 100644 --- a/doc/README.md +++ b/doc/README.md @@ -39,6 +39,7 @@ Primitives: - [Join and Join To](join.md) (`∾`) - [Logical functions](logic.md) (`∧∨¬`) - [Match](match.md) (`≡≢`) +- [Mapping](map.md) (`¨⌜`) - [Ordering functions](order.md) (`∧∨⍋⍒`) - [Prefixes and Suffixes](prefixes.md) (`↑↓`) - [Reverse and Rotate](reverse.md) (`⌽`) diff --git a/doc/map.md b/doc/map.md new file mode 100644 index 00000000..24fdc21a --- /dev/null +++ b/doc/map.md @@ -0,0 +1,165 @@ +*View this file with results and syntax highlighting [here](https://mlochbaum.github.io/BQN/doc/map.html).* + +# 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](depth.md#the-depth-modifier) (`⚇`) 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 + +<!--GEN +xt ← ("x"∾'0'⊸+)¨ ↕5 +d ← 80‿72 +Pos ↩ Pos d⊸× + +lcol ← "#521f5e"‿"#7f651c" + +rc ← At "class=code|stroke-width=1.5|rx=12" +Ge ← "g"⊸At⊸Enc +g ← "fill=currentColor|font-family=BQN,monospace" +dg ← "font-size=24px|fill=currentColor|opacity=0.9" +tg ← "font-size=18px|text-anchor=middle" +cg ← "font-size=22px|text-anchor=end|dy=0.2" +bg ← "class=bluegreen|stroke-width=3|stroke-linecap=round|style=fill:none|opacity=0.7" + +lg ← At"stroke-width=8|opacity=0.1" +Gl ← {("g"Attr"stroke"‿𝕨∾lg)Enc𝕩} + +bo ← -⌾⊑⊸≍÷12‿5 +Gb ← { + bb ← ⟨(÷2)-˜⊑𝕨,≠𝕨⟩+0.1×⟨1,¯2⟩ + 𝕩 {∾"M l l "∾¨FmtNum⥊d⊸ע⟨+⟜(𝕩⊸×)´bb,𝕨-0.28⟩∾𝕩⌽bo}⌜ ↕2 +} + +Text ← ("text" Attr Pos)⊸Enc +Line ← "line" Elt ("xy"≍⌜"12")≍˘○⥊ ·FmtNum d×⊢ +Path ← "path" Elt "d"≍○<⊢ +{ + dim ← 7.5‿2.2 ⋄ sh ← ¯2.3‿¯0.1 + tx ← ↕≠xt ⋄ ty ← 0.7+↕2 + + ((∾˜d)×((-∾+˜)0.5‿0.2)+sh∾dim) SVG g Ge ⟨ + "rect" Elt rc ∾ (Pos 1‿0×sh)∾"width"‿"height"≍˘FmtNum d×dim + dg Ge ¯2.1‿0.1 Text "Each/Table" + (1⊑lcol) Gl (Line ≍˜≍(0.2(⊣≍-˜)⊢´dim)˙)¨ tx + tg Ge (⍉tx≍⌜ty) Text¨ (⊢≍(Highlight"𝔽")⊸∾¨) xt + cg Ge (¯1.1≍¨ty) Text⟜Highlight¨ "𝕩"‿"𝔽¨𝕩" + bg Ge Path¨ tx Gb ty + ⟩ +} +--> + +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. + + ↕⌜ 3‿4‿2 + + ↕¨ 2‿2⥊3‿4‿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'+𝕩)∾"⊑𝕩"}⌜ ↕3 # Making 𝕩 with mapping instead + +The applications are performed in index order: index `…0‿0`, then `…0‿1`, `…0‿2` and so on, until `…1‿0`. 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 + +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](reshape.md#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 + +<!--GEN +{ + wt ← ("w"∾'0'⊸+)¨ ↕3 + dim ← 6.7‿4.4 ⋄ sh ← ¯1.58‿¯0.1 + tx‿ty ← (p0←¯0.9‿0.8) + (↕1+≠)¨xt‿wt + cg ← "font-size=19px|text-anchor=middle" + rb ← { + o←(1.5⋆¬𝕩)×(18÷d)×𝕩⌽¯1‿1 + ∾"M hv"∾¨FmtNum⥊d⊸עo(-˜⌾⊑≍⊣)(0≍𝕩⊑÷15‿¯6)+0.5+(-𝕩)⊑¨⟨0.15+tx,ty⟩ + }⌜ ↕2 + tx +↩0.15×0<tx + wb ← (1↓ty) { + bb ← ⟨(÷2)-˜⊑𝕨,≠𝕨⟩+0.2×⟨1,¯2⟩ + 𝕩 {∾"M l l "∾¨FmtNum⥊d⊸ע⌽˘⟨¯0.08++⟜(𝕩⊸×)´bb,𝕨-0.2⟩∾𝕩⌽bo}⌜ ↕2 + } ⊏tx + + ((∾˜d)×((-∾+˜)1‿0.2)+sh∾dim) SVG g Ge ⟨ + "rect" Elt rc ∾ (Pos 1‿0×sh)∾"width"‿"height"≍˘FmtNum d×dim + dg Ge ¯1.4‿0.1 Text "Table" + lcol Gl¨ Line¨¨ ⟨ + (((⊑sh)+0.26(⊣≍-˜)⊑dim)˙≍≍˜)¨ ¯0.06+1↓ty + (≍˜≍(0.3(⊣≍-˜)⊢´dim)˙)¨ 1↓tx + ⟩ + tg Ge (⍉tx≍⌜ty) Text¨ wt ((<"")⊸∾∾⊣∾˘∾⟜(Highlight"𝔽")⊸∾⌜) xt + cg Ge (p0⊸+¨⟨¯0.4‿0.6,0.6‿¯0.4⟩) Text¨ "𝕨"‿"𝕩" + ("Text" Attr (Pos p0+÷¯6‿16)∾"font-size"‿"26px") Enc Highlight "𝔽⌜" + bg Ge Path¨ (⥊(1↓tx) Gb ⊏ty) ∾ (⥊wb) ∾ rb + ⟩ +} +--> + +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](https://en.wikipedia.org/wiki/Cartesian_product). Below, it combines a length-3 list and a length-5 list into a shape `3‿5` table. + + "ABC" ≍⌜ "01234" + +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 + +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 `m‿n` 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 `r∾s`. + + "A "‿"B " ∾⌜ "the"‿"first"‿"row"≍"and"‿"the"‿"second" + + ≢ "A "‿"B " ∾⌜ "the"‿"first"‿"row"≍"and"‿"the"‿"second" + +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 + +<!--GEN +{ + wt ← ("w"∾'0'⊸+)¨ ↕5 + dim ← 7.5‿3.2 ⋄ sh ← ¯2.3‿¯0.1 + tx ← ↕≠xt ⋄ ty ← 0.7+↕3 + + da ← "id=gr|gradientUnits=userSpaceOnUse|x1=0|x2=0|y1=14.4|y2=216" + Stop ← "stop" Elt "offset"‿"stop-color"≍˘≍○< + defs ← "defs" Enc ("linearGradient"At da) Enc "0%"‿"70%" Stop¨ lcol + + ((∾˜d)×((-∾+˜)0.5‿0.2)+sh∾dim) SVG defs ∾ g Ge ⟨ + "rect" Elt rc ∾ (Pos 1‿0×sh)∾"width"‿"height"≍˘FmtNum d×dim + dg Ge ¯2‿0.1 Text "Each" + "url(#gr)" Gl (Line ≍˜≍(0.2(⊣≍-˜)⊢´dim)˙)¨ tx + tg Ge (⍉tx≍⌜ty) Text¨ wt (≍∾∾⟜(Highlight"𝔽")⊸∾¨) xt + cg Ge (¯1.1≍¨ty) Text⟜Highlight¨ "𝕨 "‿"𝕩"‿"𝕨𝔽¨𝕩" + bg Ge Path¨ tx Gb ty + ⟩ +} +--> + +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" + +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](transpose.md). + + 0‿0 ⍉ "ABCD" ≍⌜ "0123" + +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" + +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. + + (>⟨20‿30‿10,50‿40‿60⟩) +⟜↕¨ 2‿1‿0≍3‿2‿1 + +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](leading.md), 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. + + ≢ (0‿2‿6⥊@) ≍¨ 0‿1⥊0 # Too small + ≢ (0‿2‿6⥊@) ≍¨ 0‿2⥊0 # Just right + ≢ (0‿2‿6⥊@) ≍¨ 0‿3⥊0 # Too large + +Leading axis agreement is described further [here](leading.md#leading-axis-agreement). diff --git a/doc/primitive.md b/doc/primitive.md index d32460c5..2be91bd4 100644 --- a/doc/primitive.md +++ b/doc/primitive.md @@ -83,8 +83,8 @@ Other modifiers control array traversal and iteration. In three cases a simpler 1-Modifier | Name | 2-Modifier | Name -----------|---------------------------------------|------------|-------- `˘` | Cells | `⎉` | [Rank](https://aplwiki.com/wiki/Rank_(operator)) -`¨` | [Each](https://aplwiki.com/wiki/Each) | `⚇` | [Depth](depth.md#the-depth-modifier) -`⌜` | Table | +`¨` | [Each](map.md) | `⚇` | [Depth](depth.md#the-depth-modifier) +`⌜` | [Table](map.md) | `⁼` | Undo | `⍟` | Repeat `´` | [Fold](fold.md) | `˝` | [Insert](fold.md) | 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 @@ <li><a href="join.html">Join and Join To</a> (<code><span class='Function'>∾</span></code>)</li> <li><a href="logic.html">Logical functions</a> (<code><span class='Function'>∧∨¬</span></code>)</li> <li><a href="match.html">Match</a> (<code><span class='Function'>≡≢</span></code>)</li> +<li><a href="map.html">Mapping</a> (<code><span class='Modifier'>¨⌜</span></code>)</li> <li><a href="order.html">Ordering functions</a> (<code><span class='Function'>∧∨⍋⍒</span></code>)</li> <li><a href="prefixes.html">Prefixes and Suffixes</a> (<code><span class='Function'>↑↓</span></code>)</li> <li><a href="reverse.html">Reverse and Rotate</a> (<code><span class='Function'>⌽</span></code>)</li> 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 @@ +<head> + <link href="../favicon.ico" rel="shortcut icon" type="image/x-icon"/> + <link href="../style.css" rel="stylesheet"/> + <title>BQN: Mapping modifiers</title> +</head> +<div class="nav"><a href="https://github.com/mlochbaum/BQN">BQN</a> / <a href="../index.html">main</a> / <a href="index.html">doc</a></div> +<h1 id="mapping-modifiers">Mapping modifiers</h1> +<p>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.</p> +<p>BQN has two 1-modifiers to map over arrays: Each (<code><span class='Modifier'>¨</span></code>) and Table (<code><span class='Modifier'>⌜</span></code>). 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 (<code><span class='Modifier'>˘</span></code>) or its generalization Rank (<code><span class='Modifier2'>⎉</span></code>), which apply the function to array cells. The modifier <a href="depth.html#the-depth-modifier">Depth</a> (<code><span class='Modifier2'>⚇</span></code>) is a generalization of Each, so that <code><span class='Modifier'>¨</span></code> is <code><span class='Modifier2'>⚇</span><span class='Number'>¯1</span></code>; however, it can't be used to implement Table without some additional array operations.</p> +<h2 id="one-argument-mapping">One-argument mapping</h2> +<svg viewBox='-224 -21.6 680 187.2'> + <g fill='currentColor' font-family='BQN,monospace'> + <rect class='code' stroke-width='1.5' rx='12' x='-184' y='0' width='600' height='158.4'/> + <g font-size='24px' fill='currentColor' opacity='0.9'><text x='-168' y='7.2'>Each/Table</text></g> + <g stroke='#7f651c' stroke-width='8' opacity='0.1'> + <line x1='0' x2='0' y1='14.4' y2='144'/> + <line x1='80' x2='80' y1='14.4' y2='144'/> + <line x1='160' x2='160' y1='14.4' y2='144'/> + <line x1='240' x2='240' y1='14.4' y2='144'/> + <line x1='320' x2='320' y1='14.4' y2='144'/> + </g> + <g font-size='18px' text-anchor='middle'> + <text x='0' y='50.4'>x0</text> + <text x='80' y='50.4'>x1</text> + <text x='160' y='50.4'>x2</text> + <text x='240' y='50.4'>x3</text> + <text x='320' y='50.4'>x4</text> + <text x='0' y='122.4'><tspan class='Function'>𝔽</tspan>x0</text> + <text x='80' y='122.4'><tspan class='Function'>𝔽</tspan>x1</text> + <text x='160' y='122.4'><tspan class='Function'>𝔽</tspan>x2</text> + <text x='240' y='122.4'><tspan class='Function'>𝔽</tspan>x3</text> + <text x='320' y='122.4'><tspan class='Function'>𝔽</tspan>x4</text> + </g> + <g font-size='22px' text-anchor='end' dy='0.2'> + <text x='-88' y='50.4'><tspan class='Value'>𝕩</tspan></text> + <text x='-88' y='122.4'><tspan class='Function'>𝔽</tspan><tspan class='Modifier'>¨</tspan><tspan class='Value'>𝕩</tspan></text> + </g> + <g class='bluegreen' stroke-width='3' stroke-linecap='round' style='fill:none' opacity='0.7'> + <path d='M-32 30.24l-6.667 14.4l6.667 14.4'/> + <path d='M352 30.24l6.667 14.4l-6.667 14.4'/> + <path d='M-32 102.24l-6.667 14.4l6.667 14.4'/> + <path d='M352 102.24l6.667 14.4l-6.667 14.4'/> + </g> + </g> +</svg> + +<p>On one argument, Each and Table are identical. They apply the function <code><span class='Function'>𝔽</span></code> to every element of <code><span class='Value'>𝕩</span></code>, and return an array with the same shape that contains each result.</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oaV4oycIDPigL804oC/MgoK4oaVwqggMuKAvzLipYoz4oC/NOKAvzI=">↗️</a><pre> <span class='Function'>↕</span><span class='Modifier'>⌜</span> <span class='Number'>3</span><span class='Ligature'>‿</span><span class='Number'>4</span><span class='Ligature'>‿</span><span class='Number'>2</span> +⟨ ⟨ 0 1 2 ⟩ ⟨ 0 1 2 3 ⟩ ⟨ 0 1 ⟩ ⟩ + + <span class='Function'>↕</span><span class='Modifier'>¨</span> <span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>2</span><span class='Function'>⥊</span><span class='Number'>3</span><span class='Ligature'>‿</span><span class='Number'>4</span><span class='Ligature'>‿</span><span class='Number'>2</span> +┌─ +╵ ⟨ 0 1 2 ⟩ ⟨ 0 1 2 3 ⟩ + ⟨ 0 1 ⟩ ⟨ 0 1 2 ⟩ + ┘ +</pre> +<p>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: <code><span class='String'>"𝔽"</span><span class='Modifier2'>⊸</span><span class='Function'>∾</span></code> will place an <code><span class='Function'>𝔽</span></code> in front of the argument, which is how functions are applied.</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=IvCdlL0i4oq44oi+wqggIjDiipHwnZWpIuKAvyIx4oqR8J2VqSLigL8iMuKKkfCdlakiCgp7KCcwJyvwnZWpKeKIviLiipHwnZWpIn3ijJwg4oaVMyAgIyBNYWtpbmcg8J2VqSB3aXRoIG1hcHBpbmcgaW5zdGVhZA==">↗️</a><pre> <span class='String'>"𝔽"</span><span class='Modifier2'>⊸</span><span class='Function'>∾</span><span class='Modifier'>¨</span> <span class='String'>"0⊑𝕩"</span><span class='Ligature'>‿</span><span class='String'>"1⊑𝕩"</span><span class='Ligature'>‿</span><span class='String'>"2⊑𝕩"</span> +⟨ "𝔽0⊑𝕩" "𝔽1⊑𝕩" "𝔽2⊑𝕩" ⟩ + + <span class='Brace'>{</span><span class='Paren'>(</span><span class='String'>'0'</span><span class='Function'>+</span><span class='Value'>𝕩</span><span class='Paren'>)</span><span class='Function'>∾</span><span class='String'>"⊑𝕩"</span><span class='Brace'>}</span><span class='Modifier'>⌜</span> <span class='Function'>↕</span><span class='Number'>3</span> <span class='Comment'># Making 𝕩 with mapping instead +</span>⟨ "0⊑𝕩" "1⊑𝕩" "2⊑𝕩" ⟩ +</pre> +<p>The applications are performed in index order: index <code><span class='Value'>…</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>0</span></code>, then <code><span class='Value'>…</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span></code>, <code><span class='Value'>…</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>2</span></code> and so on, until <code><span class='Value'>…</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>0</span></code>. This can affect a program where the operand has side effects, such as the following one that appends its argument to <code><span class='Value'>o</span></code>.</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=b+KGkOKfqOKfqSDii4Qge2/iiL7in5w84oap8J2VqX3CqCAiaW5kZXgi4omNIm9yZGVyIiDii4Qgbw==">↗️</a><pre> <span class='Value'>o</span><span class='Gets'>←</span><span class='Bracket'>⟨⟩</span> <span class='Separator'>⋄</span> <span class='Brace'>{</span><span class='Value'>o</span><span class='Function'>∾</span><span class='Modifier2'>⟜</span><span class='Function'><</span><span class='Gets'>↩</span><span class='Value'>𝕩</span><span class='Brace'>}</span><span class='Modifier'>¨</span> <span class='String'>"index"</span><span class='Function'>≍</span><span class='String'>"order"</span> <span class='Separator'>⋄</span> <span class='Value'>o</span> +"indexorder" +</pre> +<p>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 <a href="reshape.html#deshape">Deshape</a>'s result, so that here <code><span class='Value'>o</span></code> ends up being <code><span class='Function'>⥊</span><span class='Value'>𝕩</span></code>. 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 <em>result</em> elements are produced in index order.</p> +<h2 id="table">Table</h2> +<svg viewBox='-206.4 -21.6 696 345.6'> + <g fill='currentColor' font-family='BQN,monospace'> + <rect class='code' stroke-width='1.5' rx='12' x='-126.4' y='0' width='536' height='316.8'/> + <g font-size='24px' fill='currentColor' opacity='0.9'><text x='-112' y='7.2'>Table</text></g> + <g stroke='#521f5e' stroke-width='8' opacity='0.1'> + <line x1='-105.6' x2='388.8' y1='125.28' y2='125.28'/> + <line x1='-105.6' x2='388.8' y1='197.28' y2='197.28'/> + <line x1='-105.6' x2='388.8' y1='269.28' y2='269.28'/> + </g> + <g stroke='#7f651c' stroke-width='8' opacity='0.1'> + <line x1='20' x2='20' y1='21.6' y2='295.2'/> + <line x1='100' x2='100' y1='21.6' y2='295.2'/> + <line x1='180' x2='180' y1='21.6' y2='295.2'/> + <line x1='260' x2='260' y1='21.6' y2='295.2'/> + <line x1='340' x2='340' y1='21.6' y2='295.2'/> + </g> + <g font-size='18px' text-anchor='middle'> + <text x='-72' y='57.6'></text> + <text x='20' y='57.6'>x0</text> + <text x='100' y='57.6'>x1</text> + <text x='180' y='57.6'>x2</text> + <text x='260' y='57.6'>x3</text> + <text x='340' y='57.6'>x4</text> + <text x='-72' y='129.6'>w0</text> + <text x='20' y='129.6'>w0<tspan class='Function'>𝔽</tspan>x0</text> + <text x='100' y='129.6'>w0<tspan class='Function'>𝔽</tspan>x1</text> + <text x='180' y='129.6'>w0<tspan class='Function'>𝔽</tspan>x2</text> + <text x='260' y='129.6'>w0<tspan class='Function'>𝔽</tspan>x3</text> + <text x='340' y='129.6'>w0<tspan class='Function'>𝔽</tspan>x4</text> + <text x='-72' y='201.6'>w1</text> + <text x='20' y='201.6'>w1<tspan class='Function'>𝔽</tspan>x0</text> + <text x='100' y='201.6'>w1<tspan class='Function'>𝔽</tspan>x1</text> + <text x='180' y='201.6'>w1<tspan class='Function'>𝔽</tspan>x2</text> + <text x='260' y='201.6'>w1<tspan class='Function'>𝔽</tspan>x3</text> + <text x='340' y='201.6'>w1<tspan class='Function'>𝔽</tspan>x4</text> + <text x='-72' y='273.6'>w2</text> + <text x='20' y='273.6'>w2<tspan class='Function'>𝔽</tspan>x0</text> + <text x='100' y='273.6'>w2<tspan class='Function'>𝔽</tspan>x1</text> + <text x='180' y='273.6'>w2<tspan class='Function'>𝔽</tspan>x2</text> + <text x='260' y='273.6'>w2<tspan class='Function'>𝔽</tspan>x3</text> + <text x='340' y='273.6'>w2<tspan class='Function'>𝔽</tspan>x4</text> + </g> + <g font-size='19px' text-anchor='middle'> + <text x='-104' y='100.8'>𝕨</text> + <text x='-24' y='28.8'>𝕩</text> + </g> + <Text x='-85.333' y='62.1' font-size='26px'><tspan class='Function'>𝔽</tspan><tspan class='Modifier'>⌜</tspan></Text> + <g class='bluegreen' stroke-width='3' stroke-linecap='round' style='fill:none' opacity='0.7'> + <path d='M-12 37.44l-6.667 14.4l6.667 14.4'/> + <path d='M372 37.44l6.667 14.4l-6.667 14.4'/> + <path d='M-88 102.24l16 -6l16 6'/> + <path d='M-88 289.44l16 6l16 -6'/> + <path d='M7 98.4h-27v27'/> + <path d='M362 297.6h18v-18'/> + </g> + </g> +</svg> + +<p>The Table modifier applies its operand function to every possible combination of one element from <code><span class='Value'>𝕨</span></code> and one from <code><span class='Value'>𝕩</span></code>, sort of like a structure-preserving and function-applying <a href="https://en.wikipedia.org/wiki/Cartesian_product">Cartesian product</a>. Below, it combines a length-3 list and a length-5 list into a shape <code><span class='Number'>3</span><span class='Ligature'>‿</span><span class='Number'>5</span></code> table.</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=IkFCQyIg4omN4oycICIwMTIzNCI=">↗️</a><pre> <span class='String'>"ABC"</span> <span class='Function'>≍</span><span class='Modifier'>⌜</span> <span class='String'>"01234"</span> +┌─ +╵ "A0" "A1" "A2" "A3" "A4" + "B0" "B1" "B2" "B3" "B4" + "C0" "C1" "C2" "C3" "C4" + ┘ +</pre> +<p>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 (<code><span class='Modifier'>˜</span></code>):</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=w5fijJzLnCAxK+KGlTY=">↗️</a><pre> <span class='Function'>×</span><span class='Modifier'>⌜˜</span> <span class='Number'>1</span><span class='Function'>+↕</span><span class='Number'>6</span> +┌─ +╵ 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 + ┘ +</pre> +<p>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 <code><span class='Value'>m</span><span class='Ligature'>‿</span><span class='Value'>n</span></code> if <code><span class='Value'>𝕨</span></code> is a list of length <code><span class='Value'>m</span></code> and <code><span class='Value'>𝕩</span></code> is a list of length <code><span class='Value'>n</span></code>, the result shape for an array <code><span class='Value'>𝕨</span></code> of shape <code><span class='Value'>r</span></code> and <code><span class='Value'>𝕩</span></code> of shape <code><span class='Value'>s</span></code> is <code><span class='Value'>r</span><span class='Function'>∾</span><span class='Value'>s</span></code>.</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=IkEgIuKAvyJCICIg4oi+4oycICJ0aGUi4oC/ImZpcnN0IuKAvyJyb3ci4omNImFuZCLigL8idGhlIuKAvyJzZWNvbmQiCgriiaIgIkEgIuKAvyJCICIg4oi+4oycICJ0aGUi4oC/ImZpcnN0IuKAvyJyb3ci4omNImFuZCLigL8idGhlIuKAvyJzZWNvbmQi">↗️</a><pre> <span class='String'>"A "</span><span class='Ligature'>‿</span><span class='String'>"B "</span> <span class='Function'>∾</span><span class='Modifier'>⌜</span> <span class='String'>"the"</span><span class='Ligature'>‿</span><span class='String'>"first"</span><span class='Ligature'>‿</span><span class='String'>"row"</span><span class='Function'>≍</span><span class='String'>"and"</span><span class='Ligature'>‿</span><span class='String'>"the"</span><span class='Ligature'>‿</span><span class='String'>"second"</span> +┌─ +╎ "A the" "A first" "A row" + "A and" "A the" "A second" + + "B the" "B first" "B row" + "B and" "B the" "B second" + ┘ + + <span class='Function'>≢</span> <span class='String'>"A "</span><span class='Ligature'>‿</span><span class='String'>"B "</span> <span class='Function'>∾</span><span class='Modifier'>⌜</span> <span class='String'>"the"</span><span class='Ligature'>‿</span><span class='String'>"first"</span><span class='Ligature'>‿</span><span class='String'>"row"</span><span class='Function'>≍</span><span class='String'>"and"</span><span class='Ligature'>‿</span><span class='String'>"the"</span><span class='Ligature'>‿</span><span class='String'>"second"</span> +⟨ 2 2 3 ⟩ +</pre> +<p>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 <code><span class='Value'>𝕨</span></code> is visited in turn, and each time the element is paired with every element of <code><span class='Value'>𝕩</span></code>.</p> +<h2 id="each">Each</h2> +<svg viewBox='-224 -21.6 680 259.2'> + <defs> + <linearGradient id='gr' gradientUnits='userSpaceOnUse' x1='0' x2='0' y1='14.4' y2='216'> + <stop offset='0%' stop-color='#521f5e'/> + <stop offset='70%' stop-color='#7f651c'/> + </linearGradient> + </defs> + <g fill='currentColor' font-family='BQN,monospace'> + <rect class='code' stroke-width='1.5' rx='12' x='-184' y='0' width='600' height='230.4'/> + <g font-size='24px' fill='currentColor' opacity='0.9'><text x='-160' y='7.2'>Each</text></g> + <g stroke='url(#gr)' stroke-width='8' opacity='0.1'> + <line x1='0' x2='0' y1='14.4' y2='216'/> + <line x1='80' x2='80' y1='14.4' y2='216'/> + <line x1='160' x2='160' y1='14.4' y2='216'/> + <line x1='240' x2='240' y1='14.4' y2='216'/> + <line x1='320' x2='320' y1='14.4' y2='216'/> + </g> + <g font-size='18px' text-anchor='middle'> + <text x='0' y='50.4'>w0</text> + <text x='80' y='50.4'>w1</text> + <text x='160' y='50.4'>w2</text> + <text x='240' y='50.4'>w3</text> + <text x='320' y='50.4'>w4</text> + <text x='0' y='122.4'>x0</text> + <text x='80' y='122.4'>x1</text> + <text x='160' y='122.4'>x2</text> + <text x='240' y='122.4'>x3</text> + <text x='320' y='122.4'>x4</text> + <text x='0' y='194.4'>w0<tspan class='Function'>𝔽</tspan>x0</text> + <text x='80' y='194.4'>w1<tspan class='Function'>𝔽</tspan>x1</text> + <text x='160' y='194.4'>w2<tspan class='Function'>𝔽</tspan>x2</text> + <text x='240' y='194.4'>w3<tspan class='Function'>𝔽</tspan>x3</text> + <text x='320' y='194.4'>w4<tspan class='Function'>𝔽</tspan>x4</text> + </g> + <g font-size='22px' text-anchor='end' dy='0.2'> + <text x='-88' y='50.4'><tspan class='Value'>𝕨</tspan> </text> + <text x='-88' y='122.4'><tspan class='Value'>𝕩</tspan></text> + <text x='-88' y='194.4'><tspan class='Value'>𝕨</tspan><tspan class='Function'>𝔽</tspan><tspan class='Modifier'>¨</tspan><tspan class='Value'>𝕩</tspan></text> + </g> + <g class='bluegreen' stroke-width='3' stroke-linecap='round' style='fill:none' opacity='0.7'> + <path d='M-32 30.24l-6.667 14.4l6.667 14.4'/> + <path d='M352 30.24l6.667 14.4l-6.667 14.4'/> + <path d='M-32 102.24l-6.667 14.4l6.667 14.4'/> + <path d='M352 102.24l6.667 14.4l-6.667 14.4'/> + <path d='M-32 174.24l-6.667 14.4l6.667 14.4'/> + <path d='M352 174.24l6.667 14.4l-6.667 14.4'/> + </g> + </g> +</svg> + +<p>Given two arguments of matching shapes, Each performs what's sometimes called a "zip", matching each element of <code><span class='Value'>𝕨</span></code> to the corresponding element of <code><span class='Value'>𝕩</span></code>.</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=IkFCQ0QiIOKJjcKoICIwMTIzIg==">↗️</a><pre> <span class='String'>"ABCD"</span> <span class='Function'>≍</span><span class='Modifier'>¨</span> <span class='String'>"0123"</span> +⟨ "A0" "B1" "C2" "D3" ⟩ +</pre> +<p>This makes for a lot fewer applications than Table. Only the diagonal elements from Table's result are seen, as we can check with <a href="transpose.html">Transpose</a>.</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MOKAvzAg4o2JICJBQkNEIiDiiY3ijJwgIjAxMjMi">↗️</a><pre> <span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>0</span> <span class='Function'>⍉</span> <span class='String'>"ABCD"</span> <span class='Function'>≍</span><span class='Modifier'>⌜</span> <span class='String'>"0123"</span> +⟨ "A0" "B1" "C2" "D3" ⟩ +</pre> +<p>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.</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=IkFCQyIg4omNwqggIjAxMjM0Ig==">↗️</a><pre> <span class='String'>"ABC"</span> <span class='Function'>≍</span><span class='Modifier'>¨</span> <span class='String'>"01234"</span> +ERROR +</pre> +<p>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.</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=KD7in6gyMOKAvzMw4oC/MTAsNTDigL80MOKAvzYw4p+pKSAr4p+c4oaVwqggMuKAvzHigL8w4omNM+KAvzLigL8x">↗️</a><pre> <span class='Paren'>(</span><span class='Function'>></span><span class='Bracket'>⟨</span><span class='Number'>20</span><span class='Ligature'>‿</span><span class='Number'>30</span><span class='Ligature'>‿</span><span class='Number'>10</span><span class='Separator'>,</span><span class='Number'>50</span><span class='Ligature'>‿</span><span class='Number'>40</span><span class='Ligature'>‿</span><span class='Number'>60</span><span class='Bracket'>⟩</span><span class='Paren'>)</span> <span class='Function'>+</span><span class='Modifier2'>⟜</span><span class='Function'>↕</span><span class='Modifier'>¨</span> <span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Function'>≍</span><span class='Number'>3</span><span class='Ligature'>‿</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>1</span> +┌─ +╵ ⟨ 20 21 ⟩ ⟨ 30 ⟩ ⟨⟩ + ⟨ 50 51 52 ⟩ ⟨ 40 41 ⟩ ⟨ 60 ⟩ + ┘ +</pre> +<p>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 <a href="leading.html">leading axis convention</a>, 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.</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4omiICgw4oC/MuKAvzbipYpAKSDiiY3CqCAw4oC/MeKlijAgICMgVG9vIHNtYWxsCuKJoiAoMOKAvzLigL824qWKQCkg4omNwqggMOKAvzLipYowICAjIEp1c3QgcmlnaHQK4omiICgw4oC/MuKAvzbipYpAKSDiiY3CqCAw4oC/M+KlijAgICMgVG9vIGxhcmdl">↗️</a><pre> <span class='Function'>≢</span> <span class='Paren'>(</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>6</span><span class='Function'>⥊</span><span class='String'>@</span><span class='Paren'>)</span> <span class='Function'>≍</span><span class='Modifier'>¨</span> <span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Function'>⥊</span><span class='Number'>0</span> <span class='Comment'># Too small +</span>ERROR + <span class='Function'>≢</span> <span class='Paren'>(</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>6</span><span class='Function'>⥊</span><span class='String'>@</span><span class='Paren'>)</span> <span class='Function'>≍</span><span class='Modifier'>¨</span> <span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>2</span><span class='Function'>⥊</span><span class='Number'>0</span> <span class='Comment'># Just right +</span>⟨ 0 2 6 ⟩ + <span class='Function'>≢</span> <span class='Paren'>(</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>6</span><span class='Function'>⥊</span><span class='String'>@</span><span class='Paren'>)</span> <span class='Function'>≍</span><span class='Modifier'>¨</span> <span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>3</span><span class='Function'>⥊</span><span class='Number'>0</span> <span class='Comment'># Too large +</span>ERROR +</pre> +<p>Leading axis agreement is described further <a href="leading.html#leading-axis-agreement">here</a>.</p> 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 @@ </tr> <tr> <td><code><span class='Modifier'>¨</span></code></td> -<td><a href="https://aplwiki.com/wiki/Each">Each</a></td> +<td><a href="map.html">Each</a></td> <td><code><span class='Modifier2'>⚇</span></code></td> <td><a href="depth.html#the-depth-modifier">Depth</a></td> </tr> <tr> <td><code><span class='Modifier'>⌜</span></code></td> -<td>Table</td> +<td><a href="map.html">Table</a></td> <td></td> <td></td> </tr> |
