fold
Description:
Standard aggregation function.
It takes a function, seed and first element, returns the new seed and progress to next element when the operation repeats.
Note:
Default implementation uses foreach.
Note:
In Iterator implementation operation moves the iterator to last element in iteration. If iterator is Iterator.valid the current element will be considered as well.
Example: Find the length of the longest string in a set:
uint max_length (Gee.Traversable<string> l) {
return l.fold<uint> ((s, maxlen) => {
return uint.max (s.length, maxlen);
}, 0U);
}
public void main () {
var ts = new Gee.TreeSet<string> ();
ts.add ("helo");
ts.add ("goodbyte");
ts.add ("every");
ts.add ("day");
foreach (string s in ts) {
print ("%s\n", s);
}
print ("longest string is of length: %u\n", max_length (ts));
}
valac --pkg gee-0.8 Gee.Traversable.fold.vala