-
Notifications
You must be signed in to change notification settings - Fork 106
/
batOption.ml
218 lines (183 loc) · 4.8 KB
/
batOption.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
(*
* Option - functions for the option type
* Copyright (C) 2003 Nicolas Cannasse
* 2008 David Teller (Contributor)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version,
* with the special exception on linking described in file LICENSE.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*)
##V>=5##module Pervasives = Stdlib
type 'a t = 'a option
let some x = Some x
let may f = function
| None -> ()
| Some v -> f v
(*$T may
let x = ref 3 in may incr (Some x); !x = 4
*)
let map f = function
| None -> None
| Some v -> Some (f v)
(*$T map
map succ None = None
map succ (Some 3) = (Some 4)
*)
let apply = function
| None -> (fun x -> x)
| Some f -> f
(*$T apply
apply None 3 = 3
apply (Some succ) 3 = 4
*)
let filter f = function
| Some x when f x -> Some x
| _ -> None
(*$T filter
filter (fun _ -> true) None = None
filter (fun _ -> true) (Some 3) = Some 3
filter (fun _ -> false) (Some 3) = None
*)
let default v = function
| None -> v
| Some v -> v
(*$T default
default 3 None = 3
default 3 (Some 4) = 4
*)
let default_delayed l = function
| None -> l ()
| Some v -> v
(*$T default_delayed
default_delayed (fun () -> 3) None = 3
default_delayed (fun () -> assert false) (Some 4) = 4
*)
let is_some = function
| None -> false
| _ -> true
(*$T is_some
not (is_some None)
is_some (Some ())
*)
let is_none = function
| None -> true
| _ -> false
(*$T is_none
is_none None
not (is_none (Some ()))
*)
let get_exn s e = match s with
| None -> raise e
| Some v -> v
(*$T get_exn
try get_exn None Exit with Exit -> true
try get_exn (Some true) Exit with Exit -> false
*)
let get s = get_exn s (Invalid_argument "Option.get")
(*$T get
try get None with Invalid_argument _ -> true
try get (Some true) with Invalid_argument _ -> false
*)
let map_default f v = function
| None -> v
| Some v2 -> f v2
(*$T map_default
map_default succ 2 None = 2
map_default succ 2 (Some 3) = 4
*)
let map_default_delayed f l = function
| None -> l ()
| Some v -> f v
(*$T map_default_delayed
map_default_delayed succ (fun () -> 2) None = 2
map_default_delayed succ (fun () -> assert false) (Some 3) = 4
*)
let compare ?(cmp=Pervasives.compare) a b = match a with
None -> (match b with
None -> 0
| Some _ -> -1)
| Some x -> (match b with
None -> 1
| Some y -> cmp x y)
(*$T compare
compare (Some 0) (Some 1) < 0
compare (Some 0) (Some 0) = 0
compare (Some 0) (Some (-1)) > 0
compare None (Some ()) < 0
compare None None = 0
compare (Some ()) None > 0
compare ~cmp:(fun _ _ -> 0) (Some (fun x -> x)) (Some (fun y -> y)) = 0
*)
let eq ?(eq=(=)) x y = match x,y with
| None, None -> true
| Some a, Some b -> eq a b
| _ -> false
(*$T eq
eq ~eq:(fun a b -> (a land 1) = (b land 1)) (Some 1) (Some 3)
eq (Some 3) (None) = false
eq None None = true
*)
let enum = function
| None -> BatEnum.from (fun () -> raise BatEnum.No_more_elements)
| Some e -> BatEnum.singleton e
(*$T enum
BatList.of_enum (enum None) = []
BatList.of_enum (enum (Some 3)) = [3]
*)
let of_enum = BatEnum.get
(*$T of_enum
of_enum (BatList.enum []) = None
let e = BatList.enum [1; 2; 3] in of_enum e = Some 1 && BatList.of_enum e = [2; 3]
*)
open BatOrd
let ord o x y = match x, y with
| None, None -> Eq
| Some x', Some y' -> o x' y'
| Some _, None -> Gt
| None, Some _ -> Lt
(*$T ord
ord BatInt.ord (Some 1) (Some 2) = BatOrd.Lt
ord BatInt.ord (Some 1) None = BatOrd.Gt
*)
let print print_a out = function
| None -> BatInnerIO.nwrite out "None"
| Some x -> BatPrintf.fprintf out "Some %a" print_a x
let maybe_printer a_printer paren out = function
| None -> ()
| Some x -> a_printer paren out x
module Monad =
struct
type 'a m = 'a option
let return x = Some x
let bind m f = match m with
| None -> None
| Some x -> f x
end
let bind = Monad.bind
(*$T bind
bind None (fun s -> Some s) = None
bind (Some ()) (fun s -> Some s) = Some ()
*)
module Labels =
struct
let may ~f o = may f o
let map ~f o = map f o
let map_default ~f d o = map_default f d o
end
module Infix =
struct
let ( |? ) x def = default def x
let (>>=) = Monad.bind
end
include Infix