|
| 1 | +package com.jnape.palatable.lambda.functions.builtin.fn2; |
| 2 | + |
| 3 | +import com.jnape.palatable.lambda.adt.hlist.Tuple2; |
| 4 | +import com.jnape.palatable.lambda.functions.Fn1; |
| 5 | +import com.jnape.palatable.lambda.functions.Fn2; |
| 6 | + |
| 7 | +import java.util.function.BiFunction; |
| 8 | + |
| 9 | +/** |
| 10 | + * Given a <code>{@link BiFunction}<A, B, C></code> and a <code>{@link Tuple2}<A, B></code>, destructure the |
| 11 | + * tuple and apply the slots as arguments to the function, returning the result. |
| 12 | + * |
| 13 | + * @param <A> the first argument type |
| 14 | + * @param <B> the second argument type |
| 15 | + * @param <C> the result type |
| 16 | + */ |
| 17 | +public final class Into<A, B, C> implements Fn2<BiFunction<? super A, ? super B, ? extends C>, Tuple2<A, B>, C> { |
| 18 | + |
| 19 | + private static final Into INSTANCE = new Into(); |
| 20 | + |
| 21 | + private Into() { |
| 22 | + } |
| 23 | + |
| 24 | + @Override |
| 25 | + public C apply(BiFunction<? super A, ? super B, ? extends C> fn, Tuple2<A, B> tuple) { |
| 26 | + return tuple.into(fn); |
| 27 | + } |
| 28 | + |
| 29 | + @SuppressWarnings("unchecked") |
| 30 | + public static <A, B, C> Into<A, B, C> into() { |
| 31 | + return INSTANCE; |
| 32 | + } |
| 33 | + |
| 34 | + public static <A, B, C> Fn1<Tuple2<A, B>, C> into(BiFunction<? super A, ? super B, ? extends C> fn) { |
| 35 | + return Into.<A, B, C>into().apply(fn); |
| 36 | + } |
| 37 | + |
| 38 | + public static <A, B, C> C into(BiFunction<? super A, ? super B, ? extends C> fn, Tuple2<A, B> tuple) { |
| 39 | + return Into.<A, B, C>into(fn).apply(tuple); |
| 40 | + } |
| 41 | +} |
0 commit comments