sourcedemos/xetal-pipes/xetal-pipes.xtl
1⍝!/usr/bin/env xetal
2⍝# Unix pipes in X_eTaL: the arrays inside the stages (stages/*.xtl),
3⍝# shown on sample.txt. A stage reads standard input as one text;
4⍝# here the same text comes from the file. Every character is
5⍝# numbered by its line, so choosing lines is a mask and reordering
6⍝# them a grade: no loop over lines anywhere. Run it with "xetal run
7⍝# demos/xetal-pipes/xetal-pipes.xtl"; it prints the first line's
8⍝# character-to-line numbers, the counts xetalwc would give, where
9⍝# "lazy" starts and which lines it is on (grep), the padded lines'
10⍝# shape and which lines uniq keeps, and each line's rank and the
11⍝# text sorted by it. No live page: command line only (see the
12⍝# stages, xetalcat and friends, in the demo's README).
13
14ᵖ⁼u̲se< "stages/Pipes.xtl"
15
16t ← ⎕N̲GET "sample.txt"
17
18⍝# -- every character's line: a running sum of newlines. ln: as
19⍝# stages/Pipes.xtl's l:l_ines computes it.
20ln ← ᵖl̲ines t
21
22⍝## grep: the text's k rotations against the needle
23
24⍝# needle, k, tt: the needle to find, its length, and the text
25⍝# repeated k times (so a rotation of it lines up with every start).
26⍝# starts: 1 wherever the needle starts: the k rotations' characters
27⍝# compared with the needle's, then reduced by & (and-reduce). hits:
28⍝# the lines those characters are on.
29needle ← "lazy"
30k ← t̲ally needle
31tt ← t c̲at k r̲eshape "\n"
32starts ← (t̲ally t) t̲ake '∧ r̲/₁ ((o̲ffsets k) o̲- tt) = needle 'l̲eft t̲able tt
33hits ← u̲nique starts r̲eplicate ln
34
35⍝## uniq: a line stays when its padded row differs from the row above
36
37⍝# m: the lines as a padded character matrix (Pipes.xtl's l:m_atrix).
38⍝# differs: 1 where a row differs from the one above it (the matrix
39⍝# rotated down one row).
40m ← ᵖm̲atrix t
41differs ← 1 c̲at 1 d̲rop '∨ r̲/₂ m ≠ -1 o̲-₁ m
42
43⍝## sort: each line's rank, given to its characters, graded stably
44
45⍝# rank: the grade of the padded rows, each line's place in order.
46⍝# sorted: every character takes its line's rank, and a stable grade
47⍝# of those ranks moves whole lines.
48rank ← g̲rade g̲rade m
49sorted ← (g̲rade ln s̲elect rank) s̲elect t
50⍝ -- end of the core --------------------------------
51
52⍝ The first line's characters and their line numbers.
5320 t̲ake ln
54⍝ Lines, words, characters (as xetalwc counts them).
55(ᵖc̲ount t) c̲at t̲ally t
56⍝ Where "lazy" starts (1 at those characters), and the lines it is on.
57w̲here starts
58hits
59⍝ The padded lines' shape, and which lines uniq keeps.
60s̲hape m
61differs
62⍝ Each line's rank in byte order, and the sorted text.
63rank
64-1 d̲rop sorted