Skip to content

Commit 7ec5690

Browse files
iamjpottsjnape
authored andcommitted
Add snoc to HNil, SingletonHList, and Tuple classes
1 parent 66b0459 commit 7ec5690

19 files changed

+154
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
1111
### Added
1212
- `$`, function application represented as a higher-order `Fn2`
1313
- `Fn1#withSelf`, a static method for constructing a self-referencing `Fn1`
14+
- `HNil/SingletonHList/TupleX#snoc`, a method to add a new last element (append to a tuple)
1415

1516
## [5.2.0] - 2020-02-12
1617

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,5 +297,16 @@ private HNil() {
297297
public <Head> SingletonHList<Head> cons(Head head) {
298298
return new SingletonHList<>(head);
299299
}
300+
301+
/**
302+
* Snoc an element onto the back of this HList.
303+
*
304+
* @param last the new last element
305+
* @param <Last> the new last element type
306+
* @return the updated HList
307+
*/
308+
public <Last> SingletonHList<Last> snoc(Last last) {
309+
return new SingletonHList<>(last);
310+
}
300311
}
301312
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ public <_0> Tuple2<_0, _1> cons(_0 _0) {
3939
return new Tuple2<>(_0, this);
4040
}
4141

42+
43+
/**
44+
* Snoc an element onto the back of this HList.
45+
*
46+
* @param _2 the new last element
47+
* @return the updated HList
48+
*/
49+
public <_2> Tuple2<_1, _2> snoc(_2 _2) {
50+
return tuple(head(), _2);
51+
}
52+
4253
/**
4354
* {@inheritDoc}
4455
*/

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ public <_0> Tuple3<_0, _1, _2> cons(_0 _0) {
7575
return new Tuple3<>(_0, this);
7676
}
7777

78+
/**
79+
* Snoc an element onto the back of this HList.
80+
*
81+
* @param _3 the new last element
82+
* @return the updated HList
83+
*/
84+
public <_3> Tuple3<_1, _2, _3> snoc(_3 _3) {
85+
return tuple(_1, _2, _3);
86+
}
87+
7888
/**
7989
* {@inheritDoc}
8090
*/

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ public <_0> Tuple4<_0, _1, _2, _3> cons(_0 _0) {
5656
return new Tuple4<>(_0, this);
5757
}
5858

59+
/**
60+
* Snoc an element onto the back of this HList.
61+
*
62+
* @param _4 the new last element
63+
* @return the updated HList
64+
*/
65+
public <_4> Tuple4<_1, _2, _3, _4> snoc(_4 _4) {
66+
return tuple(_1, _2, _3, _4);
67+
}
68+
5969
/**
6070
* {@inheritDoc}
6171
*/

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ public <_0> Tuple5<_0, _1, _2, _3, _4> cons(_0 _0) {
5959
return new Tuple5<>(_0, this);
6060
}
6161

62+
/**
63+
* Snoc an element onto the back of this HList.
64+
*
65+
* @param _5 the new last element
66+
* @return the updated HList
67+
*/
68+
public <_5> Tuple5<_1, _2, _3, _4, _5> snoc(_5 _5) {
69+
return tuple(_1, _2, _3, _4, _5);
70+
}
71+
6272
/**
6373
* {@inheritDoc}
6474
*/

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ public <_0> Tuple6<_0, _1, _2, _3, _4, _5> cons(_0 _0) {
6262
return new Tuple6<>(_0, this);
6363
}
6464

65+
/**
66+
* Snoc an element onto the back of this HList.
67+
*
68+
* @param _6 the new last element
69+
* @return the updated HList
70+
*/
71+
public <_6> Tuple6<_1, _2, _3, _4, _5, _6> snoc(_6 _6) {
72+
return tuple(_1, _2, _3, _4, _5, _6);
73+
}
74+
6575
/**
6676
* {@inheritDoc}
6777
*/

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ public <_0> Tuple7<_0, _1, _2, _3, _4, _5, _6> cons(_0 _0) {
6666
return new Tuple7<>(_0, this);
6767
}
6868

