Skip to content

Latest commit

 

History

History
167 lines (119 loc) · 8.42 KB

map.md

File metadata and controls

167 lines (119 loc) · 8.42 KB

View this file with results and syntax highlighting here.

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.

As a result, 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

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.

    ["index","order"]

    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'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 as easy to describe it in terms of the arguments: instead, the result elements are produced in index order.

Table

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 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

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 Reorder Axes.

    0‿0 ⍉ "ABCD" ⋈⌜ "0123"

If the argument lengths don't match then Each gives an error. This differs from zip in many languages, which drops elements from the longer argument (this is natural for linked lists). This flexibility 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 these shapes 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 by leading axis agreement, 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‿3⥊0  # Too large

    ≢ (0‿2‿6⥊@) ⋈¨ 0‿2⥊0  # Just right