Skip to content

Commit

Permalink
Add snoc to HNil, SingletonHList, and Tuple classes
Browse files Browse the repository at this point in the history
  • Loading branch information
iamjpotts authored and jnape committed Jun 18, 2020
1 parent 66b0459 commit 7ec5690
Show file tree
Hide file tree
Showing 19 changed files with 154 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
### Added
- `$`, function application represented as a higher-order `Fn2`
- `Fn1#withSelf`, a static method for constructing a self-referencing `Fn1`
- `HNil/SingletonHList/TupleX#snoc`, a method to add a new last element (append to a tuple)

## [5.2.0] - 2020-02-12

Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/jnape/palatable/lambda/adt/hlist/HList.java
Original file line number Diff line number Diff line change
Expand Up @@ -297,5 +297,16 @@ private HNil() {
public <Head> SingletonHList<Head> cons(Head head) {
return new SingletonHList<>(head);
}

/**
* Snoc an element onto the back of this HList.
*
* @param last the new last element
* @param <Last> the new last element type
* @return the updated HList
*/
public <Last> SingletonHList<Last> snoc(Last last) {
return new SingletonHList<>(last);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ public <_0> Tuple2<_0, _1> cons(_0 _0) {
return new Tuple2<>(_0, this);
}


/**
* Snoc an element onto the back of this HList.
*
* @param _2 the new last element
* @return the updated HList
*/
public <_2> Tuple2<_1, _2> snoc(_2 _2) {
return tuple(head(), _2);
}

/**
* {@inheritDoc}
*/
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/jnape/palatable/lambda/adt/hlist/Tuple2.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ public <_0> Tuple3<_0, _1, _2> cons(_0 _0) {
return new Tuple3<>(_0, this);
}

/**
* Snoc an element onto the back of this HList.
*
* @param _3 the new last element
* @return the updated HList
*/
public <_3> Tuple3<_1, _2, _3> snoc(_3 _3) {
return tuple(_1, _2, _3);
}

/**
* {@inheritDoc}
*/
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/jnape/palatable/lambda/adt/hlist/Tuple3.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ public <_0> Tuple4<_0, _1, _2, _3> cons(_0 _0) {
return new Tuple4<>(_0, this);
}

/**
* Snoc an element onto the back of this HList.
*
* @param _4 the new last element
* @return the updated HList
*/
public <_4> Tuple4<_1, _2, _3, _4> snoc(_4 _4) {
return tuple(_1, _2, _3, _4);
}

/**
* {@inheritDoc}
*/
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/jnape/palatable/lambda/adt/hlist/Tuple4.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ public <_0> Tuple5<_0, _1, _2, _3, _4> cons(_0 _0) {
return new Tuple5<>(_0, this);
}

/**
* Snoc an element onto the back of this HList.
*
* @param _5 the new last element
* @return the updated HList
*/
public <_5> Tuple5<_1, _2, _3, _4, _5> snoc(_5 _5) {
return tuple(_1, _2, _3, _4, _5);
}

/**
* {@inheritDoc}
*/
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/jnape/palatable/lambda/adt/hlist/Tuple5.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ public <_0> Tuple6<_0, _1, _2, _3, _4, _5> cons(_0 _0) {
return new Tuple6<>(_0, this);
}

/**
* Snoc an element onto the back of this HList.
*
* @param _6 the new last element
* @return the updated HList
*/
public <_6> Tuple6<_1, _2, _3, _4, _5, _6> snoc(_6 _6) {
return tuple(_1, _2, _3, _4, _5, _6);
}

/**
* {@inheritDoc}
*/
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/jnape/palatable/lambda/adt/hlist/Tuple6.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ public <_0> Tuple7<_0, _1, _2, _3, _4, _5, _6> cons(_0 _0) {
return new Tuple7<>(_0, this);
}

/**
* Snoc an element onto the back of this HList.
*
* @param _7 the new last element
* @return the updated HList
*/
public <_7> Tuple7<_1, _2, _3, _4, _5, _6, _7> snoc(_7 _7) {
return tuple(_1, _2, _3, _4, _5, _6, _7);
}

/**
* {@inheritDoc}
*/
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/jnape/palatable/lambda/adt/hlist/Tuple7.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ public <_0> Tuple8<_0, _1, _2, _3, _4, _5, _6, _7> cons(_0 _0) {
return new Tuple8<>(_0, this);
}

/**
* Snoc an element onto the back of this HList.
*
* @param _8 the new last element
* @return the updated HList
*/
public <_8> Tuple8<_1, _2, _3, _4, _5, _6, _7, _8> snoc(_8 _8) {
return tuple(_1, _2, _3, _4, _5, _6, _7, _8);
}

/**
* {@inheritDoc}
*/
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/jnape/palatable/lambda/adt/hlist/Tuple8.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ public <_0> HCons<_0, Tuple8<_1, _2, _3, _4, _5, _6, _7, _8>> cons(_0 _0) {
return new HCons<>(_0, this);
}

/**
* Snoc an element onto the back of this HList.
*
* @param _9 the new last element
* @return the updated HList
*/
public <_9> HCons<_1, Tuple8<_2, _3, _4, _5, _6, _7, _8, _9>> snoc(_9 _9) {
return singletonHList(_9).cons(_8).cons(_7).cons(_6).cons(_5).cons(_4).cons(_3).cons(_2).cons(_1);
}

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,10 @@ public void hashCodeUsesDecentDistribution() {
assertNotEquals(nil().cons(1).hashCode(), nil().cons(2).hashCode());
assertNotEquals(nil().cons(1).cons(2).hashCode(), nil().cons(1).cons(3).hashCode());
}

@Test
public void snoc() {
SingletonHList<Float> tuple = nil().snoc((float) 4.0);
assertEquals(4.0, tuple.head(), 0.01);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import static com.jnape.palatable.lambda.adt.hlist.HList.nil;
import static com.jnape.palatable.lambda.adt.hlist.HList.singletonHList;
import static com.jnape.palatable.lambda.adt.hlist.HList.tuple;
import static com.jnape.palatable.lambda.adt.hlist.SingletonHList.pureSingletonHList;
import static org.junit.Assert.assertEquals;

Expand Down Expand Up @@ -56,4 +57,10 @@ public void staticPure() {
SingletonHList<Integer> singletonHList = pureSingletonHList().apply(1);
assertEquals(singletonHList(1), singletonHList);
}

@Test
public void snoc() {
Tuple2<Byte, Character> tuple = singletonHList((byte) 127).snoc('x');
assertEquals(tuple((byte) 127, 'x'), tuple);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,10 @@ public void staticPure() {
Tuple2<Integer, String> tuple = pureTuple(1).apply("two");
assertEquals(tuple(1, "two"), tuple);
}

@Test
public void snoc() {
Tuple3<Long, Integer, String> tuple = tuple(Long.MAX_VALUE, 123).snoc("hi");
assertEquals(tuple(Long.MAX_VALUE, 123, "hi"), tuple);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@
import testsupport.traits.MonadRecLaws;
import testsupport.traits.TraversableLaws;

import java.time.Duration;

import static com.jnape.palatable.lambda.adt.Maybe.just;
import static com.jnape.palatable.lambda.adt.Maybe.nothing;
import static com.jnape.palatable.lambda.adt.hlist.HList.tuple;
import static com.jnape.palatable.lambda.adt.hlist.Tuple3.pureTuple;
import static com.jnape.palatable.lambda.functions.builtin.fn1.Repeat.repeat;
import static java.time.Duration.ofSeconds;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -119,4 +122,10 @@ public void staticPure() {
Tuple3<Integer, String, Character> tuple = pureTuple(1, "2").apply('3');
assertEquals(tuple(1, "2", '3'), tuple);
}

@Test
public void snoc() {
Tuple4<String, Long, Integer, Duration> tuple = tuple("qux", Long.MIN_VALUE, 7).snoc(ofSeconds(13));
assertEquals(tuple("qux", Long.MIN_VALUE, 7, ofSeconds(13)), tuple);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,10 @@ public void staticPure() {
Tuple4<Integer, String, Character, Boolean> tuple = pureTuple(1, "2", '3').apply(true);
assertEquals(tuple(1, "2", '3', true), tuple);
}

@Test
public void snoc() {
Tuple5<String, Integer, String, Long, Integer> tuple = tuple("qux", 7, "foo", 13L).snoc(17);
assertEquals(tuple("qux", 7, "foo", 13L, 17), tuple);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,10 @@ public void staticPure() {
Tuple5<Integer, String, Character, Boolean, Float> tuple = pureTuple(1, "2", '3', true).apply(5f);
assertEquals(tuple(1, "2", '3', true, 5f), tuple);
}

@Test
public void snoc() {
Tuple6<String, Integer, String, Integer, String, Integer> tuple = tuple("a", 5, "b", 7, "c").snoc(11);
assertEquals(tuple("a", 5, "b", 7, "c", 11), tuple);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,10 @@ public void staticPure() {
Tuple6<Integer, String, Character, Boolean, Float, Byte> tuple = pureTuple(1, "2", '3', true, 5f).apply((byte) 6);
assertEquals(tuple(1, "2", '3', true, 5f, (byte) 6), tuple);
}

@Test
public void snoc() {
Tuple7<Long, String, Integer, String, Integer, String, Integer> tuple = tuple(5L, "a", 7, "b", 11, "c").snoc(13);
assertEquals(tuple(5L, "a", 7, "b", 11, "c", 13), tuple);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,10 @@ public void staticPure() {
pureTuple((byte) 1, (short) 2, 3, 4L, 5F, 6D).apply(true);
assertEquals(tuple((byte) 1, (short) 2, 3, 4L, 5F, 6D, true), tuple);
}

@Test
public void snoc() {
Tuple8<String, Long, String, Integer, String, Integer, String, Character> tuple = tuple("b", 7L, "c", 11, "d", 13, "e").snoc('f');
assertEquals(tuple("b", 7L, "c", 11, "d", 13, "e", 'f'), tuple);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import testsupport.traits.MonadRecLaws;
import testsupport.traits.TraversableLaws;

import java.time.LocalDate;

import static com.jnape.palatable.lambda.adt.Maybe.just;
import static com.jnape.palatable.lambda.adt.Maybe.nothing;
import static com.jnape.palatable.lambda.adt.hlist.HList.tuple;
Expand Down Expand Up @@ -147,4 +149,11 @@ public void staticPure() {
pureTuple((byte) 1, (short) 2, 3, 4L, 5F, 6D, true).apply('8');
assertEquals(tuple((byte) 1, (short) 2, 3, 4L, 5F, 6D, true, '8'), tuple);
}

@Test
public void snoc() {
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));
assertEquals("b", actual.head());
assertEquals(actual.tail(), tuple(7L, "c", 11, "d", 13, "e", 15L, LocalDate.of(2020, 4, 14)));
}
}

0 comments on commit 7ec5690

Please sign in to comment.