69+
/**
70+
* Snoc an element onto the back of this HList.
71+
*
72+
* @param _7 the new last element
73+
* @return the updated HList
74+
*/
75+
public <_7> Tuple7<_1, _2, _3, _4, _5, _6, _7> snoc(_7 _7) {
76+
return tuple(_1, _2, _3, _4, _5, _6, _7);
77+
}
78+
6979
/**
7080
* {@inheritDoc}
7181
*/

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ public <_0> Tuple8<_0, _1, _2, _3, _4, _5, _6, _7> cons(_0 _0) {
7070
return new Tuple8<>(_0, this);
7171
}
7272

73+
/**
74+
* Snoc an element onto the back of this HList.
75+
*
76+
* @param _8 the new last element
77+
* @return the updated HList
78+
*/
79+
public <_8> Tuple8<_1, _2, _3, _4, _5, _6, _7, _8> snoc(_8 _8) {
80+
return tuple(_1, _2, _3, _4, _5, _6, _7, _8);
81+
}
82+
7383
/**
7484
* {@inheritDoc}
7585
*/

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,16 @@ public <_0> HCons<_0, Tuple8<_1, _2, _3, _4, _5, _6, _7, _8>> cons(_0 _0) {
7474
return new HCons<>(_0, this);
7575
}
7676

77+
/**
78+
* Snoc an element onto the back of this HList.
79+
*
80+
* @param _9 the new last element
81+
* @return the updated HList
82+
*/
83+
public <_9> HCons<_1, Tuple8<_2, _3, _4, _5, _6, _7, _8, _9>> snoc(_9 _9) {
84+
return singletonHList(_9).cons(_8).cons(_7).cons(_6).cons(_5).cons(_4).cons(_3).cons(_2).cons(_1);
85+
}
86+
7787
/**
7888
* {@inheritDoc}
7989
*/

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,10 @@ public void hashCodeUsesDecentDistribution() {
6868
assertNotEquals(nil().cons(1).hashCode(), nil().cons(2).hashCode());
6969
assertNotEquals(nil().cons(1).cons(2).hashCode(), nil().cons(1).cons(3).hashCode());
7070
}
71+
72+
@Test
73+
public void snoc() {
74+
SingletonHList<Float> tuple = nil().snoc((float) 4.0);
75+
assertEquals(4.0, tuple.head(), 0.01);
76+
}
7177
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import static com.jnape.palatable.lambda.adt.hlist.HList.nil;
1515
import static com.jnape.palatable.lambda.adt.hlist.HList.singletonHList;
16+
import static com.jnape.palatable.lambda.adt.hlist.HList.tuple;
1617
import static com.jnape.palatable.lambda.adt.hlist.SingletonHList.pureSingletonHList;
1718
import static org.junit.Assert.assertEquals;
1819

@@ -56,4 +57,10 @@ public void staticPure() {
5657
SingletonHList<Integer> singletonHList = pureSingletonHList().apply(1);
5758
assertEquals(singletonHList(1), singletonHList);
5859
}
60+
61+
@Test
62+
public void snoc() {
63+
Tuple2<Byte, Character> tuple = singletonHList((byte) 127).snoc('x');
64+
assertEquals(tuple((byte) 127, 'x'), tuple);
65+
}
5966
}

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
@@ -143,4 +143,10 @@ public void staticPure() {
143143
Tuple2<Integer, String> tuple = pureTuple(1).apply("two");
144144
assertEquals(tuple(1, "two"), tuple);
145145
}
146+
147+
@Test
148+
public void snoc() {
149+
Tuple3<Long, Integer, String> tuple = tuple(Long.MAX_VALUE, 123).snoc("hi");
150+
assertEquals(tuple(Long.MAX_VALUE, 123, "hi"), tuple);
151+
}
146152
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@
1313
import testsupport.traits.MonadRecLaws;
1414
import testsupport.traits.TraversableLaws;
1515

