Functions with method foreach

Functions with method foreach

Functions are useful to iterate over collections. For example,

var v = [ 1, 2, 3, 4, 5, 6 ];
// sum all elements of vector v
var sum = 0;
v foreach: { (: Int x :) sum = sum + x };

Method foreach: of the array v calls the function (as in “g eval: 5″, in the previous section) for each of the array elements.