Friday, December 28, 2007

Advanced Features

Advanced features

A function type can be used as type parameter:

  public static void main(String[] args) {
    List<{ int, int => int }> operations = 
      new ArrayList<{ int, int => int }>();
    operations.add({ int x, int y => x + y });
    operations.add({ int x, int y => x | y });
    int[][] param = { { 1, 2 }, { 3, 4 }, { 5, 6 } };
    for (int[] p: param) {
      for ({ int, int => int } op : operations) {
        System.out.println(op.invoke(p[0], p[1]));
      }
    }
  }
  

A closure can return another closure:

  { String => { int => String } } concat = 
    { String s => 
      { int n => String r = ""; for ( ; n > 0; n--) r += s; r } };
  

Function concat takes a single string argument and returns a function that takes a single integer argument and returns string.

  { int => String } concatABC = concat.invoke("ABC");
  String result = concatABC.invoke(3);
  

Each function that takes multiple arguments can be transformed into a function that takes a single argument. This transformation is called currying. For example, function plus can be transformed into anotherPlus:

  { int, int => int } plus = { int x, int y => x + y };
  { int => { int => int } } anotherPlus = { int x => { int y => x + y } };
  int threePlusFour = anotherPlus.invoke(3).invoke(4);
  

The reverse transformation is called uncurrying. Currying and uncurrying are used in functional programming. For more examples of functional programming in Java, see Luc Duponcheel's blog.

5 comments:

Lawrence Kesteloot said...

You wrote: "Function concat takes a single string argument and returns a function that takes a single string argument and returns string." That second "string" should be "integer".

DedicatedHosting4u said...

I have read your blog and I gathered some needful information from your blog. Keep update your blog. Awaiting for your next update. Thanks
DedicatedHosting4u.com

MindtechAffiliates said...

Thank you for the link building list.I am going jot down this because it will help me a lot.Great blog! Please keep on posting such blog.

Thanks
Cpa offers

periyannan said...


Awesome..You have clearly explained …Its very useful for me to know about new things..Keep on blogging..

python internship | web development internship |internship for mechanical engineering students |mechanical engineering internships |java training in chennai |internship for 1st year engineering students |online internships for cse students |online internship for engineering students |internship for ece students |data science internships

카지노정보 said...

Last year I released a sci-fi Roguelike called Asterogue.토토사이트