package fun; import java.util.Iterator; import tuple.Pair; /** * Abstract class for binary functions. Binary functions are seen * as unary functions that return a unary function. */ public abstract class Fun2 extends Fun> { /** * Does partial application of this function. * f.apply(x).apply(y) is equivalent * to f.apply2(x, y). * * @param x The value to apply the function to * @return A unary function */ public Fun apply (final A x) { return new Fun() { public C apply (B y) { return Fun2.this.apply(x, y); } }; } /** * Should be implemented by subclasses. * * @param x The first parameter. * @param y The second parameter. */ public abstract C apply (A x, B y); /** * Creates a funtion that does the same thing as this function * but takes the arguments in reverse order. * * @return A function that applies this function to its arguments * in reverse order. */ public Fun2 flip () { return Flip.flip(this); } /** * Turns this function into a unary function that takes a * pair argument. * f.uncurry().apply(new Pair(x, y)) * is equivalent to f.apply(x, y). * * @return A unary function that takes a pair argument. */ public Fun,C> uncurry () { return Uncurry.uncurry(this); } public Fun2,Iterator,Iterator> zipWith () { return ZipWith.zipWith(this); } }