sourcedemos/apply.xtl
1⍝!/usr/bin/env xetal
2⍝│ Calling a function of several arguments, three ways
3⍝│ (docs/several-arguments.md). Every X_eTaL function takes one
4⍝│ argument: x f y is (f x) y, so a third argument goes to the
5⍝│ function that call gives, as an applied group (...) z.
6⍝│
7⍝│ Run it with ./demos/apply.xtl or "xetal run demos/apply.xtl"; each
8⍝│ way prints 4321. "xetal expand demos/apply.xtl" shows what the
9⍝│ macro of the second way wrote.
10
11⍝│ a b c d u:f_: the digits a, b, c and d as one number, a the ones.
12ᵘf̲ ← { a b c d → a + (10 × b) + (100 × c) + 1000 × d }
13
14⍝ 1. Written out: one applied group per argument after the second.
15((1 ᵘf̲ 2) 3) 4
16
17⍝ 2. A macro of your own writes that chain (userlibs/Apply.xtlm; in
18⍝ the live demo, Apply.xtlm among your files): the function on the
19⍝ left, the arguments on the right separated by ◆.
20"ap:" u̲se< "Apply"
21c"ᵘf̲" ᵃᵖa̲pply< c"1◆ 2◆ 3◆ 4"
22c"ᵘf̲" ᵃᵖa̲pply< c"2 − 1◆ 2◆ 3◆ 4" ⍝ any expression is an argument
23
24⍝ 3. The idiom for three or more arguments: one tuple, no nesting.
25⍝│ u:g_ (a, b, c, d): the same number, its four digits as one tuple.
26ᵘg̲ ← { (a, b, c, d) → a + (10 × b) + (100 × c) + 1000 × d }
27ᵘg̲ (1, 2, 3, 4)
28
29⍝ Written out, a call can stop early: a partial application, here the
30⍝ function still waiting for d (a tuple comes whole and cannot).
31⍝│ u:f_irstThree d: 321 plus 1000 d.
32ᵘf̲irstThree ← (1 ᵘf̲ 2) 3
33ᵘf̲irstThree 4