Array Languages #22: From Scalar Loops to Array Operations
1096 words • 6 min read • Abstract

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.
| Resource | Link |
|---|---|
| Video | From Scalar Loops to Array Operations: Array Languages #2![]() |
| Episode 1 | Why Another Programming Language? |
| Try X_eTaL | In your browser |
| Code | X_eTaL, X_eTaL-video |
| The series | Array Languages: every post |
| Comments | Discord |
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
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
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
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
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
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.
Part 22 of the Array Languages series. View all parts
Comments or questions? SW Lab Discord or YouTube @SoftwareWrighter.
