Skip to content

Commit e3e2048

Browse files
committed
Tuples are now generalized products
1 parent 4161ddd commit e3e2048

19 files changed

+371
-293
lines changed

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

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package com.jnape.palatable.lambda.adt.hlist;
22

33
import com.jnape.palatable.lambda.adt.hlist.HList.HCons;
4+
import com.jnape.palatable.lambda.adt.product.Product2;
45
import com.jnape.palatable.lambda.functor.Applicative;
56
import com.jnape.palatable.lambda.functor.Bifunctor;
67
import com.jnape.palatable.lambda.monad.Monad;
78
import com.jnape.palatable.lambda.traversable.Traversable;
89

910
import java.util.Map;
10-
import java.util.function.BiFunction;
1111
import java.util.function.Function;
1212

1313
import static com.jnape.palatable.lambda.functions.builtin.fn1.Constantly.constantly;
@@ -17,14 +17,19 @@
1717
*
1818
* @param <_1> The first slot element type
1919
* @param <_2> The second slot element type
20+
* @see Product2
2021
* @see HList
2122
* @see SingletonHList
2223
* @see Tuple3
2324
* @see Tuple4
2425
* @see Tuple5
2526
*/
26-
public class Tuple2<_1, _2> extends HCons<_1, SingletonHList<_2>>
27-
implements Map.Entry<_1, _2>, Monad<_2, Tuple2<_1, ?>>, Bifunctor<_1, _2, Tuple2>, Traversable<_2, Tuple2<_1, ?>> {
27+
public class Tuple2<_1, _2> extends HCons<_1, SingletonHList<_2>> implements
28+
Product2<_1, _2>,
29+
Map.Entry<_1, _2>,
30+
Monad<_2, Tuple2<_1, ?>>,
31+
Bifunctor<_1, _2, Tuple2>,
32+
Traversable<_2, Tuple2<_1, ?>> {
2833

2934
private final _1 _1;
3035
private final _2 _2;
@@ -40,36 +45,16 @@ public <_0> Tuple3<_0, _1, _2> cons(_0 _0) {
4045
return new Tuple3<>(_0, this);
4146
}
4247

43-
/**
44-
* Retrieve the first (head) element in constant time.
45-
*
46-
* @return the head element
47-
*/
48+
@Override
4849
public _1 _1() {
4950
return _1;
5051
}
5152

52-
/**
53-
* Retrieve the second element in constant time.
54-
*
55-
* @return the second element
56-
*/
53+
@Override
5754
public _2 _2() {
5855
return _2;
5956
}
6057

61-
/**
62-
* Destructure and apply this tuple to a function accepting the same number of arguments as this tuple's
63-
* slots. This can be thought of as a kind of dual to uncurrying a function and applying a tuple to it.
64-
*
65-
* @param fn the function to apply
66-
* @param <R> the return type of the function
67-
* @return the result of applying the destructured tuple to the function
68-
*/
69-
public <R> R into(BiFunction<? super _1, ? super _2, ? extends R> fn) {
70-
return fn.apply(_1, _2);
71-
}
72-
7358
@Override
7459
public _1 getKey() {
7560
return _1();

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

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.jnape.palatable.lambda.adt.hlist;
22

33
import com.jnape.palatable.lambda.adt.hlist.HList.HCons;
4-
import com.jnape.palatable.lambda.functions.Fn3;
4+
import com.jnape.palatable.lambda.adt.product.Product3;
55
import com.jnape.palatable.lambda.functor.Applicative;
66
import com.jnape.palatable.lambda.functor.Bifunctor;
77
import com.jnape.palatable.lambda.monad.Monad;
@@ -17,14 +17,19 @@
1717
* @param <_1> The first slot element type
1818
* @param <_2> The second slot element type
1919
* @param <_3> The third slot element type
20+
* @see Product3
2021
* @see HList
2122
* @see SingletonHList
2223
* @see Tuple2
2324
* @see Tuple4
2425
* @see Tuple5
2526
*/
26-
public class Tuple3<_1, _2, _3> extends HCons<_1, Tuple2<_2, _3>>
27-
implements Monad<_3, Tuple3<_1, _2, ?>>, Bifunctor<_2, _3, Tuple3<_1, ?, ?>>, Traversable<_3, Tuple3<_1, _2, ?>> {
27+
public class Tuple3<_1, _2, _3> extends HCons<_1, Tuple2<_2, _3>> implements
28+
Product3<_1, _2, _3>,
29+
Monad<_3, Tuple3<_1, _2, ?>>,
30+
Bifunctor<_2, _3, Tuple3<_1, ?, ?>>,
31+
Traversable<_3, Tuple3<_1, _2, ?>> {
32+
2833
private final _1 _1;
2934
private final _2 _2;
3035
private final _3 _3;
@@ -41,46 +46,21 @@ public <_0> Tuple4<_0, _1, _2, _3> cons(_0 _0) {
4146
return new Tuple4<>(_0, this);
4247
}
4348

44-
/**
45-
* Retrieve the first (head) element in constant time.
46-
*
47-
* @return the head element
48-
*/
49+
@Override
4950
public _1 _1() {
5051
return _1;
5152
}
5253

53-
/**
54-
* Retrieve the second element in constant time.
55-
*
56-
* @return the second element
57-
*/
54+
@Override
5855
public _2 _2() {
5956
return _2;
6057
}
6158

62-
/**
63-
* Retrieve the third element in constant time.
64-
*
65-
* @return the third element
66-
*/
59+
@Override
6760
public _3 _3() {
6861
return _3;
6962
}
7063

71-
/**
72-
* Destructure and apply this tuple to a function accepting the same number of arguments as this tuple's
73-
* slots.
74-
*
75-
* @param fn the function to apply
76-
* @param <R> the return type of the function
77-
* @return the result of applying the destructured tuple to the function
78-
* @see Tuple2#into
79-
*/
80-
public <R> R into(Fn3<? super _1, ? super _2, ? super _3, ? extends R> fn) {
81-
return fn.apply(_1, _2, _3);
82-
}
83-
8464
@Override
8565
@SuppressWarnings("unchecked")
8666
public <_3Prime> Tuple3<_1, _2, _3Prime> fmap(Function<? super _3, ? extends _3Prime> fn) {

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

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.jnape.palatable.lambda.adt.hlist;
22

33
import com.jnape.palatable.lambda.adt.hlist.HList.HCons;
4-
import com.jnape.palatable.lambda.functions.Fn4;
4+
import com.jnape.palatable.lambda.adt.product.Product4;
55
import com.jnape.palatable.lambda.functor.Applicative;
66
import com.jnape.palatable.lambda.functor.Bifunctor;
77
import com.jnape.palatable.lambda.monad.Monad;
@@ -18,14 +18,19 @@
1818
* @param <_2> The second slot element type
1919
* @param <_3> The third slot element type
2020
* @param <_4> The fourth slot element type
21+
* @see Product4
2122
* @see HList
2223
* @see SingletonHList
2324
* @see Tuple2
2425
* @see Tuple3
2526
* @see Tuple5
2627
*/
27-
public class Tuple4<_1, _2, _3, _4> extends HCons<_1, Tuple3<_2, _3, _4>>
28-
implements Monad<_4, Tuple4<_1, _2, _3, ?>>, Bifunctor<_3, _4, Tuple4<_1, _2, ?, ?>>, Traversable<_4, Tuple4<_1, _2, _3, ?>> {
28+
public class Tuple4<_1, _2, _3, _4> extends HCons<_1, Tuple3<_2, _3, _4>> implements
29+
Product4<_1, _2, _3, _4>,
30+
Monad<_4, Tuple4<_1, _2, _3, ?>>,
31+
Bifunctor<_3, _4, Tuple4<_1, _2, ?, ?>>,
32+
Traversable<_4, Tuple4<_1, _2, _3, ?>> {
33+
2934
private final _1 _1;
3035
private final _2 _2;
3136
private final _3 _3;
@@ -44,55 +49,26 @@ public <_0> Tuple5<_0, _1, _2, _3, _4> cons(_0 _0) {
4449
return new Tuple5<>(_0, this);
4550
}
4651

47-
/**
48-
* Retrieve the first (head) element in constant time.
49-
*
50-
* @return the head element
51-
*/
52+
@Override
5253
public _1 _1() {
5354
return _1;
5455
}
5556

56-
/**
57-
* Retrieve the second element in constant time.
58-
*
59-
* @return the second element
60-
*/
57+
@Override
6158
public _2 _2() {
6259
return _2;
6360
}
6461

65-
/**
66-
* Retrieve the third element in constant time.
67-
*
68-
* @return the third element
69-
*/
62+
@Override
7063
public _3 _3() {
7164
return _3;
7265
}
7366

74-
/**
75-
* Retrieve the fourth element in constant time.
76-
*
77-
* @return the fourth element
78-
*/
67+
@Override
7968
public _4 _4() {
8069
return _4;
8170
}
8271

83-
/**
84-
* Destructure and apply this tuple to a function accepting the same number of arguments as this tuple's
85-
* slots.
86-
*
87-
* @param fn the function to apply
88-
* @param <R> the return type of the function
89-
* @return the result of applying the destructured tuple to the function
90-
* @see Tuple2#into
91-
*/
92-
public <R> R into(Fn4<? super _1, ? super _2, ? super _3, ? super _4, ? extends R> fn) {
93-
return fn.apply(_1, _2, _3, _4);
94-
}
95-
9672
@Override
9773
@SuppressWarnings("unchecked")
9874
public <_4Prime> Tuple4<_1, _2, _3, _4Prime> fmap(Function<? super _4, ? extends _4Prime> fn) {

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

Lines changed: 13 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.jnape.palatable.lambda.adt.hlist;
22

33
import com.jnape.palatable.lambda.adt.hlist.HList.HCons;
4-
import com.jnape.palatable.lambda.functions.Fn5;
4+
import com.jnape.palatable.lambda.adt.product.Product5;
55
import com.jnape.palatable.lambda.functor.Applicative;
66
import com.jnape.palatable.lambda.functor.Bifunctor;
77
import com.jnape.palatable.lambda.monad.Monad;
@@ -19,14 +19,19 @@
1919
* @param <_3> The third slot element type
2020
* @param <_4> The fourth slot element type
2121
* @param <_5> The fifth slot element type
22+
* @see Product5
2223
* @see HList
2324
* @see SingletonHList
2425
* @see Tuple2
2526
* @see Tuple3
2627
* @see Tuple4
2728
*/
28-
public class Tuple5<_1, _2, _3, _4, _5> extends HCons<_1, Tuple4<_2, _3, _4, _5>>
29-
implements Monad<_5, Tuple5<_1, _2, _3, _4, ?>>, Bifunctor<_4, _5, Tuple5<_1, _2, _3, ?, ?>>, Traversable<_5, Tuple5<_1, _2, _3, _4, ?>> {
29+
public class Tuple5<_1, _2, _3, _4, _5> extends HCons<_1, Tuple4<_2, _3, _4, _5>> implements
30+
Product5<_1, _2, _3, _4, _5>,
31+
Monad<_5, Tuple5<_1, _2, _3, _4, ?>>,
32+
Bifunctor<_4, _5, Tuple5<_1, _2, _3, ?, ?>>,
33+
Traversable<_5, Tuple5<_1, _2, _3, _4, ?>> {
34+
3035
private final _1 _1;
3136
private final _2 _2;
3237
private final _3 _3;
@@ -47,64 +52,31 @@ public <_0> Tuple6<_0, _1, _2, _3, _4, _5> cons(_0 _0) {
4752
return new Tuple6<>(_0, this);
4853
}
4954

50-
/**
51-
* Retrieve the first (head) element in constant time.
52-
*
53-
* @return the head element
54-
*/
55+
@Override
5556
public _1 _1() {
5657
return _1;
5758
}
5859

59-
/**
60-
* Retrieve the second element in constant time.
61-
*
62-
* @return the second element
63-
*/
60+
@Override
6461
public _2 _2() {
6562
return _2;
6663
}
6764

68-
/**
69-
* Retrieve the third element in constant time.
70-
*
71-
* @return the third element
72-
*/
65+
@Override
7366
public _3 _3() {
7467
return _3;
7568
}
7669

77-
/**
78-
* Retrieve the fourth element in constant time.
79-
*
80-
* @return the fourth element
81-
*/
70+
@Override
8271
public _4 _4() {
8372
return _4;
8473
}
8574

86-
/**
87-
* Retrieve the fifth element in constant time.
88-
*
89-
* @return the fifth element
90-
*/
75+
@Override
9176
public _5 _5() {
9277
return _5;
9378
}
9479

95-
/**
96-
* Destructure and apply this tuple to a function accepting the same number of arguments as this tuple's
97-
* slots.
98-
*
99-
* @param fn the function to apply
100-
* @param <R> the return type of the function
101-
* @return the result of applying the destructured tuple to the function
102-
* @see Tuple2#into
103-
*/
104-
public <R> R into(Fn5<? super _1, ? super _2, ? super _3, ? super _4, ? super _5, ? extends R> fn) {
105-
return fn.apply(_1, _2, _3, _4, _5);
106-
}
107-
10880
@Override
10981
public <_5Prime> Tuple5<_1, _2, _3, _4, _5Prime> fmap(Function<? super _5, ? extends _5Prime> fn) {
11082
return Monad.super.<_5Prime>fmap(fn).coerce();

0 commit comments

Comments
 (0)