Tuesday, August 19, 2008

Modifier 'for'

Modifier 'for'

Method modifier 'for' enables us to declare and call "loop-like" methods.

  static for void eachEntry(int[] values, { int ==> void } block) {
    for (int p : values) {
      block.invoke(p);
    }
  }
  
  public static void main(String[] args) {
    int[] v = { 2, 3, 5, 7, 11 };
    for eachEntry(int i : v) {
      System.out.println(i);
      if (i > 3) {
        break;
      }
    }
  }
  

For example, we can declare a method that iterates through a map:

  static for <K, V> void eachMapEntry(Map<K, V> dict, 
      { K, V ==> void } block) {
    for (Map.Entry<K, V> entry : dict.entrySet()) {
      block.invoke(entry.getKey(), entry.getValue());
    }
  }
  
  public static void main(String[] args) {
    Map<String, String> dict = new HashMap<String, String>();
    ...
    for eachMapEntry(String k, String v : dict) {
      System.out.println(k + ": " + v);
    }
  }
  

10 comments:

Luc Duponcheel said...

cool post!

Anonymous said...

could you explain : I miss something which is probably so obvious that you forgot to explain to a newbie ... why is the first paramater declared int[] and then used as (int i : v) ?
a clearer descritpion of the rules is needed (I just "suspect" what is happening ..)
thanks

bernard (I can't remember my passwd!)

Zdeněk Troníček said...

Hi Bernard,

this definitely deserves explanation: 'i' in 'for eachEntry(int i : v)' is a formal parameter of the closure which is supplied using the control syntax and 'v' is the first argument of the eachEntry method. The syntax was probably inspired by the enhanced for loop but perhaps the reverse order (for eachEntry(v : int i)) would have been more logical.

Brian said...

If I declare a "for" method which takes no argument except for the closure, I can invoke it as 'for myMethod(Object p:) {...}. Should the colon really be required? The prototype compiler says it is.

Zdeněk Troníček said...

Hi Brian,

probably no. Thanks for the comment.

Dribeiro said...

Extended for (what is more formally called abstract iteration mechanism) is so usefull, and so old (Smalltalk had them back in the 80's).

I made a project called Fluent Javawhich brings down the power of Google Collections, Hamcrest and Fork Join CommonsOps to make such abstract iteration mechanisms easier on java, even though it still lacks real closures.

Audi said...

I can invoke it as 'for myMethod(Object p:) {...}. Should the colon really be required? The prototype compiler says it is. Audi RS5 I'am Audi fanat.

mmo25 said...

At first organized as a PS4 launch name, DriveClub was delayed recently to 2014. For further on the racing game, be sure you study Polygon's interview using design director Henry Rustchynsky about DriveClub's user-generated difficulties and much more.Wonderful AQL announced on this week's matter involving Famitsu publication that they can be releasing an aftermarket edition of PS Vita role-playing game Valhalla Knights Three or more.
FFXIV Gil

FFXIV Gold

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


iklan obatkuat said...

OBAT KUAT RANJANG. COM

Selamat datang di toko online kami,semoga kehadiran kami bisa membantu menyelesaikan masalah rumah tangga anda

Info pemesanan
call : 082133991881
BBm : 323fac0b

KAMI MENJUAL PRODUK -PRODUK DI BAWAH INI

VIMAX CANADA PEMBESAR PENIS

LINTAH PAPUA PEMBESAR PENIS

OBAT KUAT SEK

ALAT-ALAT SEX

PENIS TEMPEL ELEKTRIK

SEX TOYS PRIA DAN WANITA

PENIS MUTIARA GETAR GOYANG MAJU MUNDUR

VAGINA SILIKON MANUAL

VAGINA SENTER 7 SPEED

PENIS NAGA
ELEKTRIK


PERANGSANG WANITA CAIR

PENINGGI BANDAN

KONDOM DURI SILIKON

PEMBESAR PENIS

GALERI KOSMETIK

CELANA HERNIA

CONTAK KAMI :
Hub: 082133991881
Pin bb: 323fac0b





SITUS BOKEP ARTIS

TANTE TANTE KESEPIAN

VIDEO TANTE BUGIL