Lamda in predicates now?

95 views
Skip to first unread message

Eric Winter

unread,
Jan 9, 2012, 11:10:08 AM1/9/12
to guava-discuss
Curious of others thoughts on an idea I had.

I am attracted to the idea even as a throw up a little at the loss of
compiler safety. :)

Likely well beyond the intent of Guava and maybe someone knows where
it exists in other libraries. Of course, in theory I could use Jython
or Groovy.

Predicate<MyClass> predicate = Predicates.lambda(" x: x.name == 'name'
");

as opposed to

Predicate<MyClass> predicate = new Predicate<MyClass>(){
public boolean apply(MyClass x){
return x.getName().equals('name');
}
};

jordi

unread,
Jan 10, 2012, 12:49:57 PM1/10/12
to Eric Winter, guava-discuss
maybe lambdaj [1] can serve your purpose

jordi

Mark Derricutt

unread,
Mar 10, 2012, 1:28:23 AM3/10/12
to Eric Winter, guava-...@googlegroups.com
Just found an old guava post and thought I'd respond :-)

I've actually used a variation of this using the MVEL expression
language [1].

From memory the code was something like:

public class MvelPredicate<T> implements Predicate<T> {
private String eval;
public MvelPredicate(String eval) {
this.eval = eval;
}
public boolean apply(T t) {
return MVEL.eval(eval, t);
}
}

then using:

new MvelPredicate("name = 'name'");

[1] http://mvel.codehaus.org/

kbwelker

unread,
Mar 10, 2012, 7:31:26 PM3/10/12
to guava-discuss
My Funcito library at http://funcito.googlecode.com cannot yet perform
the specific example you gave, and it cannot create arbitrary
lambdas. But it can wrap existing simple methods as lambdas, AND
preserve type-safety. And it works with other FP-like APIs (Functional
Java, and Jedi-core). I am thinking about the ways in which I might
extend the syntax of Funcito to support using operator-like calls to
build more complex lambdas. But I want to do so in a useful manner
which preserves simplicity, brevity, and which would not become uglier
than the standard alternative of anonymous inner class creation. My
own thoughts on this are captured in the Funcito Google group in these
two threads:

http://groups.google.com/group/funcito/t/6012677fff5180e1?hl=en

http://groups.google.com/group/funcito/t/6682899fe6221ab6?hl=en

I would welcome your input on this matter, or any other suggestions
you have.
Reply all
Reply to author
Forward
0 new messages