You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
take(10, inGroupsOf(3, unfoldr(x ->Optional.of(tuple(x *3, x +1)), 1)));
136
+
//-> [[3, 6, 9], [12, 15, 18], [21, 24, 27]]
136
137
```
137
138
138
139
Check out the [tests](https://github.com/palatable/lambda/tree/master/src/test/java/com/jnape/palatable/lambda/functions/builtin) or [javadoc](http://palatable.github.io/lambda/javadoc/) for more examples.
@@ -149,13 +150,13 @@ HLists are type-safe heterogeneous lists, meaning they can store elements of dif
149
150
The following illustrates how the linear expansion of the recursive type signature for `HList` prevents ill-typed expressions:
Finally, all `Tuple*` classes are instances of both `Functor` and `Bifunctor`:
190
191
191
192
```Java
192
-
Tuple2<Integer, String> mappedTuple2 = tuple(1, 2).biMap(x -> x +1, Object::toString);
193
+
Tuple2<Integer, String> mappedTuple2 = tuple(1, 2).biMap(x -> x +1, Object::toString);
193
194
194
-
System.out.println(mappedTuple2._1()); // prints 2
195
-
System.out.println(mappedTuple2._2()); // prints "2"
195
+
System.out.println(mappedTuple2._1()); // prints 2
196
+
System.out.println(mappedTuple2._2()); // prints "2"
196
197
197
-
Tuple3<String, Boolean, Integer> mappedTuple3 = tuple("foo", true, 1).biMap(x ->!x, x -> x +1);
198
+
Tuple3<String, Boolean, Integer> mappedTuple3 = tuple("foo", true, 1).biMap(x ->!x, x -> x +1);
198
199
199
-
System.out.println(mappedTuple3._1()); // prints "foo"
200
-
System.out.println(mappedTuple3._2()); // prints false
201
-
System.out.println(mappedTuple3._3()); // prints 2
200
+
System.out.println(mappedTuple3._1()); // prints "foo"
201
+
System.out.println(mappedTuple3._2()); // prints false
202
+
System.out.println(mappedTuple3._3()); // prints 2
202
203
```
203
204
204
205
### <aname="hmaps">Heterogeneous Maps</a>
205
206
206
207
HMaps are type-safe heterogeneous maps, meaning they can store mappings to different value types in the same map; however, whereas HLists encode value types in their type signatures, HMaps rely on the keys to encode the value type that they point to.
@@ -223,18 +224,76 @@ Binary tagged unions are represented as `Either<L, R>`s, which resolve to one of
223
224
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.
224
225
225
226
```Java
226
-
Either<String, Integer> right =Either.right(1);
227
-
Either<String, Integer> left =Either.left("Head fell off");
227
+
Either<String, Integer> right =Either.right(1);
228
+
Either<String, Integer> left =Either.left("Head fell off");
228
229
229
-
Boolean successful = right.match(l ->false, r ->true);
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`.
237
238
239
+
<aname="lenses">Lenses</a>
240
+
----
241
+
242
+
Lambda also ships with a first-class <ahref="https://www.youtube.com/watch?v=cefnmjtAolY&feature=youtu.be&hd=1">lens</a> type, as well as a small library of useful general lenses:
set(stringAt0, "quux", strings); // [quux, bar, baz]
250
+
over(stringAt0, s -> s.map(String::toUpperCase).orElse(""), strings); // [FOO, bar, baz]
251
+
```
252
+
253
+
There are three functions that lambda provides that interface directly with lenses: `view`, `over`, and `set`. As the name implies, `view` and `set` are used to retrieve values and store values, respectively, whereas `over` is used to apply a function to the value a lens is focused on, alter it, and store it (you can think of `set` as a specialization of `over` using `constantly`).
254
+
255
+
Lenses can be easily created. Consider the following `Person` class:
Lenses also compose, can have any parameter independently mapped over, and support various other properties that make them interesting and useful. Check out the tests or the javadocs for more info.
0 commit comments