Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

functional programming in the java language

Name: Anonymous 2007-06-08 11:04 ID:zBdqBvbC

http://www.ibm.com/developerworks/library/j-fp.html

now, this is where the fun bit shows up:


Listing 1. A closure in disguise


Runnable worker = new Runnable()
{
  public void run()
  {
    parseData();
  }
 };


The method parseData is literally enclosed (hence the name "closure")

Name: Anonymous 2007-06-09 4:57 ID:qgDunm6s

package com.infosys.setl.fp;
import org.apache.commons.functor.*;
import org.apache.commons.functor.core.composite.*;
import org.apache.commons.functor.adapter.*;
import org.apache.commons.functor.UnaryFunction;
import org.apache.commons.functor.core.Constant;
import org.apache.commons.functor.core.comparator.Min;
public class TestC
{
  public static void main(String[] args)
  {
    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);
  }
}


I loled

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List