Skip to content

Commit 69d1b25

Browse files
committed
added definition for Kleisli Composition #161
1 parent 83e05ed commit 69d1b25

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

readme.md

+36-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ __Table of Contents__
5555
* [Monoid](#monoid)
5656
* [Monad](#monad)
5757
* [Comonad](#comonad)
58+
* [Kleisi Composition](#kleisi-composition)
5859
* [Applicative Functor](#applicative-functor)
5960
* [Morphism](#morphism)
6061
* [Homomorphism](#homomorphism)
@@ -472,7 +473,7 @@ object.map(x => g(f(x)))
472473
is equivalent to
473474

474475
```js
475-
object.map(f).map(g)
476+
object.map(f).map(g)
476477
```
477478

478479
(`f`, `g` are arbitrary composable functions)
@@ -718,6 +719,40 @@ Extend runs a function on the comonad. The function should return the same type
718719
CoIdentity(1).extend((co) => co.extract() + 1) // CoIdentity(2)
719720
```
720721

722+
## Kleisi Composition
723+
724+
An operation for composing two [monad](#monad)-returning functions (Kleisli Arrows) where they have compatible types. In Haskell this is the `>=>` operator.
725+
726+
Using [Option](#option):
727+
728+
```js
729+
// safeParseNum :: String -> Option Number
730+
const safeParseNum = (b) => {
731+
const n = parseNumber(b)
732+
return isNaN(n) ? none() : some(n)
733+
}
734+
735+
// validatePositive :: Number -> Option Number
736+
const validatePositive = (a) => a > 0 ? some(a) : none()
737+
738+
// kleisliCompose :: Monad M => ((b -> M c), (a -> M b)) -> a -> M c
739+
const kleisliCompose = (g, f) => (x) => f(x).chain(g)
740+
741+
// parseAndValidate :: String -> Option Number
742+
const parseAndValidate = kleisliCompose(validatePositive, safeParseNum)
743+
744+
parseAndValidate('1') // => Some(1)
745+
parseAndValidate('asdf') // => None
746+
parseAndValidate('999') // => None
747+
```
748+
749+
This works because:
750+
751+
* [Option](#option) is a [monad](#monad)
752+
* Both `validatePositive` and `safeParseNum` return the same kind of monad (Option).
753+
* The type of `validatePositive`'s argument matches `safeParseNum`'s unwrapped return.
754+
755+
721756
## Applicative Functor
722757

723758
An applicative functor is an object with an `ap` function. `ap` applies a function in the object to a value in another object of the same type.

0 commit comments

Comments
 (0)