To support query expressions on collections, it is necessary to pass EL expressions as argument to methods. These expressions will be evaluated in the methods. So essentially we need closures in EL. Fortunately, EL already has the concept of deferred expressions, so we just need to put the pieces together to add lambda expressions to EL.
An EL lambda expression is a deferred expression with parameters.
The proposed syntax follows JDK 8 see jsr 335 edr, though much simplified, since there is no type declarations in EL.
Grammar:
Lambda := LambdaParameters '->' Expression
LambdaParameters := identifier | '(' Parameters ')'
Parameters := (identifier (',' identifier)*)?
Example:
()-> No parameter, void result ()->42 No parameter (x) -> x+1 One parameter x -> x+1 Parenthesis optional for one parameter (x,y) -> x+y Two parameters
fn = x -> x+1
fn(100) yields 101
((x, y) -> x+y)(3,4) yields 7 Note the use of parenthesis to disambiguate
A lambda expression can also be invoked in Java with javax.el.LambdaExpression.invoke, described below.
employees.where(p->p.name.first == 'Larry')