A linocut of a small workshop assembly line: wooden shapes ride a conveyor under a stamping press, a sorting gate sends some down a chute, and the rest drop into a bin on a scale

Episode 2 of my video series on array programming is out. Episode 1 asked why another programming language; this one starts the work, turning common loops into array operations. It runs about five minutes, and it ends with a preview of the next episode and an exercise.

One loop, three jobs

A frame from the episode, One loop, three jobs: a Rust loop over 2, 4, 6, 8 that triples each value and adds the ones greater than 5 to a sum
One loop, three jobs.

The episode starts with a small Rust loop over 2, 4, 6, 8. It triples each value, keeps the ones greater than 5, and adds up what it keeps: 60. Three jobs are tangled together in one loop body, and they are the three kinds that come up again and again: map, filter and reduce. Scalar-style code describes how to visit every element; array-style code describes what happens to the whole collection.

Map

A frame from the episode, Tripled: values is 2 4 6 8, tripled is 3 times values, and tripled is 6 12 18 24
Map: three times values.

The first job is a map. Every input element produces one output element, so the shape stays the same. X_eTaL says it in one line, with no loop and no index:

values := 2 4 6 8
tripled := 3 * values
tripled

which gives 6 12 18 24. (The episode shows each line as X_eTaL draws it, with ← for := and × for *; the lines here are as you type them.)

Filter: a mask, then replicate

A frame from the episode, Filter: a question for every element: 2 4 6 8 compared with greater than 5 gives the mask 0 0 1 1
A question for every element.

The second job is a filter, in two steps. First, ask every element the same question: values > 5 is 0 0 1 1. That is a mask: one means keep, and zero means drop. Then replicate uses the mask to choose, keeping each value as many times as its mask says:

(values > 5) r_eplicate values

gives 6 8. The output is shorter than the input, which is what makes a filter different from a map.

Reduce, and counting with a mask

A frame from the episode, Reduce: reduce given plus, max and min over values gives 20, 8 and 2
Reduce, given plus, max and min.

The third job is a reduce, which combines many elements into one. Given plus, it adds them up; given max or min, it finds the largest or smallest:

'+ r_/ values
'm_ax r_/ values
'm_in r_/ values

gives 20, 8 and 2. Masks and reduce work well together: adding up a mask counts the matches, so '+ r_/ values > 5 is 2, and reduce given and asks whether every value passes, so '& r_/ values > 1 is 1.

Rust can map too

A frame from the episode, Iterator chains: a Rust iterator chain over values that maps times 3, filters greater than 5 and sums, giving 60
Iterator chains.

Modern languages can describe this too. Rust’s iterator chains map, then filter, then sum, and print 60, the same as the loop. The difference is not that Rust cannot map or reduce. In an array language, arrays and their operations are the center of the language.

Three steps, composed

A frame from the episode, Three steps, composed: values, tripled, big as tripled greater than 5 replicate tripled, and reduce given plus over big, 60
The loop's three jobs, one step each.

Then the three together: triple the values, keep the ones greater than 5, add them up.

big := (tripled > 5) r_eplicate tripled
'+ r_/ big

The answer is 60, the same as the loop. Each step is one whole-array operation, and each one has a name.

Coming up, and an exercise

The episode ends with a short preview of the next one: the shape of an array (how long each axis is), its rank (how many axes it has), and its axes (the directions through it, along which reduce can add). Then an exercise to try before it comes out: take a list of temperatures in Fahrenheit, change each one to Celsius, keep the ones below freezing, and count them, all without a loop. Everything in this post can be tried in the browser editor.

Episode 2 is made the same way as episode 1, in X_eTaL-video: every X_eTaL line it shows was run at a pinned version of X_eTaL, and the Rust was compiled and run, before any of it was narrated.

Closing

I am hoping to cover all the language decisions that went into X_eTaL as I demonstrate array concepts, functional programming, metaprogramming, and related tooling.