sourcelib/Text.xtl
1⍝# Text: printing lines, shared by the games.
2
3⍝ :: Num b => Box a -> b
4⍝# Print each string of a strand on its own line; how many were printed.
5⍝# >> "t:" u_se< "Text"
6⍝# >> t:l_ines "one" "two"
7⍝# one
8⍝# two
9⍝# 2
10ˡl̲ines ← { v →
11 0 = t̲ally v ? 0
12 said ← p̲rint! d̲isclose f̲irst v
13 1 + ˡl̲ines 1 d̲rop v
14}
15⍝ :: Truthy a => Int -> Int -> a
16⍝# Which rows of a text table hold in state s: a row's condition is a
17⍝# state item (0: always) and the value it must have, the second and
18⍝# third rows of kfv (the first row, the keys, is for the caller to
19⍝# match: w_here (keys m_ember? k) & kfv t:h_olds s).
20⍝# >> "t:" u_se< "Text"
21⍝# >> (3 4 r_eshape 1 1 2 2 0 2 0 2 0 1 0 0) t:h_olds 0 1
22⍝# 1 1 1 0
23ˡh̲olds ← { kfv s →
24 f ← 2 s̲elect kfv
25 (f = 0) ∨ (3 s̲elect kfv) = (1 + f) s̲elect -1 c̲at s
26}
27⍝ :: Box Char -> Char
28⍝# The boxed texts b joined into one text, in pairs, level by level: each
29⍝# level copies the text once, so n texts cost n log n, not the n squared
30⍝# of a reduce with c_at (X_eTaL has no built-in join yet: an ask).
31⍝# >> "t:" u_se< "Text"
32⍝# >> t:j_oin "ab" "c" "def"
33⍝# abcdef
34ˡj̲oin ← { b →
35 0 = t̲ally b ? ""
36 1 = t̲ally b ? d̲isclose f̲irst b
37 e ← b c̲at (2 m̲od t̲ally b) r̲eshape e̲nclose ""
38 odd ← (2 × r̲ange (t̲ally e) d̲iv 2) − 1
39 ˡj̲oin (odd s̲elect e) '{ x y → e̲nclose (d̲isclose x) c̲at d̲isclose y } e̲ach (odd + 1) s̲elect e
40}