|
| 1 | +package com.jnape.palatable.lambda.functions; |
| 2 | + |
| 3 | +import com.jnape.palatable.lambda.adt.hlist.Tuple2; |
| 4 | + |
| 5 | +/** |
| 6 | + * A function taking four arguments. Defined in terms of <code>Fn3</code>, so similarly auto-curried. |
| 7 | + * |
| 8 | + * @param <A> The first argument type |
| 9 | + * @param <B> The second argument type |
| 10 | + * @param <C> The third argument type |
| 11 | + * @param <D> The fourth argument type |
| 12 | + * @param <E> The return type |
| 13 | + * @see Fn3 |
| 14 | + */ |
| 15 | +@FunctionalInterface |
| 16 | +public interface Fn4<A, B, C, D, E> extends Fn3<A, B, C, Fn1<D, E>> { |
| 17 | + |
| 18 | + /** |
| 19 | + * Invoke this function with the given arguments. |
| 20 | + * |
| 21 | + * @param a the first argument |
| 22 | + * @param b the second argument |
| 23 | + * @param c the third argument |
| 24 | + * @param d the fourth argument |
| 25 | + * @return the result of the function application |
| 26 | + */ |
| 27 | + E apply(A a, B b, C c, D d); |
| 28 | + |
| 29 | + /** |
| 30 | + * Partially apply this function by taking its first argument. |
| 31 | + * |
| 32 | + * @param a the first argument |
| 33 | + * @return an Fn3 that takes the second, third, and fourth argument and returns the result |
| 34 | + */ |
| 35 | + @Override |
| 36 | + default Fn3<B, C, D, E> apply(A a) { |
| 37 | + return (b, c, d) -> apply(a, b, c, d); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Partially apply this function by taking its first two arguments. |
| 42 | + * |
| 43 | + * @param a the first argument |
| 44 | + * @param b the second argument |
| 45 | + * @return an Fn2 that takes the third and fourth arguments and returns the result |
| 46 | + */ |
| 47 | + @Override |
| 48 | + default Fn2<C, D, E> apply(A a, B b) { |
| 49 | + return (c, d) -> apply(a, b, c, d); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Partially apply this function by taking its first three arguments. |
| 54 | + * |
| 55 | + * @param a the first argument |
| 56 | + * @param b the second argument |
| 57 | + * @param c the third argument |
| 58 | + * @return an Fn1 that takes the fourth argument and returns the result |
| 59 | + */ |
| 60 | + @Override |
| 61 | + default Fn1<D, E> apply(A a, B b, C c) { |
| 62 | + return (d) -> apply(a, b, c, d); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Flip the order of the first two arguments. |
| 67 | + * |
| 68 | + * @return an Fn3 that takes the first and second arguments in reversed order |
| 69 | + */ |
| 70 | + @Override |
| 71 | + default Fn4<B, A, C, D, E> flip() { |
| 72 | + return (b, a, c, d) -> apply(a, b, c, d); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Returns an <code>Fn3</code> that takes the first two arguments as a <code>Tuple2<A, B></code> and the third |
| 77 | + * and fourth arguments. |
| 78 | + * |
| 79 | + * @return an Fn3 taking a Tuple2 and the third and fourth arguments |
| 80 | + */ |
| 81 | + @Override |
| 82 | + default Fn3<Tuple2<A, B>, C, D, E> uncurry() { |
| 83 | + return (ab, c, d) -> apply(ab._1(), ab._2(), c, d); |
| 84 | + } |
| 85 | +} |
0 commit comments