macro libraryuserlibs/Apply.xtlm

A macro library of your own (userlibs/, as Repeat is): a function applied to several arguments, written as one call. Every X_eTaL function takes one argument, so a call with more is a chain of applied groups, ((a f b) c) d; this macro writes that chain for you when the program is expanded. Import it with an alias of your choice, "ap:" u_se< "Apply", and see what a call becomes with xetal expand FILE. The idiom for a function of more than two arguments is a tuple (f (a, b, c, d)); this library shows what a macro can write.

source

Applying a function to several arguments

ᵐa̲pply< : Char -> Char -> Char

macro · line 25

The function on the left applied to the arguments on the right, separated by ◆ (a space would make one array of them): the first is its left argument, the second its right, and each one after that is passed to the function the call so far gives, as (...) next. One argument is a plain call. The pieces are copied as written, each in parentheses.

      "ap:" u̲se< "Apply"
      ᵘf̲ ← { a b c d → a + (10 × b) + (100 × c) + 1000 × d }
      c"ᵘf̲" ᵃᵖa̲pply< c"1◆ 2◆ 3◆ 4"
4321
ᵐa̲pply< ← { f args →
  parts ← '{ ʰt̲rim d̲isclose ⍵ } m̲ap (args ≠ f̲irst ";") p̲artition args
  0 = t̲ally parts ? "bad-macro-argument right" ⎕R̲EJECT "ap:a_pply< needs at least one argument"
  1 = t̲ally parts ? f c̲at " (" c̲at (d̲isclose f̲irst parts) c̲at ")"
  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 ")"
  first ʰc̲hain 2 d̲rop parts
}

ʰt̲rim : Char -> Char

function (private) · line 34

The text t without its leading spaces.

ʰt̲rim ← { t → ('+ r̲/ '∧ s̲\ t = f̲irst " ") d̲rop t }
Used in: ᵐa̲pply<

ʰc̲hain : Char -> Box Char -> Char

function (private) · line 38

The call so far applied to each further argument, in order: (call) (next).

ʰc̲hain ← { call rest →
  0 = t̲ally rest ? call
  ("(" c̲at call c̲at ")_ (" c̲at (d̲isclose f̲irst rest) c̲at ")") ʰc̲hain 1 d̲rop rest
}