Syntax
Closures
Inline functions as first-class values
A closure is an anonymous function value. The syntax uses a backslash, an optional parameter list, an arrow, and an expression body.
Zero arguments
\-> 1
One argument
\ s -> s.length
Multiple arguments
\ x, y -> x == y
Block-body closures
The arrow can also be followed by a brace-delimited statement block instead of a single expression:
\ x -> {
y = x + 1
print(y)
y
}
The block body runs its statements top-to-bottom. The value of the closure is the value of the last expression statement.
Closures as arguments
Closures can be passed to methods that expect functions:
['a', 'ab', 'abc'].map(\ s -> s.length)
Returns [1, 2, 3].
Closures in maps
Map values can be closures:
x = {foo = \-> "bar"}
print(x.foo())
print(x[:foo]())
Both print bar.
Interop with java.util.function
Notch closures convert to JVM functional interfaces:
(\ x, y -> x == y).toBiFunction()
(\ x, y -> x == y).toBiPredicate()
The returned objects implement BiFunction and BiPredicate respectively.
See also
- JVM Overview for calling JVM methods that take functional interfaces.