Skip to content
This repository
tag: v1379
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 155 lines (122 sloc) 3.421 kb
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
(*
Copyright © 2011 MLstate

This file is part of OPA.

OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.

OPA 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 Affero General Public License for
more details.

You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*)
(*
@author Rudy Sicard
@author Pascal Rigaux
@author Mehdi Bouaziz
@author Mathieu Barbin
@author François-Régis Sinot
@author Henri Binsztok
*)

let default d = function (* in module Base too *)
  | None -> d
  | Some e -> e

let default_lazy d = function (* in module Base too *)
  | None -> Lazy.force d
  | Some e -> e

let is_none = function None -> true | _ -> false (* in module Base too *)
let is_some = function None -> false | _ -> true (* in module Base too *)

let if_none o a b = match o with None -> a | _ -> b
let if_some o a b = match o with None -> b | _ -> a

let get = function
  | None -> failwith "Option.get"
  | Some e -> e

let get_exn exn = function
  | None -> raise exn
  | Some e -> e

let map f = function
  | None -> None
  | Some e -> Some (f e)

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

let apply o v =
  match o with
  | Some f -> f v
  | None -> v

let fold f acc = function
  | None -> acc
  | Some e -> f acc e

let fold_right f opt acc =
  match opt with
  | None -> acc
  | Some e -> f e acc

let foldmap f acc = function
  | None -> acc, None
  | Some e ->
      let acc, e = f acc e in
      acc, Some e

let foldmap_stable tra acc opt =
  match opt with
  | None -> acc, opt
  | Some e ->
      let acc, fe = tra acc e in
      acc,
      if e == fe then opt else
        Some fe

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

let bind f = function
  | None -> None
  | Some e -> f e

let to_list = function
  | None -> []
  | Some x -> [x]

let default_map d f = function
  | None -> d
  | Some e -> f e

let default_lazy_map d f = function
  | None -> Lazy.force d
  | Some e -> f e

(* monadic join -- don't eta-reduce *)
let join m = bind (fun x -> x) m

let make_compare cmp o1 o2 =
  match o1, o2 with
  | None, None -> 0
  | None, Some _ -> -1
  | Some _, None -> 1
  | Some x1, Some x2 -> cmp x1 x2

let merge conflict o1 o2 =
  match o1, o2 with
  | _, None -> o1
  | None, Some _ -> o2
  | Some x1, Some x2 -> Some (conflict x1 x2)

let to_string fct = function
  | None -> "None"
  | Some o -> Printf.sprintf "Some %s" (fct o)

let pp_none pp fmt = function
  | None -> Format.pp_print_string fmt "none"
  | Some o -> pp fmt o

let pp pp fmt = function
  | None -> ()
  | Some o -> pp fmt o

let pp_sep sep pp fmt = function
  | None -> ()
  | Some o ->
      Format.fprintf fmt sep ;
      pp fmt o

let pp_default none pp fmt = function
  | None -> pp fmt none
  | Some some -> pp fmt some

let pp_meta pp fmt = function
  | None -> Format.fprintf fmt "None"
  | Some o -> Format.fprintf fmt "Some (%a)" pp o

let exists p = function
  | None -> false
  | Some v -> p v
let for_all p = function
  | None -> true
  | Some v -> p v
Something went wrong with that request. Please try again.