Source file ext_option.ml
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
let bind x f = match x with
| Some x -> f x
| None -> None
let fmap f = function
| Some x -> Some (f x)
| None -> None
let either f default = function
| Some x -> f x
| None -> default
let iter f = either f ()
let default value = function
| None -> value
| Some x -> x
let lazy_default suspended = function
| None -> Lazy.force suspended
| Some x -> x
let (|||) x y = match x with
| None -> Lazy.force y
| Some _ as x -> x
let join = function
| Some x -> x
| None -> None
let (>>|) x f= fmap f x
let (>>=) = bind
let (>>) x y = match x with
| None -> None
| Some _ -> y
let (><) opt def = default def opt
let (&&) x y = match x,y with
| Some x, Some y -> Some (x,y)
| _ -> None
let mcons list x =
match x with Some x -> x :: list | None -> list
module List' = struct
let filter l = List.fold_left mcons [] l
let rec join list = match list with
| [] -> Some []
| None :: _ -> None
| Some x :: q ->
q |> join >>| (fun q -> x :: q)
let rec map f list = match list with
| [] -> Some []
| x :: q ->
f x >>= fun x ->
map f q >>| fun q ->
x :: q
end