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.

6 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.

Toko Jual Obat Kuat Alat Bantu Sex 087733776655 said...

Klik Aja Di Bawah Ini
Pemesanan: 087733776655
http://obatkuat-57.com/
Jual Obat Pembesar Penis Vigrx Plus
Alat Bantu Sex Pria Dan Wanita
Rajanya Obat Kuat Sex
Obat Perangsang Wanita, Obat Gairah Wanita
Obat Pembesar Payudara Monyok, Payudara Kencang
Alat Pembesar Payudara Montok
Alat Pembesar Penis
Alat Sex Pria Boneka Moderen
Obat Pengental Sperma
Minyak Pembesar Penis, Obat Pemanjang Penis
Obat Hernia Paling Ampuh
Obat Pelangsing Badan, Melangsingkan Tubuh
Obat Perapet Vagina
Obat Peninggi Badan, Meninggikan Bada
Obat Pembesar Pantat, Pembesar Bokong
Obat Pemerah Bibir, Pemerah Puting
Obat Pemutih Wajah Alami Tensung Cream
Obat Penggemuk Badan
Obat Penghilang Tatto Permanen Herbal Alami
Kondom Antik Pria
Rajanya Obat Kuat Pria
Alat Bantu Sex Wanita Antik
Alat Bantu Sex Pria
Obat Kuat Herbal
Vimax Obat Pembesar Alat Vital
Cara Memutihkan Selangkangan dan Ketiak


Obat Pelangsing Badan Fatloss Jimpness Beauty
Selaput Dara Perawan Buatan


Leo Herbalis said...

Thank you for sharing in this article
I can learn a lot and could also be a reference
I hope to read the next your article updates
Vimax Asli
Ciri-ciri Vimax Asli Canada
Vimax Original
Obat Pembesar Penis Herbal
Agen Vimax Original
Vimax
Vimax Asli Canada
Vimax Asli Canada Di Medan
Vimax Original
Vimax Asli Canada Izon
Vimax Obat Pembesar Penis
Vimax Asli
Agen Vimax Original
Obat Pembesar Penis Herbal
Ciri Vimax Asli Canada
Ciri Vimax Asli Canada
Vagina Getar Goyang Alat Bantu Sex Pria
Vimax
Obat Pembesar Penis NeoSize
Alat Pembesar Penis Terbaru Pro Extender
Celana Pembesar Penis Celana Dalam Vakoou
Vakum Pembesar Penis
Vigrx Plus Obat Pembesar Penis
KLG Pill Pembesar Penis Terbaik
Obat Perangsang Wanita Terbaik Blue Wizard Cair
Obat Perangsang Permen Cinta sex Love
Potenzol Obat Perangsang Wanita Terbaik
Obat Kuat Levitra
Viagra USA Asli Obat Sex
Cialis Obat Sex Pria Dewasa
Vakum Pembesar Payudara Permanen
Selaput Dara Buatan
Penis Getar Goyang Mutiara Alat Bantu Sex

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