Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 155 lines (122 sloc) 3.421 kB
fccc685 Initial open-source release
MLstate authored
1 (*
2 Copyright © 2011 MLstate
3
4 This file is part of OPA.
5
6 OPA is free software: you can redistribute it and/or modify it under the
7 terms of the GNU Affero General Public License, version 3, as published by
8 the Free Software Foundation.
9
10 OPA is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
13 more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with OPA. If not, see <http://www.gnu.org/licenses/>.
17 *)
18 (*
19 @author Rudy Sicard
20 @author Pascal Rigaux
21 @author Mehdi Bouaziz
22 @author Mathieu Barbin
23 @author François-Régis Sinot
24 @author Henri Binsztok
25 *)
26
27 let default d = function (* in module Base too *)
28 | None -> d
29 | Some e -> e
30
31 let default_lazy d = function (* in module Base too *)
32 | None -> Lazy.force d
33 | Some e -> e
34
35 let is_none = function None -> true | _ -> false (* in module Base too *)
36 let is_some = function None -> false | _ -> true (* in module Base too *)
37
38 let if_none o a b = match o with None -> a | _ -> b
39 let if_some o a b = match o with None -> b | _ -> a
40
41 let get = function
42 | None -> failwith "Option.get"
43 | Some e -> e
44
45 let get_exn exn = function
46 | None -> raise exn
47 | Some e -> e
48
49 let map f = function
50 | None -> None
51 | Some e -> Some (f e)
52
53 let map2 f o1 o2 =
54 match o1, o2 with
55 | Some e1, Some e2 -> Some (f e1 e2)
56 | _ -> None
57
58 let apply o v =
59 match o with
60 | Some f -> f v
61 | None -> v
62
63 let fold f acc = function
64 | None -> acc
65 | Some e -> f acc e
66
67 let fold_right f opt acc =
68 match opt with
69 | None -> acc
70 | Some e -> f e acc
71
72 let foldmap f acc = function
73 | None -> acc, None
74 | Some e ->
75 let acc, e = f acc e in
76 acc, Some e
77
78 let foldmap_stable tra acc opt =
79 match opt with
80 | None -> acc, opt
81 | Some e ->
82 let acc, fe = tra acc e in
83 acc,
84 if e == fe then opt else
85 Some fe
86
87 let iter f = function
88 | None -> ()
89 | Some x -> f x
90
91 let bind f = function
92 | None -> None
93 | Some e -> f e
94
95 let to_list = function
96 | None -> []
97 | Some x -> [x]
98
99 let default_map d f = function
100 | None -> d
101 | Some e -> f e
102
103 let default_lazy_map d f = function
104 | None -> Lazy.force d
105 | Some e -> f e
106
107 (* monadic join -- don't eta-reduce *)
108 let join m = bind (fun x -> x) m
109
110 let make_compare cmp o1 o2 =
111 match o1, o2 with
112 | None, None -> 0
113 | None, Some _ -> -1
114 | Some _, None -> 1
115 | Some x1, Some x2 -> cmp x1 x2
116
117 let merge conflict o1 o2 =
118 match o1, o2 with
675bf67 [enhance] option: add merge in opa and modify ml
Damien Graux authored
119 | _, None -> o1
fccc685 Initial open-source release
MLstate authored
120 | None, Some _ -> o2
121 | Some x1, Some x2 -> Some (conflict x1 x2)
122
123 let to_string fct = function
124 | None -> "None"
125 | Some o -> Printf.sprintf "Some %s" (fct o)
126
127 let pp_none pp fmt = function
128 | None -> Format.pp_print_string fmt "none"
129 | Some o -> pp fmt o
130
131 let pp pp fmt = function
132 | None -> ()
133 | Some o -> pp fmt o
134
135 let pp_sep sep pp fmt = function
136 | None -> ()
137 | Some o ->
138 Format.fprintf fmt sep ;
139 pp fmt o
140
141 let pp_default none pp fmt = function
142 | None -> pp fmt none
143 | Some some -> pp fmt some
144
145 let pp_meta pp fmt = function
146 | None -> Format.fprintf fmt "None"
147 | Some o -> Format.fprintf fmt "Some (%a)" pp o
148
149 let exists p = function
150 | None -> false
151 | Some v -> p v
152 let for_all p = function
153 | None -> true
154 | Some v -> p v
Something went wrong with that request. Please try again.