Logo

Programming-Idioms

  • JS
  • Groovy
  • Java
for(int i=0;i<items.length;i++){
	doSomething( items[i] );
}

This syntax is now "old-school" since the foreach construct introduced in Java 5.
T t;
Iterator<T> i = items.listIterator();
while (i.hasNext()) {
    t = i.next();
}

The `ListIterator` allows for backward iteration, and mutation.
import java.util.Iterator;
T t;
Iterator<T> i = items.iterator();
while (i.hasNext()) {
    t = i.next();
}
for (Item x: items) {
    doSomething(x);
}

This syntax is allowed since Java 5.
It works with arrays or collections as well.
Item is the type of the elements.
for (T x : items) {}
items.stream().forEach(item -> doSomething(item));

This syntax uses the streams api introduced in Java 8.
for (var i = 0; i<items.length; i++) {
  var x = items[i];
  doSomething(x);
}
items.forEach((x) => {
    doSomething(x);
});
for (const x of items) {
	doSomething(x);
}
items.forEach(doSomething)

No anonymous function here, we pass a function directly to forEach
items.map(doSomething)

doSomething is a function
items.each { doSomething(it) }

If a closure has only one parameter, it can be omitted and defaults to the name it
for(x in items) doSomething(x)
items.each { x -> doSomething(x) }
for Item of Items loop
   Do_Something (Item);
end loop;

New implementation...