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
@@ -149,7 +149,13 @@ Check out the [tests](https://github.com/palatable/lambda/tree/master/src/test/j
149
149
----
150
150
151
151
[Semigroups](https://en.wikipedia.org/wiki/Semigroup) are supported via `Semigroup<A>`, a subtype of `Fn2<A,A,A>`, and add left and right folds over an `Iterable<A>`.
Lambda ships some default logical semigroups for lambda types and core JDK types. Common examples are:
154
160
155
161
-`AddAll` for concatenating two `Collection`s
@@ -161,7 +167,14 @@ Check out the [semigroup](https://palatable.github.io/lambda/javadoc/com/jnape/p
161
167
<aname="monoids">Monoids</a>
162
168
----
163
169
164
-
[Monoids](https://en.wikipedia.org/wiki/Monoid) are supported via `Monoid<A>`, a subtype of `Semigroup<A>` with an `A #identity()` method, and add left and right reduces over an `Iterable<A>`.
170
+
[Monoids](https://en.wikipedia.org/wiki/Monoid) are supported via `Monoid<A>`, a subtype of `Semigroup<A>` with an `A #identity()` method, and add left and right reduces over an `Iterable<A>`, as well as `foldMap`.
171
+
172
+
```Java
173
+
Monoid<Integer> multiply = monoid((x, y) -> x * y, 1);
174
+
multiple.reduceLeft(emptyList()); //-> 1
175
+
multiply.reduceLeft(asList(1, 2, 3)); //-> 6
176
+
multiply.foldMap(Integer::parseInt, asList("1", "2", "3")); //-> also 6
177
+
```
165
178
166
179
Some commonly used lambda monoid implementations include:
167
180
@@ -179,6 +192,29 @@ Check out the [monoid](https://palatable.github.io/lambda/javadoc/com/jnape/pala
179
192
180
193
Functors are implemented via the `Functor` interface, and are sub-typed by every function type that lambda exports, as well as many of the [ADTs](#adts).
Profunctors -- functors that support one parameter that can be mapped over contravariantly, and a second parameter that can be mapped over covariantly -- are implemented via the `Profunctor` interface.
Implementing `Profunctor` requires implementing *either*`diMapL` and `diMapR`*or*`diMap`. As with `Functor` and `Bifunctor`, there are [some laws](https://hackage.haskell.org/package/profunctors-5.2/docs/Data-Profunctor.html) that well behaved instances of `Profunctor` should adhere to.
216
288
217
289
### <aname="applicatives">Applicatives</a>
218
290
219
291
Applicative functors -- functors that can be applied together with a 2-arity or higher function -- are implemented via the `Applicative` interface.
Slot<Integer> z = y.zip(x.fmap(add)); //-> Slot{a=3}
325
+
```
326
+
221
327
Examples of applicative functors include:
222
328
223
329
-`Fn*`, `Semigroup`, and `Monoid`
@@ -227,16 +333,74 @@ Examples of applicative functors include:
227
333
-`Const`, `Identity`, and `Compose`
228
334
-`Lens`
229
335
230
-
In addition to implementing `fmap` from `Functor`, implementing an applicative functor involves providing two methods: `pure`, a method that lifts a value into the functor; and `zip`, a method that applies a lifted function to a lifted value, returning a new lifted value. As usual, there are [some laws](https://hackage.haskell.org/package/base-4.9.1.0/docs/Control-Applicative.html) that should be adhered to.
231
-
232
-
For example, imagine we have an `Fn2<String,String,String>` we'll call `join` that simply joins two strings. If we wanted to join the `String`s wrapped inside two `Either<Integer,String>` instances, without `Applicative`, we would at best only be able to `fmap` one while `fmap`ping the other, resulting in an `Either<Integer, Either<Integer,String>>`.
233
-
234
-
With `Applicative`, however, we can `fmap(join)` the first `Either<Integer String>`, resulting in an `Either<Integer, Fn1<String, String>>`, and then `zip` the second `Either<Integer, String>`, producing another `Either<Integer, String>`. In this way, `Applicative` can be thought of as facilitating `fmap`ping a functor with a multi-arity function.
336
+
In addition to implementing `fmap` from `Functor`, implementing an applicative functor involves providing two methods: `pure`, a method that lifts a value into the functor; and `zip`, a method that applies a lifted function to a lifted value, returning a new lifted value. As usual, there are [some laws](https://hackage.haskell.org/package/base-4.9.1.0/docs/Control-Applicative.html) that should be adhered to.
235
337
236
338
### <aname="traversables">Traversables</a>
237
339
238
340
Traversable functors -- functors that can be "traversed from left to right" -- are implemented via the `Traversable` interface.
@@ -367,7 +531,7 @@ Integer result = string.<Integer>match(String::length, identity(), Character::ch
367
531
```
368
532
369
533
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:
0 commit comments