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);
    }
  }
  

8 comments:

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.

DanielRibeiro 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

Plumbing & HVAC Services San Diego said...

Compre documentos en línea, documentos originales y registrados.
Acerca de Permisodeespana, algunos dicen que somos los solucionadores de problemas, mientras que otros se refieren a nosotros como vendedores de soluciones. Contamos con cientos de clientes satisfechos a nivel mundial. Hacemos documentos falsos autorizados y aprobados como Permiso de Residencia Español, DNI, Pasaporte Español y Licencia de Conducir Española. Somos los fabricantes y proveedores de primer nivel de estos documentos, reconocidos a nivel mundial.

Comprar permiso de residencia,
permiso de residenciareal y falso en línea,
Compre licencia de conducir en línea,
Compre una licencia de conducir española falsa en línea,
Comprar tarjeta de identificación,
Licencia de conducir real y falsa,
Compre pasaporte real en línea,

Visit Here fpr more information. :- https://permisodeespana.com/licencia-de-conducir-espanola/
Address: 56 Guild Street, London, EC4A 3WU (UK)
Email: contact@permisodeespana.com
WhatsApp: +443455280186