Skip to main content

Arrays

Atoms and arrays: writing vectors, shape and rank, nesting, fill, keys, and arrays of functions

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 and popularized by BQN.

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 or a string:

x←10
[x ≥x "hi"]
10 11 "hi"

A bracket list is a vector, however many items it has. ρ shows that [5] has one axis of length 1, whereas the number 5 has no axes:

ρ[5]
ρ5
[1]ₓ
⍬ₓ

You can omit the brackets around a list of literals. Values written side by side form a strand, the same vector as the bracket list:

1 2 3≡[1 2 3]
$t

Names side by side form a strand too:

a←1
b←2
a b
1 2

Matrices are written in brackets too, one row per line. Types of brackets covers this array notation, along with what ; and ◇ mean inside brackets:

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:

ρm
≢ρm
[2 3]ₓ
2ₓ

ρ builds an array of any shape from the items on its right. ι6 counts from 0 to 5:

2 3ρι6ₓ
[0 1 2
 3 4 5]ₓ

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

ρ0 3ρ0
[0 3]ₓ

, lists the items in row-major order:

,m
1 2 3 4 5 6

Atoms and scalars

An atom has no axes, and neither does a scalar, which is the array of rank 0. Enclose ⊂ gives a scalar holding its argument:

ρ3
ρ⊂3
⍬ₓ
⍬ₓ

3 and ⊂3 are still different values. The scalar ⊂3 is an array with one position, holding the atom 3:

3≡⊂3
$f

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

≡3
≡⊂3
≡⊂⊂3
0ₓ
1ₓ
2ₓ

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

≡+ˢ
1ₓ

Nesting

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

n←[[1 2] [3 4 5]]
n
(1 2◇3 4 5)

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

↑n
≢¨n
1 2
[2 3]ₓ

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

≡n
2ₓ

Arithmetic, Each and indexing on a scalar give a scalar, not the atom inside it:

⊂3 + 4
-¨⊂3
⊂7
⊂¯3

Rank and the search functions work on whole cells instead, as Rank describes.

Fill

Every array has a prototype, the value that it supplies as fill: zero for numbers, a space for characters, and a filled copy of the first item for nested arrays. ↑ pads with fill when its left argument is greater than the length of its right argument:

5↑[1 2]
"<",3↑"ab" , ">"
1 2 0 0 0
"<ab >"

An empty array keeps its prototype. First of 0ρn gives the fill of 1 2, the first item of n:

↑0ρn
0 0

When results of different lengths are assembled into one array, the shorter ones are padded with fill. ≥ι counts from 1 up to its argument, and with ⁰ it runs once for each item, which gives one row per count:

≥ι ⁰ [2 4]
1 2 0 0
1 2 3 4

Each keeps the results as separate items instead:

≥ι ¨[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:

T←"x" "y":1 2
T
T.x
["x":1 "y":2]
1

Axes can also have names. Pass Reshape a keyed shape to name the axes it builds. Axis keys covers keyed arrays in full:

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

fs←[+ × ÷]
fs
[+ × ÷]

Selecting an item gives back a function. A subscript selects one position. To call the selected function, name it first, as with any function that a call or a selection returns:

times←fs₁
2 times 3
6

Pick ⊃, First ↑ and Index ⌷ with a single position also return stored functions. ↑⌽fs is the last function in fs:

div←↑⌽fs
6 div 3
2

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

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

Successive Picks select items from successively nested arrays. Picking with an empty vector returns the right argument unchanged:

x←[[+ ×] [- ÷]]
f←1⊃0⊃x
2 f 3
⍬⊃n ≡ n
6
$t

A dfn can return a function it selects. Agenda selects one and calls it in one step:

g←{1⊃ω}fs
2 g 3
6

An array that holds a function as an item is callable. Each function is called with the whole arguments, and each other item is returned unchanged:

2 fs 8
10 16 0.25

The results have the array’s shape and keys as their frame, as Rank’s results do. Calling a keyed array of aggregation functions gives a keyed result, and calling it through Rank gives one row of results for each row of its argument. Write Rank out as ⍤1 here, because stats¹ would raise the items of stats to a power:

stats←["mean":+/÷≢ "max":⌈/]
stats 3 1 4 1 5
stats⍤1[3 1 4◇1 5 9]
["mean":2.8 "max":5]
       mean max
2.666666667   4
          5   9

Results of different shapes are padded with fill:

[ι ⊢] 3
0 1 2
3 0 0

An array is callable only when one of its direct items is a function. Calling a scalar that holds a function, such as -ˢ, calls that function, and its empty frame adds no axis. An enclosed function array holds an array rather than a function, so (⊂fs) 2 is a strand:

-ˢ 3
(⊂fs) 2
¯3
[⊂[+ × ÷] 2]

A function array acts as a function only where a subject follows it. Elsewhere it’s an array, so ≢fs counts its items. Binding gives the rule, and Operators and operands says which operators call the array:

≢fs
3ₓ

An array holds a function item as a reference to the function. Reshape, selection, catenation and the other structural functions move function items 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:

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.

[+]≡[+]
[+/]≡[+/]
h←{ω}
[h]≡[h]
[{ω}]≡[{ω}]
$t
$t
$t
$f

A dfn can return a function. A function that a dfn defines in its body works only while the call that defined it is running. Returning such a function, or storing it anywhere that outlives the call, including inside an array or an empty array’s prototype, raises a DOMAIN error.

In Python, arrays of functions work as Callable functions describes.