Skip to content

Instantly share code, notes, and snippets.

@notgiorgi
Last active December 17, 2018 20:12
Show Gist options
  • Save notgiorgi/5a9c8863845b004a8d631d993970f202 to your computer and use it in GitHub Desktop.
Save notgiorgi/5a9c8863845b004a8d631d993970f202 to your computer and use it in GitHub Desktop.
module type Monad = {
type m(_);
let flatMap: m('a) => ('a => m('b)) => m('b);
};
module ListMonad: Monad {
type t('a) = list('a)
let rec flatMap = (ma, f) => switch (ma) {
| [] => []
| [x, ...xs] => List.concat([f(x), flatMap(xs, f)])
}
}
open ListMonad;
Js.log([1,2,3])
Js.log(
[1,2,3]->flatMap(x => [x, x])
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment