-
Notifications
You must be signed in to change notification settings - Fork 13
/
stdcompat__either.ml.in
70 lines (56 loc) · 1.23 KB
/
stdcompat__either.ml.in
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
@BEGIN_FROM_4_12_0@
include Either
@END_FROM_4_12_0@
@BEGIN_BEFORE_4_12_0@
type ('a, 'b) t = ('a, 'b) Stdcompat__init.either =
| Left of 'a
| Right of 'b
let left v = Left v
let right v = Right v
let is_left e =
match e with
| Left _ -> true
| Right _ -> false
let is_right e =
match e with
| Left _ -> false
| Right _ -> true
let find_left e =
match e with
| Left v -> Some v
| Right _ -> None
let find_right e =
match e with
| Left _ -> None
| Right v -> Some v
let map_left f e =
match e with
| Left v -> Left (f v)
| Right _ as e -> e
let map_right f e =
match e with
| Left _ as e -> e
| Right v -> Right (f v)
let map ~left ~right e =
match e with
| Left v -> Left (left v)
| Right v -> Right (right v)
let fold ~left ~right e =
match e with
| Left v -> left v
| Right v -> right v
let iter = fold
let for_all = fold
let equal ~left ~right e1 e2 =
match e1, e2 with
| Left v1, Left v2 -> left v1 v2
| Right v1, Right v2 -> right v1 v2
| Left _, Right _
| Right _, Left _ -> false
let compare ~left ~right e1 e2 =
match e1, e2 with
| Left v1, Left v2 -> left v1 v2
| Right v1, Right v2 -> right v1 v2
| Left _, Right _ -> -1
| Right _, Left _ -> 1
@END_BEFORE_4_12_0@