Source file CCOption.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
219
220
221
222
223
224
225
226
(* This file is free software, part of containers. See file "license" for more details. *)

(** {1 Options} *)

include Option

let map_or ~default f = function
  | None -> default
  | Some x -> f x

let map_lazy default_fn f = function
  | None -> default_fn ()
  | Some x -> f x

let return x = Some x

let[@inline] flat_map f o =
  match o with
  | None -> None
  | Some x -> f x

let[@inline] flat_map_l f o =
  match o with
  | None -> []
  | Some x -> f x

let ( >>= ) = bind
let pure x = Some x
let k_compose f g x = f x |> flat_map g
let ( >=> ) = k_compose
let ( <=< ) f g = g >=> f

let ( <*> ) f x =
  match f, x with
  | None, _ | _, None -> None
  | Some f, Some x -> Some (f x)

let or_ ~else_ a =
  match a with
  | None -> else_
  | Some _ -> a

let or_lazy ~else_ a =
  match a with
  | None -> else_ ()
  | Some _ -> a

let ( <+> ) a b = or_ ~else_:b a
let choice l = List.fold_left ( <+> ) None l

let map2 f o1 o2 =
  match o1, o2 with
  | None, _ | _, None -> None
  | Some x, Some y -> Some (f x y)

let filter p = function
  | Some x as o when p x -> o
  | _ -> None

let if_ p x =
  if p x then
    Some x
  else
    None

let exists p = function
  | None -> false
  | Some x -> p x

let for_all p = function
  | None -> true
  | Some x -> p x

let fold f acc o =
  match o with
  | None -> acc
  | Some x -> f acc x

let get_or ~default x =
  match x with
  | None -> default
  | Some y -> y

let apply_or f x =
  match f x with
  | None -> x
  | Some y -> y

let ( |?> ) x f = apply_or f x

let get_exn = function
  | Some x -> x
  | None -> invalid_arg "CCOption.get_exn"

let get_exn_or msg = function
  | Some x -> x
  | None -> invalid_arg msg

let get_lazy default_fn x =
  match x with
  | None -> default_fn ()
  | Some y -> y

let sequence_l l =
  let rec aux acc l =
    match l with
    | [] -> Some (List.rev acc)
    | Some x :: l' -> aux (x :: acc) l'
    | None :: _ -> raise Exit
  in
  try aux [] l with Exit -> None

let wrap ?(handler = fun _ -> true) f x =
  try Some (f x)
  with e ->
    if handler e then
      None
    else
      raise e

let wrap2 ?(handler = fun _ -> true) f x y =
  try Some (f x y)
  with e ->
    if handler e then
      None
    else
      raise e

let of_list = function
  | x :: _ -> Some x
  | [] -> None

let to_result err = function
  | None -> Error err
  | Some x -> Ok x

let to_result_lazy err_fn = function
  | None -> Error (err_fn ())
  | Some x -> Ok x

let of_result = function
  | Error _ -> None
  | Ok x -> Some x

module Infix = struct
  let ( >|= ) x f = map f x
  let ( >>= ) = ( >>= )
  let ( <*> ) = ( <*> )
  let ( <$> ) = map
  let ( <+> ) = ( <+> )
  let ( |?> ) = ( |?> )
  let ( let+ ) = ( >|= )
  let ( let* ) = ( >>= )

  let[@inline] ( and+ ) o1 o2 =
    match o1, o2 with
    | Some x, Some y -> Some (x, y)
    | _ -> None

  let ( and* ) = ( and+ )
  let ( >=> ) = ( >=> )
  let ( <=< ) = ( <=< )
end

include Infix

type 'a iter = ('a -> unit) -> unit
type 'a gen = unit -> 'a option
type 'a printer = Format.formatter -> 'a -> unit
type 'a random_gen = Random.State.t -> 'a

let random g st =
  if Random.State.bool st then
    Some (g st)
  else
    None

exception ExitChoice

let choice_iter s =
  let r = ref None in
  (try
     s (function
       | None -> ()
       | Some _ as o ->
         r := o;
         raise ExitChoice)
   with ExitChoice -> ());
  !r

let rec choice_seq s =
  match s () with
  | Seq.Nil -> None
  | Seq.Cons (Some x, _) -> Some x
  | Seq.Cons (None, tl) -> choice_seq tl

let to_gen o =
  match o with
  | None -> fun () -> None
  | Some _ ->
    let first = ref true in
    fun () ->
      if !first then (
        first := false;
        o
      ) else
        None

let to_iter o k =
  match o with
  | None -> ()
  | Some x -> k x

let pp ppx out = function
  | None -> Format.pp_print_string out "None"
  | Some x -> Format.fprintf out "@[Some %a@]" ppx x

let flatten = function
  | Some x -> x
  | None -> None

let return_if b x =
  if b then
    Some x
  else
    None