Skip to content

Commit e3a849d

Browse files
committed
Add init methods to Tuple classes
1 parent 8e95a1d commit e3a849d

File tree

12 files changed

+104
-61
lines changed

12 files changed

+104
-61
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,15 @@ AppTrav extends Applicative<TravB, App>> AppTrav traverse(
219219
return fn.apply(_3).fmap(_3Prime -> fmap(constantly(_3Prime))).<TravB>fmap(Applicative::coerce).coerce();
220220
}
221221

222+
/**
223+
* Returns a Tuple of all the elements of this Tuple except the last
224+
*
225+
* @return The Tuple representing all but the last element
226+
*/
227+
public Tuple2<_1, _2> init() {
228+
return rotateR3().tail();
229+
}
230+
222231
/**
223232
* Given a value of type <code>A</code>, produced an instance of this tuple with each slot set to that value.
224233
*

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,15 @@ AppTrav extends Applicative<TravB, App>> AppTrav traverse(
246246
return fn.apply(_4).fmap(_4Prime -> fmap(constantly(_4Prime))).<TravB>fmap(Applicative::coerce).coerce();
247247
}
248248

249+
/**
250+
* Returns a Tuple of all the elements of this Tuple except the last
251+
*
252+
* @return The Tuple representing all but the last element
253+
*/
254+
public Tuple3<_1, _2, _3> init() {
255+
return rotateR4().tail();
256+
}
257+
249258
/**
250259
* Given a value of type <code>A</code>, produced an instance of this tuple with each slot set to that value.
251260
*

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,15 @@ AppTrav extends Applicative<TravB, App>> AppTrav traverse(
273273
return fn.apply(_5).fmap(_3Prime -> fmap(constantly(_3Prime))).<TravB>fmap(Applicative::coerce).coerce();
274274
}
275275

276+
/**
277+
* Returns a Tuple of all the elements of this Tuple except the last
278+
*
279+
* @return The Tuple representing all but the last element
280+
*/
281+
public Tuple4<_1, _2, _3, _4> init() {
282+
return rotateR5().tail();
283+
}
284+
276285
/**
277286
* Given a value of type <code>A</code>, produced an instance of this tuple with each slot set to that value.
278287
*

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,15 @@ AppTrav extends Applicative<TravB, App>> AppTrav traverse(
304304
return fn.apply(_6).fmap(_6Prime -> fmap(constantly(_6Prime))).<TravB>fmap(Applicative::coerce).coerce();
305305
}
306306

307+
/**
308+
* Returns a Tuple of all the elements of this Tuple except the last
309+
*
310+
* @return The Tuple representing all but the last element
311+
*/
312+
public Tuple5<_1, _2, _3, _4, _5> init() {
313+
return rotateR6().tail();
314+
}
315+
307316
/**
308317
* Given a value of type <code>A</code>, produced an instance of this tuple with each slot set to that value.
309318
*

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,15 @@ AppTrav extends Applicative<TravB, App>> AppTrav traverse(
334334
return fn.apply(_7).fmap(_7Prime -> fmap(constantly(_7Prime))).<TravB>fmap(Applicative::coerce).coerce();
335335
}
336336

337+
/**
338+
* Returns a Tuple of all the elements of this Tuple except the last
339+
*
340+
* @return The Tuple representing all but the last element
341+
*/
342+
public Tuple6<_1, _2, _3, _4, _5, _6> init() {
343+
return rotateR7().tail();
344+
}
345+
337346
/**
338347
* Given a value of type <code>A</code>, produced an instance of this tuple with each slot set to that value.
339348
*

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,15 @@ AppTrav extends Applicative<TravB, App>> AppTrav traverse(
364364
return fn.apply(_8).fmap(_8Prime -> fmap(constantly(_8Prime))).<TravB>fmap(Applicative::coerce).coerce();
365365
}
366366

367+
/**
368+
* Returns a Tuple of all the elements of this Tuple except the last
369+
*
370+
* @return The Tuple representing all but the last element
371+
*/
372+
public Tuple7<_1, _2, _3, _4, _5, _6, _7> init() {
373+
return rotateR8().tail();
374+
}
375+
367376
/**
368377
* Given a value of type <code>A</code>, produced an instance of this tuple with each slot set to that value.
369378
*

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,7 @@
66
import org.junit.Before;
77
import org.junit.Test;
88
import org.junit.runner.RunWith;
9-
import testsupport.traits.ApplicativeLaws;
10-
import testsupport.traits.BifunctorLaws;
11-
import testsupport.traits.FunctorLaws;
12-
import testsupport.traits.MonadLaws;
13-
import testsupport.traits.MonadRecLaws;
14-
import testsupport.traits.TraversableLaws;
9+
import testsupport.traits.*;
1510

1611
import static com.jnape.palatable.lambda.adt.Maybe.just;
1712
import static com.jnape.palatable.lambda.adt.Maybe.nothing;
@@ -22,10 +17,7 @@
2217
import static java.util.Collections.emptyList;
2318
import static java.util.Collections.singletonList;
2419
import static org.junit.Assert.assertEquals;
25-
import static org.mockito.Mockito.spy;
26-
import static org.mockito.Mockito.times;
27-
import static org.mockito.Mockito.verify;
28-
import static org.mockito.Mockito.verifyNoMoreInteractions;
20+
import static org.mockito.Mockito.*;
2921

3022
@RunWith(Traits.class)
3123
public class Tuple3Test {
@@ -126,4 +118,10 @@ public void staticPure() {
126118
Tuple3<Integer, String, Character> tuple = pureTuple(1, "2").apply('3');
127119
assertEquals(tuple(1, "2", '3'), tuple);
128120
}
121+
122+
@Test
123+
public void init() {
124+
assertEquals(tuple(1, 2),
125+
tuple(1, 2, 3).init());
126+
}
129127
}

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,7 @@
66
import org.junit.Before;
77
import org.junit.Test;
88
import org.junit.runner.RunWith;
9-
import testsupport.traits.ApplicativeLaws;
10-
import testsupport.traits.BifunctorLaws;
11-
import testsupport.traits.FunctorLaws;
12-
import testsupport.traits.MonadLaws;
13-
import testsupport.traits.MonadRecLaws;
14-
import testsupport.traits.TraversableLaws;
9+
import testsupport.traits.*;
1510

1611
import static com.jnape.palatable.lambda.adt.Maybe.just;
1712
import static com.jnape.palatable.lambda.adt.Maybe.nothing;
@@ -21,10 +16,7 @@
2116
import static java.util.Collections.emptyList;
2217
import static java.util.Collections.singletonList;
2318
import static org.junit.Assert.assertEquals;
24-
import static org.mockito.Mockito.spy;
25-
import static org.mockito.Mockito.times;
26-
import static org.mockito.Mockito.verify;
27-
import static org.mockito.Mockito.verifyNoMoreInteractions;
19+
import static org.mockito.Mockito.*;
2820

2921
@RunWith(Traits.class)
3022
public class Tuple4Test {
@@ -127,4 +119,10 @@ public void staticPure() {
127119
Tuple4<Integer, String, Character, Boolean> tuple = pureTuple(1, "2", '3').apply(true);
128120
assertEquals(tuple(1, "2", '3', true), tuple);
129121
}
122+
123+
@Test
124+
public void init() {
125+
assertEquals(tuple(1, 2, 3),
126+
tuple(1, 2, 3, 4).init());
127+
}
130128
}

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,7 @@
77
import org.junit.Before;
88
import org.junit.Test;
99
import org.junit.runner.RunWith;
10-
import testsupport.traits.ApplicativeLaws;
11-
import testsupport.traits.BifunctorLaws;
12-
import testsupport.traits.FunctorLaws;
13-
import testsupport.traits.MonadLaws;
14-
import testsupport.traits.MonadRecLaws;
15-
import testsupport.traits.TraversableLaws;
10+
import testsupport.traits.*;
1611

1712
import static com.jnape.palatable.lambda.adt.Maybe.just;
1813
import static com.jnape.palatable.lambda.adt.Maybe.nothing;
@@ -22,10 +17,7 @@
2217
import static java.util.Collections.emptyList;
2318
import static java.util.Collections.singletonList;
2419
import static org.junit.Assert.assertEquals;
25-
import static org.mockito.Mockito.spy;
26-
import static org.mockito.Mockito.times;
27-
import static org.mockito.Mockito.verify;
28-
import static org.mockito.Mockito.verifyNoMoreInteractions;
20+
import static org.mockito.Mockito.*;
2921

3022
@RunWith(Traits.class)
3123
public class Tuple5Test {
@@ -133,4 +125,10 @@ public void staticPure() {
133125
Tuple5<Integer, String, Character, Boolean, Float> tuple = pureTuple(1, "2", '3', true).apply(5f);
134126
assertEquals(tuple(1, "2", '3', true, 5f), tuple);
135127
}
128+
129+
@Test
130+
public void init() {
131+
assertEquals(tuple(1, 2, 3, 4),
132+
tuple(1, 2, 3, 4, 5).init());
133+
}
136134
}

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,7 @@
77
import org.junit.Before;
88
import org.junit.Test;
99
import org.junit.runner.RunWith;
10-
import testsupport.traits.ApplicativeLaws;
11-
import testsupport.traits.BifunctorLaws;
12-
import testsupport.traits.FunctorLaws;
13-
import testsupport.traits.MonadLaws;
14-
import testsupport.traits.MonadRecLaws;
15-
import testsupport.traits.TraversableLaws;
10+
import testsupport.traits.*;
1611

1712
import static com.jnape.palatable.lambda.adt.Maybe.just;
1813
import static com.jnape.palatable.lambda.adt.Maybe.nothing;
@@ -22,10 +17,7 @@
2217
import static java.util.Collections.emptyList;
2318
import static java.util.Collections.singletonList;
2419
import static org.junit.Assert.assertEquals;
25-
import static org.mockito.Mockito.spy;
26-
import static org.mockito.Mockito.times;
27-
import static org.mockito.Mockito.verify;
28-
import static org.mockito.Mockito.verifyNoMoreInteractions;
20+
import static org.mockito.Mockito.*;
2921

3022
@RunWith(Traits.class)
3123
public class Tuple6Test {
@@ -134,7 +126,14 @@ public void fromIterable() {
134126

135127
@Test
136128
public void staticPure() {
137-
Tuple6<Integer, String, Character, Boolean, Float, Byte> tuple = pureTuple(1, "2", '3', true, 5f).apply((byte) 6);
129+
Tuple6<Integer, String, Character, Boolean, Float, Byte> tuple = pureTuple(1, "2", '3', true, 5f).apply(
130+
(byte) 6);
138131
assertEquals(tuple(1, "2", '3', true, 5f, (byte) 6), tuple);
139132
}
133+
134+
@Test
135+
public void init() {
136+
assertEquals(tuple(1, 2, 3, 4, 5),
137+
tuple(1, 2, 3, 4, 5, 6).init());
138+
}
140139
}

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,7 @@
77
import org.junit.Before;
88
import org.junit.Test;
99
import org.junit.runner.RunWith;
10-
import testsupport.traits.ApplicativeLaws;
11-
import testsupport.traits.BifunctorLaws;
12-
import testsupport.traits.FunctorLaws;
13-
import testsupport.traits.MonadLaws;
14-
import testsupport.traits.MonadRecLaws;
15-
import testsupport.traits.TraversableLaws;
10+
import testsupport.traits.*;
1611

1712
import static com.jnape.palatable.lambda.adt.Maybe.just;
1813
import static com.jnape.palatable.lambda.adt.Maybe.nothing;
@@ -22,10 +17,7 @@
2217
import static java.util.Collections.emptyList;
2318
import static java.util.Collections.singletonList;
2419
import static org.junit.Assert.assertEquals;
25-
import static org.mockito.Mockito.spy;
26-
import static org.mockito.Mockito.times;
27-
import static org.mockito.Mockito.verify;
28-
import static org.mockito.Mockito.verifyNoMoreInteractions;
20+
import static org.mockito.Mockito.*;
2921

3022
@RunWith(Traits.class)
3123
public class Tuple7Test {
@@ -141,4 +133,10 @@ public void staticPure() {
141133
pureTuple((byte) 1, (short) 2, 3, 4L, 5F, 6D).apply(true);
142134
assertEquals(tuple((byte) 1, (short) 2, 3, 4L, 5F, 6D, true), tuple);
143135
}
136+
137+
@Test
138+
public void init() {
139+
assertEquals(tuple(1, 2, 3, 4, 5, 6),
140+
tuple(1, 2, 3, 4, 5, 6, 7).init());
141+
}
144142
}

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,7 @@
77
import org.junit.Before;
88
import org.junit.Test;
99
import org.junit.runner.RunWith;
10-
import testsupport.traits.ApplicativeLaws;
11-
import testsupport.traits.BifunctorLaws;
12-
import testsupport.traits.FunctorLaws;
13-
import testsupport.traits.MonadLaws;
14-
import testsupport.traits.MonadRecLaws;
15-
import testsupport.traits.TraversableLaws;
10+
import testsupport.traits.*;
1611

1712
import java.time.LocalDate;
1813

@@ -24,10 +19,7 @@
2419
import static java.util.Collections.emptyList;
2520
import static java.util.Collections.singletonList;
2621
import static org.junit.Assert.assertEquals;
27-
import static org.mockito.Mockito.spy;
28-
import static org.mockito.Mockito.times;
29-
import static org.mockito.Mockito.verify;
30-
import static org.mockito.Mockito.verifyNoMoreInteractions;
22+
import static org.mockito.Mockito.*;
3123

3224
@RunWith(Traits.class)
3325
public class Tuple8Test {
@@ -158,4 +150,10 @@ public void staticPure() {
158150
pureTuple((byte) 1, (short) 2, 3, 4L, 5F, 6D, true).apply('8');
159151
assertEquals(tuple((byte) 1, (short) 2, 3, 4L, 5F, 6D, true, '8'), tuple);
160152
}
153+
154+
@Test
155+
public void init() {
156+
assertEquals(tuple(1, 2, 3, 4, 5, 6, 7),
157+
tuple(1, 2, 3, 4, 5, 6, 7, 8).init());
158+
}
161159
}

0 commit comments

Comments
 (0)