Chapter 3 of 8

Verbs: the vocabulary you use every day

There are about thirty verbs. You will use twelve of them constantly and meet the rest when you need them. This chapter is the twelve, grouped by intent rather than by glyph, because that is how you will look for them.

\+ at the prompt prints the complete table with an example per line. Keep it open in a second terminal while you read this.

Selecting and slicing

x: 10 20 30 40 50
x @ 0                  / 10        -- index
x @ 0 2                / 10 30     -- index with a vector: gather
x 0 2                  / 10 30     -- @ is optional for a vector on the left
2 # x                  / 10 20     -- take the first 2
-2 # x                 / 40 50     -- take the last 2
2 _ x                  / 30 40 50  -- drop the first 2
x @ & x > 25           / 30 40 50  -- mask, where, gather
| x                    / 50 40 30 20 10

Searching and set operations

x: 10 20 30 40 50
x ? 30                 / 2         -- find: first index, the count of x if absent
? 1 2 2 3 1            / 1 2 3     -- distinct, first-appearance order
1 2 3 in 2 3 4         / 0 1 1     -- membership mask
except[1 2 3; 2]       / 1 3
inter[1 2 3; 2 3 4]    / 2 3
union[1 2; 2 3]        / 1 2 3
within[25; 10 50]      / 1         -- inclusive range test
An atom on the right is a one-element set

1 2 3 in 2 is 0 1 0, exactly as if you had written in[1 2 3; ,2]. This was wrong before 2.2.0, where it silently returned two random numbers, which is worth knowing if you are reading older material.

Sorting and grading

x: 30 10 20
asc x                  / 10 20 30      -- sorted values
< x                    / 1 2 0         -- GRADE: the permutation that sorts
x @ < x                / 10 20 30      -- ... which is what asc does
desc x  /  > x         / descending value sort / grade
rank x                 / 2 0 1         -- rank of each element

The distinction between asc (values) and < (a permutation) is the one to hold on to. The grade is what lets you sort one vector by another:

sym: `c`a`b
px:  30 10 20
sym @ < px             / `a`b`c   -- symbols ordered by price
asc sets an attribute for you

asc and xasc mark the result `s (sorted). A later ?, in or aj on that vector takes an O(log n) binary search instead of a linear scan. Chapter 7 covers attributes.

Reshaping

2 3 # !6               / (0 1 2; 3 4 5)   -- reshape into 2 rows of 3
, 1 2                  / ,1 2             -- enlist: wrap in one more level
raze (1 2; 3 4)        / 1 2 3 4          -- flatten one level
+ (1 2; 3 4)           / (1 3; 2 4)       -- flip (transpose)
1 2 , 3 4              / 1 2 3 4          -- concatenate

Arithmetic you will actually type

x: 1 2 3 4
+/ x                   / 10       -- sum
*/ x                   / 24       -- product
|/ x                   / 4        -- max
&/ x                   / 1        -- min
+\ x                   / 1 3 6 10 -- running sum (scan)
avg x  /  med x        / mean / median
dev x  /  var x        / standard deviation / variance

+/ is not a special "sum" function. It is the verb + with the adverb / (fold) applied to it. That composability is the subject of the next chapter, and it is where the language stops being a curiosity and starts being useful.