sourcedemos/higher-order.xtl
1⍝!/usr/bin/env xetal
2⍝# Higher-order built-ins: they take quoted functions as operands, as
3⍝# '+ r̲/ v reduces v with plus. Run it with
4⍝# "xetal run demos/higher-order.xtl" (or "./demos/higher-order.xtl");
5⍝# it prints the result of each line: reductions, a scan, an outer and
6⍝# an inner product, e_ach, c_ompose and s_wap, then the search and
7⍝# order built-ins s_ort, g_rade, u_nique, i_ndexOf and w_here.
8
9⍝## Reduce and scan
10
11⍝# v: the vector the reductions, scan and search built-ins work on.
12v ← 3 1 4 1 5
13'+ r̲/ v
14'− r̲/ 1 2 3
15'+ s̲\ v
16
17⍝## Matrices: reduce, outer and inner products
18
19⍝# m: a 2 by 3 matrix holding 1 to 6, row by row.
20m ← 2 3 r̲eshape r̲ange 6
21'+ r̲/ m
221 2 3 '× t̲able 1 2 3
23m '+ '× i̲nner 1 1 1
24
25⍝## Functions as operands
26
27⍝# u:s_ign x: -1 when x is negative, 0 when it is zero, 1 otherwise.
28ᵘs̲ign ← { x → x < 0 ? -1◆ x = 0 ? 0◆ 1 }
29'ᵘs̲ign e̲ach -2 0 7
301 2 3 '= e̲ach 1 5 3
31'n̲eg 'a̲bs c̲ompose -3 4
322 '÷ s̲wap 1
33
34⍝## Search and order
35
36s̲ort v
37g̲rade v
38u̲nique v
39v i̲ndexOf 4 9
40w̲here v > 2