Skip to content

Commit 9202290

Browse files
committed
Adding into, used for applying a destructured tuple to a bifunction
1 parent 5f8a73b commit 9202290

File tree

2 files changed

+56
-0
lines changed
  • src
    • main/java/com/jnape/palatable/lambda/functions/builtin/fn2
    • test/java/com/jnape/palatable/lambda/functions/builtin/fn2

2 files changed

+56
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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}&lt;A, B, C&gt;</code> and a <code>{@link Tuple2}&lt;A, B&gt;</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+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.jnape.palatable.lambda.functions.builtin.fn2;
2+
3+
import org.junit.Test;
4+
5+
import static com.jnape.palatable.lambda.adt.hlist.HList.tuple;
6+
import static com.jnape.palatable.lambda.functions.builtin.fn2.Into.into;
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class IntoTest {
10+
11+
@Test
12+
public void appliesTupleToFunction() {
13+
assertEquals((Integer) 3, into((a, b) -> a + b, tuple(1, 2)));
14+
}
15+
}

0 commit comments

Comments
 (0)