sourceuserlibs/Apply.xtlm

1⍝│ A macro library of your own (userlibs/, as Repeat is): a function 2⍝│ applied to several arguments, written as one call. Every X_eTaL 3⍝│ function takes one argument, so a call with more is a chain of 4⍝│ applied groups, ((a f b) c) d; this macro writes that chain for 5⍝│ you when the program is expanded. Import it with an alias of your 6⍝│ choice, "ap:" u_se< "Apply", and see what a call becomes with 7⍝│ xetal expand FILE. The idiom for a function of more than two 8⍝│ arguments is a tuple (f (a, b, c, d)); this library shows what a 9⍝│ macro can write. 10 11⍝━━ Applying a function to several arguments 12 13⍝│ The function on the left applied to the arguments on the right, 14⍝│ separated by ◆ (a space would make one array of them): the first 15⍝│ is its left argument, the second its right, and each one after that 16⍝│ is passed to the function the call so far gives, as (...) next. 17⍝│ One argument is a plain call. The pieces are copied as written, each 18⍝│ in parentheses. 19⍝│ takes: code code 20⍝│ uses: copied 21⍝│ >> "ap:" u_se< "Apply" 22⍝│ >> u:f_ := { a b c d -> a + (10 * b) + (100 * c) + 1000 * d } 23⍝│ >> c"u:f_" ap:a_pply< c"1; 2; 3; 4" 24⍝│ 4321 25ᵐa̲pply< ← { f args → 26 parts ← '{ ʰt̲rim d̲isclose ⍵ } m̲ap (args ≠ f̲irst ";") p̲artition args 27 0 = t̲ally parts ? "bad-macro-argument right" ⎕R̲EJECT "ap:a_pply< needs at least one argument" 28 1 = t̲ally parts ? f c̲at " (" c̲at (d̲isclose f̲irst parts) c̲at ")" 29 first ← "(" c̲at (d̲isclose f̲irst parts) c̲at ") " c̲at f c̲at " (" c̲at (d̲isclose (2 s̲elect parts)) c̲at ")" 30 first ʰc̲hain 2 d̲rop parts 31} 32 33⍝│ The text t without its leading spaces. 34ʰt̲rim ← { t → ('+ r̲/ '∧ s̲\ t = f̲irst " ") d̲rop t } 35 36⍝│ The call so far applied to each further argument, in order: 37⍝│ (call) (next). 38ʰc̲hain ← { call rest → 39 0 = t̲ally rest ? call 40 ("(" c̲at call c̲at ")_ (" c̲at (d̲isclose f̲irst rest) c̲at ")") ʰc̲hain 1 d̲rop rest 41}