Inline Lambda (Expression) — skLambda
Addon: skLambda · Category: Expression · Since: 0.0.3-alpha
Syntax
lambda[ ]<.+>Description
Creates a lambda on a single line, usable anywhere an expression is. The body after `:` is one of: - a condition: the lambda returns whether it holds (a predicate, see `passes`), - an effect: the lambda runs it and returns nothing, or - a value: `return <expression>` (or just a bare expression) makes the lambda return that value.
A parameter may declare a default with `name: type = value`, used when the caller leaves that (trailing) argument off, e.g. `lambda (n: number = 1) -> number:`.
Parameters become locals (`{_p}`) inside the body. The lambda also closes over the local variables in scope where it is written, a snapshot taken when the expression is evaluated, readable inside the body when the lambda is later called (parameters shadow captured locals of the same name). For multi-line bodies, use the `set %object% to lambda ...:` section form instead.
Examples
set {is-op} to lambda (p: player): {_p} is op
add lambda (n: number): {_n} > 0 to {positive-checks::*}
run lambda (p: player): send "hi" with player
set {_add} to lambda (a: number, b: number): return {_a} + {_b}
set {_double} to lambda (n: number): {_n} * 2