package fun; import java.util.Iterator; /** * Folds a list using a function, associating to the left. */ public class FoldL extends Fun3,B,Iterator,B> { public B apply (Fun2 f, B y, Iterator xs) { return foldl(f, y, xs); } public static Fun2,B> foldl (Fun2 f) { return new FoldL().apply(f); } public static Fun,B> foldl (Fun2 f, B y) { return new FoldL().apply(f, y); } public static B foldl(Fun2 f, B y, Iterator it) { while (it.hasNext()) y = f.apply(y, it.next()); return y; } }