-
Notifications
You must be signed in to change notification settings - Fork 5
/
option2.ml
81 lines (63 loc) · 1.11 KB
/
option2.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
let map f =
function
| Some x ->
Some (f x)
| None ->
None
let pure_bind ox f =
map f ox
let bind ox f =
match ox with
| Some x ->
f x
| None ->
None
let and_then f ox =
bind ox f
let guard b =
if b then Some () else None
let sequence xs =
let rec helper acc =
function
| [] ->
Some (List.rev acc)
| head :: tail ->
begin match head with
| Some x ->
helper (x :: acc) tail
| None ->
None
end
in
helper [] xs
let with_default default ox =
match ox with
| Some x ->
x
| None ->
default
let sequence_fst (ox, y) =
match ox with
| Some x ->
Some (x, y)
| None ->
None
let sequence_snd (x, oy) =
match oy with
| Some y ->
Some (x, y)
| None ->
None
let filter pred ox =
match ox with
| Some x ->
if pred x then
Some x
else
None
| None ->
None
module Syntax = struct
let ( let+ ) = pure_bind
let ( let* ) = bind
end