programdemos/xetal-pipes/xetal-pipes.xtl

Unix pipes in X_eTaL: the arrays inside the stages (stages/*.xtl), shown on sample.txt. A stage reads standard input as one text; here the same text comes from the file. Every character is numbered by its line, so choosing lines is a mask and reordering them a grade: no loop over lines anywhere. Run it with "xetal run demos/xetal-pipes/xetal-pipes.xtl"; it prints the first line's character-to-line numbers, the counts xetalwc would give, where "lazy" starts and which lines it is on (grep), the padded lines' shape and which lines uniq keeps, and each line's rank and the text sorted by it. No live page: command line only (see the stages, xetalcat and friends, in the demo's README).

source · imports p: demos/xetal-pipes/stages/Pipes.xtl

t : Char

value · line 16
t ← ⎕N̲GET "sample.txt"

ln : Int

value · line 20

-- every character's line: a running sum of newlines. ln: as stages/Pipes.xtl's l:l_ines computes it.

ln ← ᵖl̲ines t

grep: the text's k rotations against the needle

needle : Char

value · line 29

needle, k, tt: the needle to find, its length, and the text repeated k times (so a rotation of it lines up with every start). starts: 1 wherever the needle starts: the k rotations' characters compared with the needle's, then reduced by & (and-reduce). hits: the lines those characters are on.

needle ← "lazy"
Used in: k, starts

k : Int

value · line 30
k ← t̲ally needle
Used in: tt, starts

tt : Char

value · line 31
tt ← t c̲at k r̲eshape "\n"
Used in: starts

starts : Bool

value · line 32
starts ← (t̲ally t) t̲ake '∧ r̲/₁ ((o̲ffsets k) o̲- tt) = needle 'l̲eft t̲able tt

hits : Int

value · line 33
hits ← u̲nique starts r̲eplicate ln

uniq: a line stays when its padded row differs from the row above

m : Char

value · line 40

m: the lines as a padded character matrix (Pipes.xtl's l:m_atrix). differs: 1 where a row differs from the one above it (the matrix rotated down one row).

m ← ᵖm̲atrix t

differs : Int

value · line 41
differs ← 1 c̲at 1 d̲rop '∨ r̲/₂ m ≠ -1 o̲-₁ m

sort: each line's rank, given to its characters, graded stably

rank : Int

value · line 48

rank: the grade of the padded rows, each line's place in order. sorted: every character takes its line's rank, and a stable grade of those ranks moves whole lines.

rank ← g̲rade g̲rade m

sorted : Char

value · line 49
sorted ← (g̲rade ln s̲elect rank) s̲elect t