1Either(3) OCaml library Either(3)
2
3
4
6 Either - Either type.
7
9 Module Either
10
12 Module Either
13 : sig end
14
15
16 Either type.
17
18 Either is the simplest and most generic sum/variant type: a value of
19 ('a, 'b) Either.t is either a Left (v : 'a) or a Right (v : 'b) .
20
21 It is a natural choice in the API of generic functions where values
22 could fall in two different cases, possibly at different types, without
23 assigning a specific meaning to what each case should be.
24
25 For example:
26
27
28 List.partition_map:
29 ('a -> ('b, 'c) Either.t) -> 'a list -> 'b list * 'c list
30
31 If you are looking for a parametrized type where one alternative means
32 success and the other means failure, you should use the more specific
33 type Result.t .
34
35
36 Since 4.12
37
38
39
40
41
42 type ('a, 'b) t =
43 | Left of 'a
44 | Right of 'b
45
46
47 A value of ('a, 'b) Either.t contains either a value of 'a or a value
48 of 'b
49
50
51
52
53 val left : 'a -> ('a, 'b) t
54
55
56 left v is Left v .
57
58
59
60 val right : 'b -> ('a, 'b) t
61
62
63 right v is Right v .
64
65
66
67 val is_left : ('a, 'b) t -> bool
68
69
70 is_left (Left v) is true , is_left (Right v) is false .
71
72
73
74 val is_right : ('a, 'b) t -> bool
75
76
77 is_right (Left v) is false , is_right (Right v) is true .
78
79
80
81 val find_left : ('a, 'b) t -> 'a option
82
83
84 find_left (Left v) is Some v , find_left (Right _) is None
85
86
87
88
89 val find_right : ('a, 'b) t -> 'b option
90
91
92 find_right (Right v) is Some v , find_right (Left _) is None
93
94
95
96
97 val map_left : ('a1 -> 'a2) -> ('a1, 'b) t -> ('a2, 'b) t
98
99
100 map_left f e is Left (f v) if e is Left v and e if e is Right _ .
101
102
103
104 val map_right : ('b1 -> 'b2) -> ('a, 'b1) t -> ('a, 'b2) t
105
106
107 map_right f e is Right (f v) if e is Right v and e if e is Left _ .
108
109
110
111 val map : left:('a1 -> 'a2) -> right:('b1 -> 'b2) -> ('a1, 'b1) t ->
112 ('a2, 'b2) t
113
114
115 map ~left ~right (Left v) is Left (left v) , map ~left ~right (Right v)
116 is Right (right v) .
117
118
119
120 val fold : left:('a -> 'c) -> right:('b -> 'c) -> ('a, 'b) t -> 'c
121
122
123 fold ~left ~right (Left v) is left v , and fold ~left ~right (Right v)
124 is right v .
125
126
127
128 val iter : left:('a -> unit) -> right:('b -> unit) -> ('a, 'b) t ->
129 unit
130
131
132 iter ~left ~right (Left v) is left v , and iter ~left ~right (Right v)
133 is right v .
134
135
136
137 val for_all : left:('a -> bool) -> right:('b -> bool) -> ('a, 'b) t ->
138 bool
139
140
141 for_all ~left ~right (Left v) is left v , and for_all ~left ~right
142 (Right v) is right v .
143
144
145
146 val equal : left:('a -> 'a -> bool) -> right:('b -> 'b -> bool) -> ('a,
147 'b) t -> ('a, 'b) t -> bool
148
149
150 equal ~left ~right e0 e1 tests equality of e0 and e1 using left and
151 right to respectively compare values wrapped by Left _ and Right _ .
152
153
154
155 val compare : left:('a -> 'a -> int) -> right:('b -> 'b -> int) -> ('a,
156 'b) t -> ('a, 'b) t -> int
157
158
159 compare ~left ~right e0 e1 totally orders e0 and e1 using left and
160 right to respectively compare values wrapped by Left _ and Right _ .
161 Left _ values are smaller than Right _ values.
162
163
164
165
166
167OCamldoc 2023-01-23 Either(3)