Monday, February 23, 2009

Micro DSL list finder versus Jedi

Toby "blog-a-lot" Weston wrote about a micro DSL style list finder that he is playing around with. His list finder looks like this:

Employee candidate = find(interviewee).in(applicants).using(superStrictCriteria);


where the list finder uses a custom comparator interface (in this case the superStrictCriteria) to determine if a target (interviewee) matches any of the list elements (applicants). I think this DSL is quite nice-but there is another library that does this for you already in a similar fashion-Jedi. Jedi is a functional programming library that allows you to express your ideas in a very computer-sciencey way. Impress your collegues and try it today!

The above example, using Jedi can be done as follows:

Employee candidate = first(select(applicants, superStrictCriteria));


where superStrictCriteria is a Filter interface which essentially boils down to:

public interface Filter <T> {
boolean execute(T value);
}


so in this case could be:

new Filter<employee>() {
public Boolean execute(Employee employee) {
return superStrictCriteria.canBeAnsweredBy(employee);
}
}

(Note: in the actual Jedi code the Filter interface extends Functor-but I left that bit out for brevity)

An advantage that you get with Jedi is that there are already a lot of functional ways to handle lists. Have a look in the jedi.functional.FunctionalPrimitives class. You can mix and match pretty much any of these functional primitives to attain quite complex list handling and criteria selection. And if this is not enough, Jedi gives you Coercions (eg. converting lists and arrays), Comparables (eg. equal, lessThan, min), FirstOrderLogic (eg. exists, all, not) and much much more!

So, although I'm not one for reinventing the wheel (I would normally use Jedi for what Toby describes), I do like to explore new styles of coding-just so long as the substance is there.

No comments:

Post a Comment