Java 7에 포함되기로 했다가 취소된 함수형 프로그래밍(closure) 미리 맛보기

    관련 자료
        [1] http://javac.info/
        [2] http://stronglytypedblog.blogspot.com/2010/06/lambdas-in-java-preview-part-1-basics.html )
        [3] http://www.infoq.com/articles/lambdas-java-analysis
        [4] http://wiki.java.net/bin/view/JDK/ClosuresSyntaxInJava7
        [5] http://www.ibm.com/developerworks/java/library/j-jtp04247.html
        [6] http://www.java.net/pub/pq/242
        [7] http://www.baptiste-wicht.com/2010/05/oracle-pushes-a-first-version-of-closures/
        [8] http://www.davidflanagan.com/2010/05/closures-in-jav.html


1. FuctionalJava 홈페이지
   http://functionaljava.org/examples/

2. 다운로드
    http://functionaljava.org/download/


3. 예제

JDK 1.5 이상 용 예제 파일 (Java 7 용은 별도로 있음)

// 소스 파일: Fibs.java

package concurrent;

import static fj.Bottom.error;
import fj.Effect;
import fj.F;
import fj.P;
import fj.P2;
import fj.Unit;
import fj.data.List;
import fj.control.parallel.Actor;
import fj.control.parallel.Promise;
import fj.control.parallel.Strategy;
import static fj.data.List.range;
import static fj.function.Integers.add;
import static fj.control.parallel.Promise.join;
import static fj.control.parallel.Promise.promise;
import static fj.control.parallel.Actor.actor;

import java.text.MessageFormat;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Parallel Fibonacci numbers.
 * Based on a Haskell example by Don Stewart.
 * Author: Runar
 */
public class Fibs
  {private static final int CUTOFF = 35;

   public static void main(final String[] args)
     {if (args.length < 1)
         throw error("This program takes an argument: number_of_threads");

      final int threads = Integer.parseInt(args[0]);
      final ExecutorService pool = Executors.newFixedThreadPool(threads);
      final Strategy<Unit> su = Strategy.executorStrategy(pool);
      final Strategy<Promise<Integer>> spi = Strategy.executorStrategy(pool);

      // This actor performs output and detects the termination condition.
      final Actor<List<Integer>> out = actor(su, new Effect<List<Integer>>()
        {public void e(final List<Integer> fs)
          {for (final P2<Integer, Integer> p : fs.zipIndex())
               {System.out.println(MessageFormat.format("n={0} => {1}", p._2(), p._1()));}
           pool.shutdown();}});

      // A parallel recursive Fibonacci function
      final F<Integer, Promise<Integer>> fib = new F<Integer, Promise<Integer>>()
        {public Promise<Integer> f(final Integer n)
          {return n < CUTOFF ? promise(su, P.p(seqFib(n))) : f(n - 1).bind(f(n - 2), add);}};

      System.out.println("Calculating Fibonacci sequence in parallel...");
      join(su, spi.parMap(fib, range(0, 46)).map(Promise.<Integer>sequence(su))).to(out);}

  // The sequential version of the recursive Fibonacci function
  public static int seqFib(final int n)
    {return n < 2 ? n : seqFib(n - 1) + seqFib(n - 2);}}



4. 컴파일하기

    javac -d . -cp functionaljava.jar Fibs.java


5. 실행하기 (옵션 3은 쓰레드 개수)

    java -classpath .;functionaljava.jar concurrent.Fibs 3

실행 결과:
ibs 3
Calculating Fibonacci sequence in parallel...
n=0 => 0
n=1 => 1
n=2 => 1
n=3 => 2
n=4 => 3
n=5 => 5
n=6 => 8
n=7 => 13
n=8 => 21
n=9 => 34
n=10 => 55
n=11 => 89
n=12 => 144
n=13 => 233
n=14 => 377
n=15 => 610
n=16 => 987
n=17 => 1,597
n=18 => 2,584
n=19 => 4,181
n=20 => 6,765
n=21 => 10,946
n=22 => 17,711
n=23 => 28,657
n=24 => 46,368
n=25 => 75,025
n=26 => 121,393
n=27 => 196,418
n=28 => 317,811
n=29 => 514,229
n=30 => 832,040
n=31 => 1,346,269
n=32 => 2,178,309
n=33 => 3,524,578
n=34 => 5,702,887
n=35 => 9,227,465
n=36 => 14,930,352
n=37 => 24,157,817
n=38 => 39,088,169
n=39 => 63,245,986
n=40 => 102,334,155
n=41 => 165,580,141
n=42 => 267,914,296
n=43 => 433,494,437
n=44 => 701,408,733
n=45 => 1,134,903,170



Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
Posted by Scripter
,