Skip to content

Commit db1cbb7

Browse files
7h3kk1djnape
authored andcommitted
Add init methods to Tuple classes
1 parent cc0cda9 commit db1cbb7

File tree

15 files changed

+111
-0
lines changed

15 files changed

+111
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
99
- `IterateT#unfold` now only computes a single `Pure` for the given input
1010
- `ReaderT#fmap` and `StateT#fmap` avoid unnecessary calls to `pure`
1111
- `MaybeT` implements `MonadError`
12+
- `Tuple2-8#init`, for populating a `TupleN` with all but the last element
1213

1314
### Added
1415
- `$`, function application represented as a higher-order `Fn2`

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,15 @@ AppTrav extends Applicative<TravB, App>> AppTrav traverse(
237237
return fn.apply(_2).fmap(_2Prime -> fmap(constantly(_2Prime))).<TravB>fmap(Applicative::coerce).coerce();
238238
}
239239

240+
/**
241+
* Returns a <code>{@link SingletonHList}&lt;_1&gt;</code> of the first element.
242+
*
243+
* @return The {@link SingletonHList}&lt;_1&gt;
244+
*/
245+
public SingletonHList<_1> init() {
246+
return invert().tail();
247+
}
248+
240249
/**
241250
* Static factory method for creating <code>Tuple2</code>s from {@link java.util.Map.Entry}s.
242251
*

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
@@ -219,6 +219,16 @@ 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 <code>{@link Tuple2}&lt;_1, _2&gt;</code> of all the elements of this
224+
* <code>{@link Tuple3}&lt;_1, _2, _3&gt;</code> except the last.
225+
*
226+
* @return The {@link Tuple2}&lt;_1, _2&gt; representing all but the last element
227+
*/
228+
public Tuple2<_1, _2> init() {
229+
return rotateR3().tail();
230+
}
231+
222232
/**
223233
* Given a value of type <code>A</code>, produced an instance of this tuple with each slot set to that value.
224234
*

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
@@ -246,6 +246,16 @@ 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 <code>{@link Tuple3}&lt;_1, _2, _3&gt;</code> of all the elements of this
251+
* <code>{@link Tuple4}&lt;_1, _2, _3, _4&gt;</code> except the last.
252+
*
253+
* @return The {@link Tuple3}&lt;_1, _2, _3&gt; representing all but the last element
254+
*/
255+
public Tuple3<_1, _2, _3> init() {
256+
return rotateR4().tail();
257+
}
258+
249259
/**
250260
* Given a value of type <code>A</code>, produced an instance of this tuple with each slot set to that value.
251261
*

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
@@ -273,6 +273,16 @@ 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 <code>{@link Tuple4}&lt;_1, _2, _3, _4&gt;</code> of all the elements of this
278+
* <code>{@link Tuple5}&lt;_1, _2, _3, _4, _5&gt;</code> except the last.
279+
*
280+
* @return The {@link Tuple4}&lt;_1, _2, _3, _4&gt; representing all but the last element
281+
*/
282+
public Tuple4<_1, _2, _3, _4> init() {
283+
return rotateR5().tail();
284+
}
285+
276286
/**
277287
* Given a value of type <code>A</code>, produced an instance of this tuple with each slot set to that value.
278288
*

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
@@ -304,6 +304,16 @@ 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 <code>{@link Tuple5}&lt;_1, _2, _3, _4, _5&gt;</code> of all the elements of this
309+
* <code>{@link Tuple6}&lt;_1, _2, _3, _4, _5, _6&gt;</code> except the last.
310+
*
311+
* @return The {@link Tuple5}&lt;_1, _2, _3, _4, _5&gt; representing all but the last element
312+
*/
313+
public Tuple5<_1, _2, _3, _4, _5> init() {
314+
return rotateR6().tail();
315+
}
316+
307317
/**
308318
* Given a value of type <code>A</code>, produced an instance of this tuple with each slot set to that value.
309319
*

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
@@ -334,6 +334,16 @@ 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 <code>{@link Tuple6}&lt;_1, _2, _3, _4, _5, _6&gt;</code> of all the elements of this
339+
* <code>{@link Tuple7}&lt;_1, _2, _3, _4, _5, _6, _7&gt;</code> except the last.
340+
*
341+
* @return The {@link Tuple6}&lt;_1, _2, _3, _4, _5, _6&gt; representing all but the last element
342+
*/
343+
public Tuple6<_1, _2, _3, _4, _5, _6> init() {
344+
return rotateR7().tail();
345+
}
346+
337347
/**
338348
* Given a value of type <code>A</code>, produced an instance of this tuple with each slot set to that value.
339349
*

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
@@ -364,6 +364,16 @@ 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 <code>{@link Tuple7}&lt;_1, _2, _3, _4, _5, _6, _7&gt;</code> of all the elements of this
369+
* <code>{@link Tuple8}&lt;_1, _2, _3, _4, _5, _6, _7, _8&gt;</code> except the last.
370+
*
371+
* @return The {@link Tuple7}&lt;_1, _2, _3, _4, _5, _6, _7&gt; representing all but the last element
372+
*/
373+
public Tuple7<_1, _2, _3, _4, _5, _6, _7> init() {
374+
return rotateR8().tail();
375+
}
376+
367377
/**
368378
* Given a value of type <code>A</code>, produced an instance of this tuple with each slot set to that value.
369379
*

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
@@ -63,6 +63,11 @@ public void tail() {
6363
assertEquals(new SingletonHList<>(2), tuple2.tail());
6464
}
6565

66+
@Test
67+
public void init() {
68+
assertEquals(new SingletonHList<>(1), tuple2.init());
69+
}
70+
6671
@Test
6772
public void cons() {
6873
assertEquals(new Tuple3<>(0, tuple2), tuple2.cons(0));

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
@@ -126,4 +126,10 @@ public void staticPure() {
126126
Tuple3<Integer, String, Character> tuple = pureTuple(1, "2").apply('3');
127127
assertEquals(tuple(1, "2", '3'), tuple);
128128
}
129+
130+
@Test
131+
public void init() {
132+
assertEquals(tuple(1, 2),
133+
tuple(1, 2, 3).init());
134+
}
129135
}

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
@@ -127,4 +127,10 @@ public void staticPure() {
127127
Tuple4<Integer, String, Character, Boolean> tuple = pureTuple(1, "2", '3').apply(true);
128128
assertEquals(tuple(1, "2", '3', true), tuple);
129129
}
130+
131+
@Test
132+
public void init() {
133+
assertEquals(tuple(1, 2, 3),
134+
tuple(1, 2, 3, 4).init());
135+
}
130136
}

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
@@ -133,4 +133,10 @@ public void staticPure() {
133133
Tuple5<Integer, String, Character, Boolean, Float> tuple = pureTuple(1, "2", '3', true).apply(5f);
134134
assertEquals(tuple(1, "2", '3', true, 5f), tuple);
135135
}
136+
137+
@Test
138+
public void init() {
139+
assertEquals(tuple(1, 2, 3, 4),
140+
tuple(1, 2, 3, 4, 5).init());
141+
}
136142
}

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
@@ -137,4 +137,10 @@ public void staticPure() {
137137
Tuple6<Integer, String, Character, Boolean, Float, Byte> tuple = pureTuple(1, "2", '3', true, 5f).apply((byte) 6);
138138
assertEquals(tuple(1, "2", '3', true, 5f, (byte) 6), tuple);
139139
}
140+
141+
@Test
142+
public void init() {
143+
assertEquals(tuple(1, 2, 3, 4, 5),
144+
tuple(1, 2, 3, 4, 5, 6).init());
145+
}
140146
}

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
@@ -141,4 +141,10 @@ public void staticPure() {
141141
pureTuple((byte) 1, (short) 2, 3, 4L, 5F, 6D).apply(true);
142142
assertEquals(tuple((byte) 1, (short) 2, 3, 4L, 5F, 6D, true), tuple);
143143
}
144+
145+
@Test
146+
public void init() {
147+
assertEquals(tuple(1, 2, 3, 4, 5, 6),
148+
tuple(1, 2, 3, 4, 5, 6, 7).init());
149+
}
144150
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,10 @@ public void staticPure() {
158158
pureTuple((byte) 1, (short) 2, 3, 4L, 5F, 6D, true).apply('8');
159159
assertEquals(tuple((byte) 1, (short) 2, 3, 4L, 5F, 6D, true, '8'), tuple);
160160
}
161+
162+
@Test
163+
public void init() {
164+
assertEquals(tuple(1, 2, 3, 4, 5, 6, 7),
165+
tuple(1, 2, 3, 4, 5, 6, 7, 8).init());
166+
}
161167
}

0 commit comments

Comments
 (0)