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.

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 ⟨ ⟨ 0 1 2 ⟩ ⟨ 0 1 2 3 ⟩ ⟨ 0 1 ⟩ ⟩ ↕¨ 2‿2⥊3‿4‿2 ┌─ ╵ ⟨ 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 `…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 "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 as easy to describe it in terms of the arguments: instead, the *result* elements are produced in index order.

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" ┌─ ╵ "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 `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 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 `𝕩`

.

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.

↗️0‿0 ⍉ "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 natural for linked lists). This flexibility is rarely wanted in BQN, and having an error right away saves debugging time.

↗️"ABC" ≍¨ "01234" Error: Mapping: Expected equal shape prefix (⟨3⟩ ≡ ≢𝕨, ⟨5⟩ ≡ ≢𝕩)

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 ┌─ ╵ ⟨ 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.

↗️≢ (0‿2‿6⥊@) ≍¨ 0‿1⥊0 # Too small Error: Mapping: Expected equal shape prefix (0‿2‿6 ≡ ≢𝕨, 0‿1 ≡ ≢𝕩) ≢ (0‿2‿6⥊@) ≍¨ 0‿2⥊0 # Just right ⟨ 0 2 6 ⟩ ≢ (0‿2‿6⥊@) ≍¨ 0‿3⥊0 # Too large Error: Mapping: Expected equal shape prefix (0‿2‿6 ≡ ≢𝕨, 0‿3 ≡ ≢𝕩)

Leading axis agreement is described further here.