Skip to content

Commit 79e7ac2

Browse files
committed
Adding Either<L,R>, a BiFunctor supporting type intersections
- removed LoanShark dependency - cleaned up spacing in README - added adt package, and moving tuples under that - BiFunctor now extends Functor as it should
1 parent 033006d commit 79e7ac2

34 files changed

Lines changed: 566 additions & 130 deletions

‎README.md‎

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@ Add the following dependency to your:
1111

1212
`pom.xml` ([Maven](https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html)):
1313

14-
```xml
14+
```xml
1515
<dependency>
1616
<groupId>com.jnape.palatable</groupId>
1717
<artifactId>lambda</artifactId>
1818
<version>1.0</version>
1919
</dependency>
20-
```
20+
```
2121

22-
`build.gradle` ([Gradle](https://docs.gradle.org/current/userguide/dependency_management.html)):
22+
`build.gradle` ([Gradle](https://docs.gradle.org/current/userguide/dependency_management.html)):
2323

24-
```gradle
24+
```gradle
2525
compile group: 'com.jnape.palatable', name: 'lambda', version: '1.0'
26-
```
26+
```
2727

2828
Background
2929
----------
@@ -122,7 +122,67 @@ And have fun with 3s:
122122

123123
Or check out [the tests](https://github.com/palatable/lambda/tree/master/src/test/java/com/jnape/palatable/lambda/functions/builtin) for more examples.
124124

125-
license
125+
ADTs
126+
----
127+
128+
In addition to the functions above, Lambda also supports a few first-class [algebraic data types](https://www.wikiwand.com/en/Algebraic_data_type).
129+
130+
### Tuples
131+
132+
There are two parametric variants on the tuple product type, `Tuple2<_1, _2>` and `Tuple3<_1, _2, _3>`, both immutable.
133+
134+
```Java
135+
Tuple2<String, Integer> tuple2 = Tuple2.tuple("foo", 1);
136+
137+
System.out.println(tuple2._1); // prints "foo"
138+
System.out.println(tuple2._2); // prints 1
139+
```
140+
141+
```Java
142+
Tuple3<String, Boolean, Integer> tuple3 = Tuple3.tuple("foo", true, 1);
143+
144+
System.out.println(tuple3._1); // prints "foo"
145+
System.out.println(tuple3._2); // prints true
146+
System.out.println(tuple3._3); // prints 1
147+
```
148+
149+
Both `Tuple2` and `Tuple3` are `BiFunctor`s over `_1` and `_2`:
150+
151+
```Java
152+
Tuple2<String, Integer> mappedTuple2 = tuple2.biMap(String::toUpperCase, x -> x + 1);
153+
154+
System.out.println(mappedTuple2._1); // prints "FOO"
155+
System.out.println(mappedTuple2._2); // prints 2
156+
```
157+
158+
```Java
159+
Tuple3<String, Boolean, Integer> mappedTuple3 = tuple3.biMap(String::toUpperCase, b -> !b);
160+
161+
System.out.println(mappedTuple3._1); // prints "FOO"
162+
System.out.println(mappedTuple3._2); // prints false
163+
System.out.println(mappedTuple3._3); // prints 1
164+
```
165+
166+
### Either
167+
168+
Binary tagged unions are represented as `Either<L, R>`s, which resolve to one of two possible values: a `Left` value wrapping an `L`, or a `Right` value wrapping an `R` (typically an exceptional value or a successful value, respectively).
169+
170+
Rather than supporting explicit value unwrapping, `Either` supports many useful comprehensions to help facilitate type-safe interactions. For example, `Either#match` is used to resolve an `Either<L,R>` to a different type.
171+
172+
```Java
173+
Either<String, Integer> right = Either.right(1);
174+
Either<String, Integer> left = Either.left("Head fell off");
175+
176+
Boolean successful = right.match(l -> false, r -> true);
177+
//-> true
178+
179+
List<Integer> values = left.match(l -> Collections.emptyList(), Collections::singletonList);
180+
//-> []
181+
```
182+
183+
Check out the tests for [more examples](https://github.com/palatable/lambda/blob/master/src/test/java/com/jnape/palatable/lambda/adt/EitherTest.java) of ways to interact with `Either`.
184+
185+
License
126186
-------
127187

128188
_lambda_ is part of [palatable](http://www.github.com/palatable), which is distributed under [The MIT License](http://choosealicense.com/licenses/mit/).

‎pom.xml‎

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

4545
<properties>
4646
<commons-lang3.version>3.1</commons-lang3.version>
47-
<loan-shark.version>1.0</loan-shark.version>
4847
<traitor.version>1.0</traitor.version>
4948
<maven-compiler-plugin.version>3.3</maven-compiler-plugin.version>
5049
</properties>
@@ -58,12 +57,6 @@
5857
<groupId>org.mockito</groupId>
5958
<artifactId>mockito-all</artifactId>
6059
</dependency>
61-
<dependency>
62-
<groupId>com.jnape.loan-shark</groupId>
63-
<artifactId>loan-shark</artifactId>
64-
<version>${loan-shark.version}</version>
65-
<scope>provided</scope>
66-
</dependency>
6760
<dependency>
6861
<groupId>com.jnape.palatable</groupId>
6962
<artifactId>traitor</artifactId>
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
package com.jnape.palatable.lambda.adt;
2+
3+
import com.jnape.palatable.lambda.applicative.BiFunctor;
4+
import com.jnape.palatable.lambda.applicative.Functor;
5+
import com.jnape.palatable.lambda.functions.DyadicFunction;
6+
import com.jnape.palatable.lambda.functions.MonadicFunction;
7+
8+
import java.util.Objects;
9+
import java.util.Optional;
10+
import java.util.function.Supplier;
11+
12+
public interface Either<L, R> extends Functor<R>, BiFunctor<L, R> {
13+
14+
static <L, R> Either<L, R> either(Optional<R> optional, Supplier<L> leftFn) {
15+
return optional.<Either<L, R>>map(Either::right)
16+
.orElse(left(leftFn.get()));
17+
}
18+
19+
default R or(R defaultValue) {
20+
return recover(l -> defaultValue);
21+
}
22+
23+
R recover(MonadicFunction<? super L, ? extends R> recoveryFn);
24+
25+
<E extends RuntimeException> R orThrow(MonadicFunction<? super L, ? extends E> throwableFn) throws E;
26+
27+
Either<L, R> filter(MonadicFunction<? super R, Boolean> pred,
28+
Supplier<L> leftSupplier);
29+
30+
<L2, R2> Either<L2, R2> flatMap(MonadicFunction<? super L, ? extends Either<L2, R2>> leftFn,
31+
MonadicFunction<? super R, ? extends Either<L2, R2>> rightFn);
32+
33+
Either<L, R> merge(DyadicFunction<? super L, ? super L, ? extends L> leftFn,
34+
DyadicFunction<? super R, ? super R, ? extends R> rightFn,
35+
Either<L, R> other);
36+
37+
<V> V match(MonadicFunction<? super L, ? extends V> leftFn,
38+
MonadicFunction<? super R, ? extends V> rightFn);
39+
40+
@Override
41+
default <R2> Either<L, R2> fmap(MonadicFunction<? super R, ? extends R2> fn) {
42+
return biMapR(fn);
43+
}
44+
45+
@Override
46+
default <L2> Either<L2, R> biMapL(MonadicFunction<? super L, ? extends L2> fn) {
47+
return (Either<L2, R>) BiFunctor.super.biMapL(fn);
48+
}
49+
50+
@Override
51+
default <R2> Either<L, R2> biMapR(MonadicFunction<? super R, ? extends R2> fn) {
52+
return (Either<L, R2>) BiFunctor.super.biMapR(fn);
53+
}
54+
55+
@Override
56+
<L2, R2> Either<L2, R2> biMap(MonadicFunction<? super L, ? extends L2> leftFn,
57+
MonadicFunction<? super R, ? extends R2> rightFn);
58+
59+
static <L, R> Either<L, R> left(L l) {
60+
return new Left<>(l);
61+
}
62+
63+
static <L, R> Either<L, R> right(R r) {
64+
return new Right<>(r);
65+
}
66+
67+
final class Left<L, R> implements Either<L, R> {
68+
private final L l;
69+
70+
private Left(L l) {
71+
this.l = l;
72+
}
73+
74+
@Override
75+
public R recover(MonadicFunction<? super L, ? extends R> recoveryFn) {
76+
return recoveryFn.apply(l);
77+
}
78+
79+
@Override
80+
public <E extends RuntimeException> R orThrow(MonadicFunction<? super L, ? extends E> throwableFn) throws E {
81+
throw throwableFn.apply(l);
82+
}
83+
84+
@Override
85+
public Either<L, R> filter(MonadicFunction<? super R, Boolean> pred, Supplier<L> leftSupplier) {
86+
return this;
87+
}
88+
89+
@Override
90+
public <L2, R2> Either<L2, R2> flatMap(MonadicFunction<? super L, ? extends Either<L2, R2>> leftFn,
91+
MonadicFunction<? super R, ? extends Either<L2, R2>> rightFn) {
92+
return leftFn.apply(l);
93+
}
94+
95+
@Override
96+
public Either<L, R> merge(DyadicFunction<? super L, ? super L, ? extends L> leftFn,
97+
DyadicFunction<? super R, ? super R, ? extends R> rightFn, Either<L, R> other) {
98+
return other instanceof Left
99+
? left(leftFn.apply(l, ((Left<L, R>) other).l))
100+
: this;
101+
}
102+
103+
@Override
104+
public <V> V match(MonadicFunction<? super L, ? extends V> leftFn,
105+
MonadicFunction<? super R, ? extends V> rightFn) {
106+
return leftFn.apply(l);
107+
}
108+
109+
@Override
110+
public <L2, R2> Either<L2, R2> biMap(MonadicFunction<? super L, ? extends L2> leftFn,
111+
MonadicFunction<? super R, ? extends R2> rightFn) {
112+
return left(leftFn.apply(l));
113+
}
114+
115+
@Override
116+
public boolean equals(Object other) {
117+
return other instanceof Left && Objects.equals(l, ((Left) other).l);
118+
}
119+
120+
@Override
121+
public int hashCode() {
122+
return Objects.hash(l);
123+
}
124+
125+
@Override
126+
public String toString() {
127+
return "Left{" +
128+
"l=" + l +
129+
'}';
130+
}
131+
}
132+
133+
final class Right<L, R> implements Either<L, R> {
134+
private final R r;
135+
136+
private Right(R r) {
137+
this.r = r;
138+
}
139+
140+
@Override
141+
public R recover(MonadicFunction<? super L, ? extends R> recoveryFn) {
142+
return r;
143+
}
144+
145+
@Override
146+
public <E extends RuntimeException> R orThrow(MonadicFunction<? super L, ? extends E> throwableFn) throws E {
147+
return r;
148+
}
149+
150+
@Override
151+
public Either<L, R> filter(MonadicFunction<? super R, Boolean> pred, Supplier<L> leftSupplier) {
152+
return pred.apply(r) ? this : left(leftSupplier.get());
153+
}
154+
155+
@Override
156+
public <L2, R2> Either<L2, R2> flatMap(MonadicFunction<? super L, ? extends Either<L2, R2>> leftFn,
157+
MonadicFunction<? super R, ? extends Either<L2, R2>> rightFn) {
158+
return rightFn.apply(r);
159+
}
160+
161+
@Override
162+
public Either<L, R> merge(DyadicFunction<? super L, ? super L, ? extends L> leftFn,
163+
DyadicFunction<? super R, ? super R, ? extends R> rightFn, Either<L, R> other) {
164+
return other instanceof Right
165+
? right(rightFn.apply(r, ((Right<L, R>) other).r))
166+
: other;
167+
}
168+
169+
@Override
170+
public <V> V match(MonadicFunction<? super L, ? extends V> leftFn,
171+
MonadicFunction<? super R, ? extends V> rightFn) {
172+
return rightFn.apply(r);
173+
}
174+
175+
@Override
176+
public <L2, R2> Either<L2, R2> biMap(MonadicFunction<? super L, ? extends L2> leftFn,
177+
MonadicFunction<? super R, ? extends R2> rightFn) {
178+
return right(rightFn.apply(r));
179+
}
180+
181+
@Override
182+
public boolean equals(Object other) {
183+
return other instanceof Right && Objects.equals(r, ((Right) other).r);
184+
}
185+
186+
@Override
187+
public int hashCode() {
188+
return Objects.hash(r);
189+
}
190+
191+
@Override
192+
public String toString() {
193+
return "Right{" +
194+
"r=" + r +
195+
'}';
196+
}
197+
}
198+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.jnape.palatable.lambda.adt.tuples;
2+
3+
import com.jnape.palatable.lambda.applicative.BiFunctor;
4+
import com.jnape.palatable.lambda.functions.MonadicFunction;
5+
6+
import java.util.Objects;
7+
8+
import static java.lang.String.format;
9+
10+
public class Tuple2<_1, _2> implements BiFunctor<_1, _2> {
11+
12+
public final _1 _1;
13+
public final _2 _2;
14+
15+
public Tuple2(_1 _1, _2 _2) {
16+
this._1 = _1;
17+
this._2 = _2;
18+
}
19+
20+
@Override
21+
public <_2A> Tuple2<_1, _2A> fmap(MonadicFunction<? super _2, ? extends _2A> fn) {
22+
return (Tuple2<_1, _2A>) BiFunctor.super.fmap(fn);
23+
}
24+
25+
@Override
26+
public <_1A, _2A> Tuple2<_1A, _2A> biMap(MonadicFunction<? super _1, ? extends _1A> f1,
27+
MonadicFunction<? super _2, ? extends _2A> f2) {
28+
return tuple(f1.apply(_1), f2.apply(_2));
29+
}
30+
31+
@Override
32+
public <_1A> Tuple2<_1A, _2> biMapL(MonadicFunction<? super _1, ? extends _1A> fn) {
33+
return (Tuple2<_1A, _2>) BiFunctor.super.biMapL(fn);
34+
}
35+
36+
@Override
37+
public <_2A> Tuple2<_1, _2A> biMapR(MonadicFunction<? super _2, ? extends _2A> fn) {
38+
return (Tuple2<_1, _2A>) BiFunctor.super.biMapR(fn);
39+
}
40+
41+
@Override
42+
public String toString() {
43+
return format("(%s, %s)", _1, _2);
44+
}
45+
46+
@Override
47+
public boolean equals(Object o) {
48+
if (this == o) return true;
49+
if (o == null || getClass() != o.getClass()) return false;
50+
Tuple2<?, ?> tuple2 = (Tuple2<?, ?>) o;
51+
return Objects.equals(_1, tuple2._1) &&
52+
Objects.equals(_2, tuple2._2);
53+
}
54+
55+
@Override
56+
public int hashCode() {
57+
return Objects.hash(_1, _2);
58+
}
59+
60+
public static <_1, _2> Tuple2<_1, _2> tuple(_1 _1, _2 _2) {
61+
return new Tuple2<>(_1, _2);
62+
}
63+
}

0 commit comments

Comments
 (0)