Skip to content

Commit 993c322

Browse files
authored
Updating README
Adding CoProduct examples and bumping README version info
1 parent 0306a6e commit 993c322

File tree

1 file changed

+37
-7
lines changed

1 file changed

+37
-7
lines changed

README.md

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ Functional patterns for Java 8
1414
- [HLists](#hlists)
1515
- [Tuples](#tuples)
1616
- [HMaps](#hmaps)
17-
- [Either](#either)
17+
- [CoProducts](#coproducts)
18+
- [Either](#either)
1819
- [Lenses](#lenses)
1920
- [Notes](#notes)
2021
- [License](#license)
@@ -47,14 +48,14 @@ Add the following dependency to your:
4748
<dependency>
4849
<groupId>com.jnape.palatable</groupId>
4950
<artifactId>lambda</artifactId>
50-
<version>1.5.2</version>
51+
<version>1.5.3</version>
5152
</dependency>
5253
```
5354

5455
`build.gradle` ([Gradle](https://docs.gradle.org/current/userguide/dependency_management.html)):
5556

5657
```gradle
57-
compile group: 'com.jnape.palatable', name: 'lambda', version: '1.5.2'
58+
compile group: 'com.jnape.palatable', name: 'lambda', version: '1.5.3'
5859
```
5960

6061

@@ -234,18 +235,47 @@ Optional<Integer> intValue = hmap.get(intKey); // Optional[1]
234235
Optional<Integer> anotherIntValue = hmap.get(anotherIntKey); // Optional.empty
235236
```
236237

238+
### <a name="coproducts">CoProducts</a>
239+
240+
`CoProduct`s generalize unions of disparate types in a single consolidated type.
241+
242+
```Java
243+
CoProduct3<String, Integer, Character> string = CoProduct3.a("string");
244+
CoProduct3<String, Integer, Character> integer = CoProduct3.b(1);
245+
CoProduct3<String, Integer, Character> character = CoProduct3.c('a');
246+
```
247+
248+
Rather than supporting explicit value unwrapping, which would necessarily jeopardize type safety, `CoProduct`s support a `match` method that takes one function per possible value type and maps it to a final common result type:
249+
250+
```Java
251+
CoProduct3<String, Integer, Character> string = CoProduct3.a("string");
252+
CoProduct3<String, Integer, Character> integer = CoProduct3.b(1);
253+
CoProduct3<String, Integer, Character> character = CoProduct3.c('a');
254+
255+
Integer result = string.<Integer>match(String::length, identity(), Character::charCount); // 6
256+
```
257+
258+
Additionally, because a `CoProduct2<A, B>` guarantees a subset of a `CoProduct3<A, B, C>`, the `diverge` method exists between `CoProduct` types of single magnitude differences to make it easy to use a more convergent `CoProduct` where a more divergent `CoProduct` is expected:
259+
260+
```Java
261+
CoProduct2<String, Integer> coProduct2 = CoProduct2.a("string");
262+
CoProduct3<String, Integer, Character> coProduct3 = coProduct2.diverge(); // still just the coProduct2 value, adapted to the coProduct3 shape
263+
```
264+
265+
There are `CoProduct` specializations for type unions of up to 5 different types: `CoProduct2` through `CoProduct5`, respectively.
266+
237267
### <a name="either">Either</a>
238268

239-
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).
269+
`Either<L, R>` represents a specialized `CoProduct2<L, R>`, 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).
240270

241-
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.
271+
As with `CoProduct2`, rather than supporting explicit value unwrapping, `Either` supports many useful comprehensions to help facilitate type-safe interactions:
242272

243273
```Java
244274
Either<String, Integer> right = Either.right(1);
245275
Either<String, Integer> left = Either.left("Head fell off");
246276

247-
Boolean successful = right.match(l -> false, r -> true);
248-
//-> true
277+
Integer result = right.orElse(-1);
278+
//-> 1
249279

250280
List<Integer> values = left.match(l -> Collections.emptyList(), Collections::singletonList);
251281
//-> []

0 commit comments

Comments
 (0)