Skip to content

Commit 538b645

Browse files
committed
adding Tuple2/3/4/5#fill to fill each tuple instance with a given value
1 parent 00a4a37 commit 538b645

File tree

8 files changed

+68
-2
lines changed

8 files changed

+68
-2
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ public _2 _2() {
5757
* Destructure and apply this tuple to a function accepting the same number of arguments as this tuple's
5858
* slots. This can be thought of as a kind of dual to uncurrying a function and applying a tuple to it.
5959
*
60-
* @param fn the function to apply
61-
* @param <R> the return type of the function
60+
* @param fn the function to apply
61+
* @param <R> the return type of the function
6262
* @return the result of applying the destructured tuple to the function
6363
*/
6464
public <R> R into(BiFunction<? super _1, ? super _2, ? extends R> fn) {
@@ -115,4 +115,14 @@ public static <K, V> Tuple2<K, V> fromEntry(Map.Entry<K, V> entry) {
115115
return new Tuple2<>(entry.getKey(), singletonHList(entry.getValue()));
116116
}
117117

118+
/**
119+
* Given a value of type <code>A</code>, produced an instance of this tuple with each slot set to that value.
120+
*
121+
* @param a the value to fill the tuple with
122+
* @param <A> the value type
123+
* @return the filled tuple
124+
*/
125+
public static <A> Tuple2<A, A> fill(A a) {
126+
return tuple(a, a);
127+
}
118128
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,16 @@ public <_2Prime, _3Prime> Tuple3<_1, _2Prime, _3Prime> biMap(Function<? super _2
9898
Function<? super _3, ? extends _3Prime> rFn) {
9999
return new Tuple3<>(_1(), tail().biMap(lFn, rFn));
100100
}
101+
102+
/**
103+
* Given a value of type <code>A</code>, produced an instance of this tuple with each slot set to that value.
104+
*
105+
* @param a the value to fill the tuple with
106+
* @param <A> the value type
107+
* @return the filled tuple
108+
* @see Tuple2#fill
109+
*/
110+
public static <A> Tuple3<A, A, A> fill(A a) {
111+
return tuple(a, a, a);
112+
}
101113
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,16 @@ public <_3Prime, _4Prime> Tuple4<_1, _2, _3Prime, _4Prime> biMap(Function<? supe
110110
Function<? super _4, ? extends _4Prime> rFn) {
111111
return new Tuple4<>(_1(), tail().biMap(lFn, rFn));
112112
}
113+
114+
/**
115+
* Given a value of type <code>A</code>, produced an instance of this tuple with each slot set to that value.
116+
*
117+
* @param a the value to fill the tuple with
118+
* @param <A> the value type
119+
* @return the filled tuple
120+
* @see Tuple2#fill
121+
*/
122+
public static <A> Tuple4<A, A, A, A> fill(A a) {
123+
return tuple(a, a, a, a);
124+
}
113125
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,16 @@ public <_4Prime, _5Prime> Tuple5<_1, _2, _3, _4Prime, _5Prime> biMap(Function<?
108108
Function<? super _5, ? extends _5Prime> rFn) {
109109
return new Tuple5<>(_1(), tail().biMap(lFn, rFn));
110110
}
111+
112+
/**
113+
* Given a value of type <code>A</code>, produced an instance of this tuple with each slot set to that value.
114+
*
115+
* @param a the value to fill the tuple with
116+
* @param <A> the value type
117+
* @return the filled tuple
118+
* @see Tuple2#fill
119+
*/
120+
public static <A> Tuple5<A, A, A, A, A> fill(A a) {
121+
return tuple(a, a, a, a, a);
122+
}
111123
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ public void into() {
6161
assertEquals("foo1", tuple.into((s, i) -> s + i));
6262
}
6363

64+
@Test
65+
public void fill() {
66+
assertEquals(tuple("foo", "foo"), Tuple2.fill("foo"));
67+
}
68+
6469
@Test
6570
public void functorProperties() {
6671
assertEquals(new Tuple2<>(1, new SingletonHList<>("2")), tuple2.fmap(Object::toString));

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ public void into() {
6060
assertEquals("foo12.0", tuple.into((s, i, d) -> s + i + d));
6161
}
6262

63+
@Test
64+
public void fill() {
65+
assertEquals(tuple("foo", "foo", "foo"), Tuple3.fill("foo"));
66+
}
67+
6368
@Test
6469
public void functorProperties() {
6570
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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ public void into() {
6363
assertEquals("foo12.0false", tuple.into((s, i, d, b) -> s + i + d + b));
6464
}
6565

66+
@Test
67+
public void fill() {
68+
assertEquals(tuple("foo", "foo", "foo", "foo"), Tuple4.fill("foo"));
69+
}
70+
6671
@Test
6772
public void functorProperties() {
6873
assertEquals(new Tuple4<>(1, new Tuple3<>("2", new Tuple2<>('3', new SingletonHList<>(true)))), tuple4.fmap(x -> !x));

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ public void randomAccess() {
6161
verifyNoMoreInteractions(spiedTail);
6262
}
6363

64+
@Test
65+
public void fill() {
66+
assertEquals(tuple("foo", "foo", "foo", "foo", "foo"), Tuple5.fill("foo"));
67+
}
68+
6469
@Test
6570
public void functorProperties() {
6671
assertEquals(new Tuple5<>(1, new Tuple4<>("2", new Tuple3<>('3', new Tuple2<>(false, new SingletonHList<>("5"))))),

0 commit comments

Comments
 (0)