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.

11 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".

Zdeněk Troníček said...

Thanks.

Radley Co Tad said...

Very much useful article. Kindly keep blogging

Java Training in Chennai

Java Online Training India

Jual Hammer Of Thor di Medan said...

Jual Vimax Asli di Medan
Jual Hammer Of Thor Asli di Medan
Jual Titan Gel Asli di Medan

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

Logical softTech said...

Your post is very good. I got to learn a lot from your post. Thank you for sharing your article for us. it is amazing post
what is seo
types of seo

Logical softTech said...


This is a fantastic idea! I like it a lot because it's super easy for the audience to see the value of opting in. wonderful and amazing post very use full your post thanks for sharing your article
Android Application development
Web application

klinik aborsi said...

Jual Obat Aborsi Asli | Obat Cytotec Asli | 082241083319
jual obat aborsi pills cytotec asli manjur untuk menggugurkan kandungan usia 1 - 6 bulan gugur tuntas.CYTOTEC OBAT ASLI sangat efektif mengatasi TELAT DATANG BULANdan menjadikan anda gagal hamil , CYTOTEC adalah OBAT ABORSI 3 JAM BERHASIL GUGUR TUNTAS dengan kwalitas terbaik produk asli pfizer nomor 1 di dunia medis
JUAL OBAT ABORSI DI JEMBER
JUAL OBAT ABORSI DI JOMBANG
JUAL OBAT ABORSI DI KEDIRI
JUAL OBAT ABORSI DI NGASEM
JUAL OBAT ABORSI DI LAMONGAN
JUAL OBAT ABORSI DI LUMAJANG
JUAL OBAT ABORSI DI MADIUN

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.토토사이트