Tasks & lambdas
A task is a function: parameters, a body, and give to return a value.
-- Doc example: tasks, default args, named args, recursion, lambdas.
intent: "doc example: tasks and lambdas"
task add(a, b)
give a + b
task greet(name, greeting = "Hello")
give greeting + ", " + name + "!"
task factorial(n)
when n <= 1
give 1
otherwise
give n * factorial(n - 1)
print(greet("Ada") + " factorial(5) = " + text(factorial(5))) -- run shows the result
test "call, default arg, named arg"
assert_eq(add(2, 3), 5)
assert_eq(greet("Ada"), "Hello, Ada!")
assert_eq(greet("Ada", "Hi"), "Hi, Ada!")
assert_eq(greet("Ada", greeting = "Hey"), "Hey, Ada!")
test "recursion"
assert_eq(factorial(5), 120)
test "lambdas are values, passed to higher-order tasks"
let triple be (n) => n * 3
assert_eq(triple(4), 12)
assert_eq(apply(triple, [1, 2, 3]), [3, 6, 9])
Default & named arguments
task greet(name, greeting = "Hello")
give greeting + ", " + name + "!"
greet("Ada") -- uses the default
greet("Ada", "Hi") -- positional
greet("Ada", greeting = "Hey") -- named (any order)
The default value (= expr) is evaluated at call time. Note = here is the argument form — general assignment is let/set, and equality is ==.
Recursion
A task may call itself (factorial above). There is no special keyword — just give.
Lambdas
A lambda is a value: (params) => expr. Store it, pass it, or hand it to a higher-order op:
let triple be (n) => n * 3
apply(triple, [1, 2, 3]) -- [3, 6, 9]
where(items, (x) => x.active) -- inline predicate