2. Purpose S eminar gives an overview of Functional Programming methods and its applications in Java for coding Business Logic and its customization
3.
4. FP Overview Computation is executing statements to change the program state. Imperative programming Functional programming Computation is evaluation of expressions The focus is on what , not how Expressions are formed by using functions to combine basic values Program consists of a sequence of commands.
5.
6. Sandwich algorithm If we want to use sausage instead of cheese ? Let’s pass sausage/cheese as input parameter No problems!
7.
8. Sandwich algorithm If we want to put butter instead of spreading ? Imperative programming: Problem! Functional programming: not a problem
21. Business logic with FP GroomFilter List suitableGrooms = new ArrayList(); for (groom in allGrooms) { if ( minAge > -1 && groom.getAge() < minAge ) continue; if (maxAge > -1 && groom.getAge() > maxAge) continue; suitableGrooms .add(groom); } return suitableGrooms ; List filterGrooms(List allGrooms , int minAge, int maxAge) If age is -1 then Don’t check age
22. Business logic with FP GroomFilter List suitableGrooms = new ArrayList(); for (groom in allGrooms) { if ( groomChecker .accept(groom)) suitableGrooms.add(groom); } return suitableGrooms; List filterGrooms(List allGrooms, Filter groomChecker ) Pass function as parameter
23. Business logic with FP public interface Filter { /** * Method defines whether given object is accepted. * @param obj any Object * @return true iff object is accepted */ boolean accept (Object obj); }
24. Business logic with FP public interface Filter { boolean accept (Object obj); public static final Filter ACCEPT = new Filter() { public boolean accept(Object obj){ return true; } }; public static final Filter NOT_NULL = new Filter() { public boolean accept(Object obj){ return obj!=null; } }; public static final Filter NEGATE ..; public static final Filter IS_NULL = …; } Predefined values
25. Business logic with FP Client 1 List suitableGrooms grooms = GroomFilter.filterGrooms(…, new Filter() { public boolean accept(Object obj) { return ((Groom) obj).getAge() > 23; } } ); Client 2 List suitableGrooms = GroomFilter.filterGrooms(…, Filter.ACCEPT ); Closure – object representing a function Anonymous classes are often used as closures
27. Parameterized Closures StringFilter public class StringFilter implements Filter { public static startsWith (final String prefix ) { return new Filter { public boolean accept (Object o){ return ((String) o). startsWith (prefix); } }; } public static endsWith (final String postfix ) {…} public static contains (final String substring ) {…} public static matches (final String regexp ) {…} };
28. Composition of functions Composition of functions: AND public class AND implements Filter { public AND (Filter filter1, Filter filter2) { this.filter1 = filter1; this.filter2 = filter2; } public boolean accept (Object obj) { return filter1.accept (obj) && filter2.accept (obj); } };
29. FP Applications: Filters FilteredIterator public class FilteredIterator implements Iterator { public FilteredIterator ( Iterator iterator , Filter filter ); } CollectionsUtils static List collectList ( Iterator it ); static Set collectSet ( Iterator it ); static List filterList ( List original , Filter filter ); static Set filterSet ( Set originalSet , Filter filter );
30. FP Applications: Filters Given: a list of all grooms’ names. Goal: find all names with prefix “Mr.” List gentlemen = new LinkedList(); for (Iterator it = groomsNames .iterator(); it.hasNext(); ) { String name = (String) it.next(); if (name != null && name.startsWith(“Mr.”)) { gentlemen .add(name); } } return gentlemen ; Imperative
31. FP Applications: Filters Functional return CollectionsUtils . filterList( allGrooms, StringFilter.startsWith( “Mr.” ) ) ; Given: a list of all grooms’ names. Goal: find all names with prefix “Mr.”
32. FP Applications: Transformers Transformer public interface Transformer { Object transform ( Object sourceObject ); } ListTransformer public class ListTransformer { public List transform ( List sourceList , Transformer transformer ); }
33. FP Applications: Transformers Given: list of Grooms Goal: create list grooms’ names List groomsNames = new ArrayList(); for (Iterator it = allGrooms .iterator(); it.hasNext(); ) { Groom groom = (Groom) it.next(); groomsNames .add(groom.getName()); } return groomsNames ; Imperative
34. FP Applications: Transformers return ListTransformer. transform( allGrooms , new Transformer () { public Object transform(Object obj) { return ((Groom) obj).getName(); } } ) ; Functional Given: list of Grooms Goal: create list grooms’ names
35. Business Logic customization Example using Plexus container import org.codehaus.plexus.embed.Embedder; public List findSuitableGrooms(Client woman) { Filter clientGroomFilter = ( Filter ) embedder.lookup ( “ groomFilter” , woman.getName() ); return GroomFilter.filterGrooms( allGrooms, clientGroomFilter ); }
38. FP Libraries for Java Commons Functors : Function Objects for Java http://jakarta.apache.org/commons/sandbox/functor JGA: Generic Algorithms for Java http:// jga.sourceforge.net http://plexus.codehaus.org
39. Articles Functional programming in the Java language http ://www-128.ibm.com/developerworks/library-combined/j-fp.html Use recursion effectively in XSL http://www-128.ibm.com/developerworks/xml/library/x-xslrecur Why Functional Programming Matters http:// www.math.chalmers.se/~rjmh/Papers/whyfp.html Introduction to Haskell http:// www.haskell.org/tutorial/