Skip to content

Commit bd49839

Browse files
committed
adding Tuple2/3/4#into
1 parent d598452 commit bd49839

File tree

6 files changed

+59
-0
lines changed

6 files changed

+59
-0
lines changed

src/main/java/com/jnape/palatable/lambda/adt/hlist/Tuple2.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.jnape.palatable.lambda.functor.Functor;
66

77
import java.util.Map;
8+
import java.util.function.BiFunction;
89
import java.util.function.Function;
910

1011
/**
@@ -52,6 +53,18 @@ public _2 _2() {
5253
return _2;
5354
}
5455

56+
/**
57+
* Destructure and apply this tuple to a function accepting the same number of arguments as this tuple's
58+
* slots. This can be thought of as a kind of dual to uncurrying a function and applying a tuple to it.
59+
*
60+
* @param fn the function to apply
61+
* @param <R> the return type of the function
62+
* @return the result of applying the destructured tuple to the function
63+
*/
64+
public <R> R into(BiFunction<? super _1, ? super _2, ? extends R> fn) {
65+
return fn.apply(_1, _2);
66+
}
67+
5568
@Override
5669
public _1 getKey() {
5770
return _1();

src/main/java/com/jnape/palatable/lambda/adt/hlist/Tuple3.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.jnape.palatable.lambda.adt.hlist;
22

33
import com.jnape.palatable.lambda.adt.hlist.HList.HCons;
4+
import com.jnape.palatable.lambda.functions.Fn3;
45
import com.jnape.palatable.lambda.functor.Bifunctor;
56
import com.jnape.palatable.lambda.functor.Functor;
67

@@ -62,6 +63,19 @@ public _3 _3() {
6263
return _3;
6364
}
6465

66+
/**
67+
* Destructure and apply this tuple to a function accepting the same number of arguments as this tuple's
68+
* slots.
69+
*
70+
* @param fn the function to apply
71+
* @param <R> the return type of the function
72+
* @return the result of applying the destructured tuple to the function
73+
* @see Tuple2#into
74+
*/
75+
public <R> R into(Fn3<? super _1, ? super _2, ? super _3, ? extends R> fn) {
76+
return fn.apply(_1, _2, _3);
77+
}
78+
6579
@Override
6680
public <_3Prime> Tuple3<_1, _2, _3Prime> fmap(Function<? super _3, ? extends _3Prime> fn) {
6781
return biMapR(fn);

src/main/java/com/jnape/palatable/lambda/adt/hlist/Tuple4.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.jnape.palatable.lambda.adt.hlist;
22

33
import com.jnape.palatable.lambda.adt.hlist.HList.HCons;
4+
import com.jnape.palatable.lambda.functions.Fn4;
45
import com.jnape.palatable.lambda.functor.Bifunctor;
56
import com.jnape.palatable.lambda.functor.Functor;
67

@@ -74,6 +75,19 @@ public _4 _4() {
7475
return _4;
7576
}
7677

78+
/**
79+
* Destructure and apply this tuple to a function accepting the same number of arguments as this tuple's
80+
* slots.
81+
*
82+
* @param fn the function to apply
83+
* @param <R> the return type of the function
84+
* @return the result of applying the destructured tuple to the function
85+
* @see Tuple2#into
86+
*/
87+
public <R> R into(Fn4<? super _1, ? super _2, ? super _3, ? super _4, ? extends R> fn) {
88+
return fn.apply(_1, _2, _3, _4);
89+
}
90+
7791
@Override
7892
public <_4Prime> Tuple4<_1, _2, _3, _4Prime> fmap(Function<? super _4, ? extends _4Prime> fn) {
7993
return biMapR(fn);

src/test/java/com/jnape/palatable/lambda/adt/hlist/Tuple2Test.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ public void randomAccess() {
5555
verifyNoMoreInteractions(spiedTail);
5656
}
5757

58+
@Test
59+
public void into() {
60+
Tuple2<String, Integer> tuple = tuple("foo", 1);
61+
assertEquals("foo1", tuple.into((s, i) -> s + i));
62+
}
63+
5864
@Test
5965
public void functorProperties() {
6066
assertEquals(new Tuple2<>(1, new SingletonHList<>("2")), tuple2.fmap(Object::toString));

src/test/java/com/jnape/palatable/lambda/adt/hlist/Tuple3Test.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ public void randomAccess() {
5454
verifyNoMoreInteractions(spiedTail);
5555
}
5656

57+
@Test
58+
public void into() {
59+
Tuple3<String, Integer, Double> tuple = tuple("foo", 1, 2.0d);
60+
assertEquals("foo12.0", tuple.into((s, i, d) -> s + i + d));
61+
}
62+
5763
@Test
5864
public void functorProperties() {
5965
assertEquals(new Tuple3<>(1, new Tuple2<>("2", new SingletonHList<>("3"))), tuple3.fmap(Object::toString));

src/test/java/com/jnape/palatable/lambda/adt/hlist/Tuple4Test.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ public void randomAccess() {
5757
verifyNoMoreInteractions(spiedTail);
5858
}
5959

60+
@Test
61+
public void into() {
62+
Tuple4<String, Integer, Double, Boolean> tuple = tuple("foo", 1, 2.0d, false);
63+
assertEquals("foo12.0false", tuple.into((s, i, d, b) -> s + i + d + b));
64+
}
65+
6066
@Test
6167
public void functorProperties() {
6268
assertEquals(new Tuple4<>(1, new Tuple3<>("2", new Tuple2<>('3', new SingletonHList<>(true)))), tuple4.fmap(x -> !x));

0 commit comments

Comments
 (0)