[rules-dev] Functional programming in Drools: Brackets and Commas

Toni Rikkola toni.rikkola at gmail.com
Thu Oct 20 07:15:19 EDT 2011


I'm just asking if we can drop the brackets and maybe even the commas to get a cleaner look.

The goal is to pick 10 students who have the best score. We do not want to give out who is the best, so the student list is ordered by student names. I'm sure that there are some syntax errors, but hopefully I got most of it right.

function String name( Student student ) => student.name;
function String score( Student student ) => student.score;

function List<T> orderBy( Comparable => String field, List<T> list  ){
    orderBy( ( first, second ) => field( first ).compareTo( field( second ) ), list );
}

function List<T> orderBy( (Comparable, Comparable) => int comparator, List<Comparable> list ){
   ## sort ##
}

function (int, List<T>) => List<T> top( int number, List<T> list ) => list.subList( 0, number );


Student( ) 
    |> collect
    |> top( 10, orderBy( score ) )  
    |> orderBy( name )

vs.

Student( ) 
    |> collect
    |> top 10 orderBy score   
    |> orderBy name 

Another example. A function returning a function. Let' s take the bottom 3.

function (int, List<T>) => List<T> bottom(){
    (number, list) => list.sublist( number, list.size() -1 );
}

Student( ) 
    |> collect
    |> orderBy( score )  
    |> bottom()( 3 )

vs.

Student( ) 
    |> collect
    |> orderBy score  
    |> bottom 3

And of course you could have a function returning a function returning a function and so on and you get doSomething()(1)("Toni",29)()("more text for testing").

Toni

On Oct 19, 2011, at 11:59 PM, Mario Fusco wrote:

> Hi all,
> 
> as anticipated by Mark, I put down some ideas on how we could start introducing some functional programming features in the DRL.
> 
> http://community.jboss.org/wiki/FunctionalProgrammingInDrools
> 
> It's needless to say that the document has to been considered just a draft in its very first stage and any feedback or suggestion to improve or clarify it is welcome.
> 
> Mario
> _______________________________________________
> rules-dev mailing list
> rules-dev at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-dev

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-dev/attachments/20111020/21b3c953/attachment-0001.html 


More information about the rules-dev mailing list