# Arrays


<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

BPL has two kinds of value. Numbers, characters and functions are atoms.
An array is a rectangular collection of values, including other arrays.
This is the *based array* model, named in a [1981
paper](https://dl.acm.org/doi/abs/10.1145/586656.586663) and popularized
by [BQN](https://mlochbaum.github.io/BQN/doc/based.html).

## Writing vectors

Write a vector by putting its items in brackets, separated by spaces. An
item can be any expression without a space in it, such as a number, a
name, `x+1` or a string:

``` bpl
x←10
[x x+1 "hi"]
```

    10 11 "hi"

A bracket list is a vector, however many items it has. Shape, monadic
`⍴`, shows that `[5]` has one axis of length 1, where the number `5` has
none:

``` bpl
⍴[5]
⍴5
```

    [1]ₓ

    ⍬ₓ

A list of literals can leave its brackets out. Values written side by
side form a [strand](terms.qmd#term-strand), the same vector as the
bracket list:

``` bpl
1 2 3≡[1 2 3]
```

    $t

Names side by side form a strand too:

``` bpl
a←1
b←2
a b
```

    1 2

Matrices are written in brackets too, one row per line. [Types of
brackets](bracket-types.ipynb) covers this array notation, along with
what `;` and `⋄` mean inside brackets:

``` bpl
m←[1 2 3
   4 5 6]
m
```

    1 2 3
    4 5 6

## Shape and rank

An array’s shape lists the lengths of its axes. Its rank is the number
of axes, the tally of its shape. Scalars, vectors and matrices are the
arrays of rank 0, 1 and 2. `m` has two rows and three columns:

``` bpl
⍴m
≢⍴m
```

    [2 3]ₓ

    2ₓ

Reshape, dyadic `⍴`, builds an array of any shape from the items on its
right. `⍳6` counts from 0 to 5:

``` bpl
2 3⍴⍳6
```

    0 1 2
    3 4 5

An axis can have length 0. This matrix has no rows and three columns:

``` bpl
⍴0 3⍴0
```

    [0 3]ₓ

Ravel, monadic `,`, lists the items in row-major order:

``` bpl
,m
```

    1 2 3 4 5 6

## Atoms and scalars

Neither an atom nor a scalar, the array of rank 0, has any axes.
Enclose, `⊂`, gives a scalar holding its argument:

``` bpl
⍴3
⍴⊂3
```

    ⍬ₓ

    ⍬ₓ

They’re still different values. A scalar is an array with one position,
holding the atom:

``` bpl
3≡⊂3
```

    $f

Enclosing always adds a layer, even around an atom. Depth, monadic `≡`,
counts the layers:

``` bpl
≡3
≡⊂3
≡⊂⊂3
```

    0ₓ

    1ₓ

    2ₓ

`⊂` encloses only subjects. For a function `f`, the scalar holding it is
`fᵘ`:

``` bpl
≡+ᵘ
```

    1ₓ

## Nesting

The items of an array can be arrays. This vector holds two vectors of
different lengths:

``` bpl
n←[[1 2] [3 4 5]]
n
```

    (1 2 ⋄ 3 4 5)

First, monadic `↑`, gives the first item. Each, `¨`, applies a function
to every item, here Tally, `≢`:

``` bpl
↑n
≢¨n
```

    1 2

    [2 3]ₓ

`n`, a vector of vectors of numbers, has depth 2:

``` bpl
≡n
```

    2ₓ

Arithmetic, Each and indexing keep the container they map over, even a
scalar:

``` bpl
(⊂3)+4
-¨⊂3
```

    ⊂7

    ⊂¯3

Rank and the search functions work on whole cells instead, as
[Rank](glyphs/rank.qmd) describes.

## Fill

Every array has a prototype, which gives its fill. The fill is zero for
numbers, a space for characters, and a filled copy of the first item for
nested arrays. Take, dyadic `↑`, pads with fill when it asks for more
items than there are:

``` bpl
5↑[1 2]
"<",(3↑"ab"),">"
```

    1 2 0 0 0

    <ab >

An empty array keeps its prototype. First of this empty vector gives the
fill of `1 2`, the first item of the vector it came from:

``` bpl
↑0⍴n
```

    0 0

When results of different lengths are assembled into one array, the
shorter ones are padded with fill. `{1+⍳⍵}⍤0` gives one row per count:

``` bpl
{1+⍳⍵}⍤0 [2 4]
```

    1 2 0 0
    1 2 3 4

Each keeps the results as separate items instead:

``` bpl
{1+⍳⍵}¨[2 4]
```

    (1 2 ⋄ 1 2 3 4)

## Keys

Keys label the positions along an axis. Write them with `:`, then pick
an item by its key with a dot:

``` bpl
T←"x" "y":1 2
T
T.x
```

    ["x":1 "y":2]

    1

Axes can also have names. A shape with keys names the axes Reshape
builds. [Axis keys](keyed.ipynb) covers keyed arrays in full:

``` bpl
M←["city":2 "month":3]⍴⍳6
⍴M
```

    ["city":2 "month":3]ₓ

## Functions in arrays

An array can hold functions. `fs` is a vector of three:

``` bpl
fs←[+ × ÷]
fs
```

    [+ × ÷]

Selecting an item gives back a function you can call. A
[subscript](scripts.ipynb#subscripts) selects one position:

``` bpl
2 fs₁ 3
```

    6

Pick, `⊃`, First, `↑`, and [Index](glyphs/squad.qmd), `⌷`, with a single
position also return stored functions. `↑⌽fs` is the last one:

``` bpl
div←↑⌽fs
6 div 3
```

    2

A list can mix functions with other values, including a vector of
functions as one item:

``` bpl
mixed←[[1 2 3] + ×]
≢mixed
≢[fs ÷]
```

    3ₓ

    2ₓ

Successive Picks go down into nested arrays. Empty coordinates return
the argument unchanged:

``` bpl
x←[[+ ×] [- ÷]]
f←1⊃0⊃x
2 f 3
(⍬⊃n)≡n
```

    6

    $t

A dfn can return a function it selects. [Agenda](glyphs/agenda.qmd)
selects one and calls it in one step:

``` bpl
g←{1⊃⍵}fs
2 g 3
```

    6

Function items are shared handles, not source text. Reshape, selection,
catenation and the other structural functions move them without running
them. Display prints each function as its source. A native function with
no source spelling, such as a generator’s `roll`, prints in `⟨…⟩`. A
function’s fill is the function itself, which Take shows:

``` bpl
5↑fs
```

    [+ × ÷ + +]

Two functions match when they’re built from matching parts. A primitive
matches itself, as does a derived function such as `+/`, however many
times either is written. A dfn matches only itself. Two separately
written `{⍵}` don’t match, even though they do the same thing. Functions
have no ordering, and Grade and Interval Index reject them.

``` bpl
[+]≡[+]
[+/]≡[+/]
h←{⍵}
[h]≡[h]
[{⍵}]≡[{⍵}]
```

    $t

    $t

    $t

    $f

A dfn can return a function. A local function it captures works while
the call that defined it is still running. Returning it, or storing it
anywhere that outlives the call, including inside an array or an empty
prototype, is a `DOMAIN` error.

In Python, arrays of functions work as [Callable
functions](python.ipynb#callable-functions) describes.
