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