16+
import java.time.Duration;
17+
1618
import static com.jnape.palatable.lambda.adt.Maybe.just;
1719
import static com.jnape.palatable.lambda.adt.Maybe.nothing;
1820
import static com.jnape.palatable.lambda.adt.hlist.HList.tuple;
1921
import static com.jnape.palatable.lambda.adt.hlist.Tuple3.pureTuple;
2022
import static com.jnape.palatable.lambda.functions.builtin.fn1.Repeat.repeat;
23+
import static java.time.Duration.ofSeconds;
2124
import static java.util.Collections.emptyList;
2225
import static java.util.Collections.singletonList;
2326
import static org.junit.Assert.assertEquals;
@@ -119,4 +122,10 @@ public void staticPure() {
119122
Tuple3<Integer, String, Character> tuple = pureTuple(1, "2").apply('3');
120123
assertEquals(tuple(1, "2", '3'), tuple);
121124
}
125+
126+
@Test
127+
public void snoc() {
128+
Tuple4<String, Long, Integer, Duration> tuple = tuple("qux", Long.MIN_VALUE, 7).snoc(ofSeconds(13));
129+
assertEquals(tuple("qux", Long.MIN_VALUE, 7, ofSeconds(13)), tuple);
130+
}
122131
}

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
@@ -122,4 +122,10 @@ public void staticPure() {
122122
Tuple4<Integer, String, Character, Boolean> tuple = pureTuple(1, "2", '3').apply(true);
123123
assertEquals(tuple(1, "2", '3', true), tuple);
124124
}
125+
126+
@Test
127+
public void snoc() {
128+
Tuple5<String, Integer, String, Long, Integer> tuple = tuple("qux", 7, "foo", 13L).snoc(17);
129+
assertEquals(tuple("qux", 7, "foo", 13L, 17), tuple);
130+
}
125131
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,10 @@ public void staticPure() {
128128
Tuple5<Integer, String, Character, Boolean, Float> tuple = pureTuple(1, "2", '3', true).apply(5f);
129129
assertEquals(tuple(1, "2", '3', true, 5f), tuple);
130130
}
131+
132+
@Test
133+
public void snoc() {
134+
Tuple6<String, Integer, String, Integer, String, Integer> tuple = tuple("a", 5, "b", 7, "c").snoc(11);
135+
assertEquals(tuple("a", 5, "b", 7, "c", 11), tuple);
136+
}
131137
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,10 @@ public void staticPure() {
132132
Tuple6<Integer, String, Character, Boolean, Float, Byte> tuple = pureTuple(1, "2", '3', true, 5f).apply((byte) 6);
133133
assertEquals(tuple(1, "2", '3', true, 5f, (byte) 6), tuple);
134134
}
135+
136+
@Test
137+
public void snoc() {
138+
Tuple7<Long, String, Integer, String, Integer, String, Integer> tuple = tuple(5L, "a", 7, "b", 11, "c").snoc(13);
139+
assertEquals(tuple(5L, "a", 7, "b", 11, "c", 13), tuple);
140+
}
135141
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,10 @@ public void staticPure() {
136136
pureTuple((byte) 1, (short) 2, 3, 4L, 5F, 6D).apply(true);
137137
assertEquals(tuple((byte) 1, (short) 2, 3, 4L, 5F, 6D, true), tuple);
138138
}
139+
140+
@Test
141+
public void snoc() {
142+
Tuple8<String, Long, String, Integer, String, Integer, String, Character> tuple = tuple("b", 7L, "c", 11, "d", 13, "e").snoc('f');
143+
assertEquals(tuple("b", 7L, "c", 11, "d", 13, "e", 'f'), tuple);
144+
}
139145
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import testsupport.traits.MonadRecLaws;
1515
import testsupport.traits.TraversableLaws;
1616

17+
import java.time.LocalDate;
18+
1719
import static com.jnape.palatable.lambda.adt.Maybe.just;
1820
import static com.jnape.palatable.lambda.adt.Maybe.nothing;
1921
import static com.jnape.palatable.lambda.adt.hlist.HList.tuple;
@@ -147,4 +149,11 @@ public void staticPure() {
147149
pureTuple((byte) 1, (short) 2, 3, 4L, 5F, 6D, true).apply('8');
148150
assertEquals(tuple((byte) 1, (short) 2, 3, 4L, 5F, 6D, true, '8'), tuple);
149151
}
152+
153+
@Test
154+
public void snoc() {
155+
HCons<String, Tuple8<Long, String, Integer, String, Integer, String, Long, LocalDate>> actual = tuple("b", 7L, "c", 11, "d", 13, "e", 15L).snoc(LocalDate.of(2020, 4, 14));
156+
assertEquals("b", actual.head());
157+
assertEquals(actual.tail(), tuple(7L, "c", 11, "d", 13, "e", 15L, LocalDate.of(2020, 4, 14)));
158+
}
150159
}

0 commit comments

Comments
 (0)