-
Notifications
You must be signed in to change notification settings - Fork 2
/
Traversable.re
41 lines (38 loc) · 1.01 KB
/
Traversable.re
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
open Applicative;
module type Traversable = {
type t('a);
module Applicative: Applicative;
let traverse: ('a => Applicative.t('b), t('a)) => Applicative.t(t('b));
};
module ListTraversable =
(A: Applicative)
: (
Traversable with
type t('a) = list('a) and type Applicative.t('a) = A.t('a)
) => {
type t('a) = list('a);
module Applicative = A;
module AppU = ApplicativeUtils(Applicative);
open AppU;
let rec traverse = (f, xs) =>
switch (xs) {
| [] => A.pure([])
| [x, ...xs] => ((y, ys) => [y, ...ys]) <$> f(x) <*> traverse(f, xs)
};
};
module OptionTraversable =
(A: Applicative)
: (
Traversable with
type t('a) = option('a) and type Applicative.t('a) = A.t('a)
) => {
type t('a) = option('a);
module Applicative = A;
module AppU = ApplicativeUtils(Applicative);
open AppU;
let traverse = (f, x) =>
switch (x) {
| None => A.pure(None)
| Some(x) => (a => Some(a)) <$> f(x)
};
};