"Word"Word
Read Basics, Functions, and Operators first. An array is nested when at least one of its items is itself an array. We’ll work with those items, then combine functions without naming their arguments.
↑ (Up arrow)↑ (First)First returns the first item of a vector. For a string, that item is a character.
If the first item is an array, First returns that whole item. A matrix’s rows are its major cells. First returns its first major cell.
First’s inverse chooses an enclosure holding its argument. An enclosure is a scalar, an array of rank 0 with one item. The display marks it with ⊂. First returns that enclosed item.
↑ (Take)For a vector ⍵, a positive count ⍺ takes its first ⍺ items.
If the magnitude of the count exceeds the vector’s length, Take adds extra items called fill. For characters the fill is a space, and for numbers it is zero.
A negative count takes items from the end instead of the start.
For higher-rank arrays, a single count takes major cells along the first axis. Negative counts and padding work the same way.
A vector of counts applies to successive axes. 2 ¯3↑mat takes the first two rows and the last three columns.
↓ (Down arrow)↓ (Split)Split nests the vectors along the last axis. For a matrix it returns a vector containing its rows. Parentheses with ⋄ between items, such as (1 2⋄3 4), display a vector of vectors.
Split’s inverse joins these row vectors back into a matrix.
For a rank-3 array, Split keeps the first two axes and nests the vectors along the last:
↓ (Drop)For a vector ⍵, a positive count ⍺ drops its first ⍺ items.
A negative count drops items from the end instead of the start.
For higher-rank arrays, a single count drops major cells along the first axis.
A vector of counts drops along successive axes. 2 ¯3↓mat removes the first two rows and the last three columns.
⊂ (Left shoe)⊂ (Enclose)Enclose makes the scalar we met with First’s inverse. Start with a vector of length 3:
Enclosing the vector adds a layer. Its shape is now the empty vector, because a scalar has no axes.
Enclose’s inverse removes the enclosure and returns its item, as First does.
Brackets make nested vectors directly. [1 [2 3]] has two items, whereas ⊂[1 [2 3]] is a scalar holding that whole vector.
Catenate takes the scalar’s one item, adding the whole nested vector:
Each Enclose adds another layer. First removes the outer enclosure.
A number such as 1 is an atom, not a scalar. Enclosing it creates a scalar holding that number. They have the same rank but are different values.
Arithmetic extends a scalar across a vector. Here each left-hand number adds to the whole enclosed vector:
An atom extends too. Adding 1 adds to every item without adding nesting:
Write ᵘ after a function to enclose it. +ᵘ is a scalar holding Plus, with an empty shape. First takes the function back out.
⊂ (Partitioned enclose)Partitioned enclose splits an array into parts, called partitions. A positive mark in the left argument starts a partition before that position. Zeros continue the current partition. Here the marks at positions 0 and 2 split the vector into two parts:
The same marks split a string:
A mark greater than 1 starts that many partitions, putting empty partitions before the one containing the item. Leading zeros leave out the items before the first partition.
With the marks fixed, Partitioned enclose’s inverse joins the parts. The marks must reproduce those parts when applied again; leading omitted items cannot be recovered.
⊆ (Left shoe underbar)⊆ (Nest)Nest encloses atoms and arrays whose items are all atoms. An array that is already nested stays unchanged.
Nest’s inverse removes a scalar’s enclosure. Other arrays stay unchanged.
⊆ (Partition)Partition starts a part at the first positive mark and whenever a mark increases from the preceding one. A zero leaves out its item.
In the next example, zero marks exclude the spaces. Each rise from 0 to 1 starts the next word.
A decrease in the markers does not start a new partition:
Partition’s inverse joins the parts using the same marks. All marks must be positive, because a zero would have discarded an item.
⊃ (Right shoe)⊃ (Mix)Mix turns a vector of vectors into a matrix, with one vector per row.
If the vectors have different lengths, Mix pads the shorter ones with fill. For strings, the fill character is a space.
Mix’s inverse splits a matrix into rows. Padding remains part of each row.
Items can be numbers or nested arrays. Mix assembles them into rows and pads with zeros where needed.
⊃ (Pick)Pick selects inside arrays. For a vector, a single position selects an item, counting from 0. Position 2 of "Word" is its third character.
If the selected item is an array, Pick returns that whole array.
A vector of positions defines a path through nested items. 1 0 selects item 1, which is 3 4 5, then item 0 of that vector, which is 3.
A path item can select several axes at once by listing one position for each axis. The path [[1 0]] has one item, 1 0, and selects row 1, column 0.
Add another path item to select inside that result. Each item of this matrix is a two-item vector with a string and a number. The path first selects row 1, column 0, then item 0 of that vector.
["ABC" 1] ["DEF" 2] ["GHI" 3]
["JKL" 4] ["MNO" 5] ["PQR" 6]
⌸ (Quad equal)⌸ (Key operator)Key groups equal values and calls its function operand on each group. With one argument, that operand receives the group’s value as ⍺ and its positions as ⍵.
Catenate joins each character with its positions. In "banana", b is at position 0, a at positions 1, 3 and 5, and n at positions 2 and 4. The rows have different lengths, so their results are padded with fill.
With two arguments, Key groups the right argument by the values on the left. Here the right argument contains positions, and the dfn joins each character with the sum of its positions.
A string key labels a position in an array, independently of the grouping operator ⌸. Write key:value items in brackets to make a keyed vector, also called a record.
Pick accepts a key instead of a position. Writing .price after a record reads its "price" item.
Arithmetic aligns the keys, even when their order differs:
Dyadic : attaches keys to existing data. An axis can also have a name. Here city and month name the two axes, while NY, LA, and the month names label positions along them. The semicolon keeps each vector of labels together.
Jan Feb Mar
NY 10 20 30
LA 40 50 60
Shape includes the axis names. Pick follows a path through the keys in the usual way:
The Axis operator’s right operand can be an axis name. Sum across month to get one total per city:
Monadic : removes keys. With the labels fixed on the left, its inverse first checks that they match the array’s labels.
See Axis keys for keyed selection, assignment, and agreement between named axes.
⊢ (Right tack)⊢ (Same)Return the argument back unchanged
Same is its own inverse.
⊢ (Right)Returns the right argument back unchanged
Right’s inverse returns its right argument unchanged too.
⊣ (Left tack)⊣ (Same)Returns the argument back unchanged
This monadic form is also its own inverse.
⊣ (Left)Returns the left argument back unchanged
Left discards its right argument. Its inverse accepts a result matching the fixed left argument and returns that result as one possible right argument.
⊥ (Up tack)⊥ (Binary decode)Binary uses the digits 0 and 1 with place values 1, 2, 4, 8, and so on from right to left. Decode reads these digits along the last axis.
Binary Decode’s inverse writes the binary digits instead of reading them.
⊥ (Decode)The left argument gives the base, the multiplier between adjacent place values. 2⊥1 1 0 1 reads the digits as binary: 8 + 4 + 0 + 1, or 13.
Different positions can use different bases. For hours, minutes, and seconds, the two 60s mean 60 seconds per minute and 60 minutes per hour. Decode does not use the first base, which we write as 0.
With mixed bases fixed on the left, Decode’s inverse writes one digit for each base.
Base 10 reads decimal digits:
A single fixed base makes the inverse choose enough digits for the number.
The base can be complex too:
Base 1 adds the digits. 1⊥M sums the rows of a matrix, like +/M.
⊤ (Down tack)⊤ (Binary encode)Encode writes binary digits, choosing enough positions for the number:
With several numbers, each row uses enough digits for the largest number:
Binary Encode’s inverse reads the digits back into numbers.
⊤ (Encode)The left argument lists the bases, one for each digit. Five decimal positions leave a leading zero:
Three decimal positions keep only the last three digits. A zero base in the first position keeps the leading part as one number:
Changing every base to 2 gives binary digits:
Two positions keep only the last two binary digits:
A vector of numbers becomes a matrix with one number’s digits in each row. The digit axis is the last axis.
Mixed bases convert 10000 seconds back to hours, minutes, and seconds:
With the bases fixed, Encode’s inverse reads digits in those bases. Digits discarded by encoding are not recovered.
A run that ends in a function is a train. Three functions form a fork, f g h, giving (f x) g (h x). The middle function takes two arguments. In mathematical notation, (f+g)(x)=f(x)+g(x) is one example of this pattern.
For example, add the reciprocal of a number to its exponential:
The fork applies both outer functions to 3 before adding their results. A fork with Sum and Tally as its outer functions, and Divide in the middle, computes the mean:
A space before the argument separates the train from the call. +/÷≢ x applies the mean function to x. Without that space, +/÷≢x reads as +/(÷(≢x)).
With two arguments, a fork f g h gives (a f b) g (a h b). Each outer function receives both arguments. We can split a string at spaces by using ≠ to mark its non-space characters and ⊢ to supply the string itself.
Give the fork a name to reuse it:
A two-function train is an atop. f g x means f(g x). For example, -| takes the magnitude of its argument and then negates it:
Previous: Operators and axes · Study guide · Next: Searching and indexing