-
Notifications
You must be signed in to change notification settings - Fork 0
Currying
Functions can be curried, For example:
let
link "listutils.fn" as list;
fn mul(x, y) {
x * y
}
in
list.map(mul(2), [1, 2, 3, 4]); // [2, 4, 6, 8]
Note that mul is defined with two arguments but called with only one, resulting
in a closure that accepts the second argument and performs the computation. That
closure is what gets handed to map which applies it to each element of the list.
In addition to partial application, you can also pass more arguments than a function’s declared arity. Extra arguments are applied one-by-one to the result of the call, left-to-right, as long as that result is itself a function.
Examples:
let f = fn (x, y) { fn (z) { x + y + z } };
in f(1, 2, 3) // 6
Here f takes two arguments and returns a function. Calling f(1,2,3) applies
(1,2) first, producing a new function, then applies the extra 3 to that result.
You can chain and nest multiple argument functions arbitrarily:
let g = fn (a, b) { fn (c, d) { fn (e) { a + b + c + d + e } } };
in g(1, 2, 3, 4, 5) // 15
Next: Pattern Matching

CEKF(s) a.k.a. F Natural a.k.a F♮