16 Jul 2004 08:03
Re: DeveloperWorks Article: Use closures and higher order functions to write modular Java code
I blogged about this yesterday to show how verbose doing functors & closures in Java is compared to other languages... http://tinyurl.com/3rmeu On 16 Jul 2004, at 04:45, Gerald Bauer wrote: > Hello, > > Allow me to highlight the IBM developerWorks article > by Abhijit Belapurkar titled "Functional programming > in the Java language - Use closures and higher order > functions to write modular Java code". > > Abhijit writes: > > This article will introduce you to the basic > characteristics of functional programming, but will > focus on two elements that are especially applicable > to the Java development framework: closures and higher > order functions. If you've ever worked with an agile > development language such as Python, Ruby, or Groovy, > then you've probably already encountered these > elements. Here, you'll see what happens when you apply > them directly within a Java development framework. > I'll start with a short, conceptual overview of > functional programming and its core elements, and then > use common programming scenarios to demonstrate the > benefits that a structured approach to using closures > and higher order functions can bring to your Java > code. > > Full story <at> > http://www-106.ibm.com/developerworks/java/library/j-fp.html > > > Anyone wants to clean up any of the plain old Java > coding samples using closures and see how much simpler > it all looks in Groovy? > > How about this business rule sample using closures > in plain old Java? > > double discountRate = 0.1; > double taxRate=0.33; > double maxDiscount = 30; > SETLItem item = new SETLItem(); > item.setPrice(350); > UnaryFunction calcDiscount = > new RightBoundFunction(new Multiply(), new > Double(discountRate)); > Constant cap = new Constant(new Double(maxDiscount)); > BinaryFunction calcActualDiscount = > new UnaryCompositeBinaryFunction (new Min(), > calcDiscount, cap); > BinaryFunctionUnaryFunction calcActualDiscountAsUnary > = > new > BinaryFunctionUnaryFunction(calcActualDiscount); > BinaryFunction calcDiscountedPrice = > new UnaryCompositeBinaryFunction (new Subtract(), > new Identity(), calcActualDiscountAsUnary); > BinaryFunctionUnaryFunction > calcDiscountedPriceAsUnary = > new > BinaryFunctionUnaryFunction(calcDiscountedPrice); > UnaryFunction calcTax = > new RightBoundFunction(new Multiply(), new > Double(1+taxRate)); > CompositeUnaryFunction calcNetPrice = > new CompositeUnaryFunction(calcTax, > calcDiscountedPriceAsUnary); > Double netPrice = (Double)calcNetPrice.evaluate(new > Double(item.getPrice())); > System.out.println("The net price is: " + netPrice); > > Any takers? > > - Gerald > > ------------------- > Gerald Bauer > > XUL Alliance | http://xul.sourceforge.net > United XAML | http://xaml.sourceforge.net > > Interested in hiring Gerald Bauer? Yes, I'm available. > If you know of an opportunity, please contact me today. > > ______________________________________________________________________ > Post your free ad now! http://personals.yahoo.ca > > James ------- http://radio.weblogs.com/0112098/
RSS Feed