ligo/src/lib_stdlib/option.ml

46 lines
1.2 KiB
OCaml
Raw Normal View History

2016-09-08 21:13:10 +04:00
(**************************************************************************)
(* *)
2018-02-06 00:17:03 +04:00
(* Copyright (c) 2014 - 2018. *)
2016-09-08 21:13:10 +04:00
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
(* *)
(* All rights reserved. No warranty, explicit or implicit, provided. *)
(* *)
(**************************************************************************)
let map ~f = function
| None -> None
| Some x -> Some (f x)
2016-09-08 21:13:10 +04:00
let apply ~f = function
| None -> None
| Some x -> f x
2018-02-01 20:31:08 +04:00
let (>>=) x f = apply ~f x
let (>>|) x f = map ~f x
let iter ~f = function
| None -> ()
| Some x -> f x
2016-09-08 21:13:10 +04:00
let unopt ~default = function
| None -> default
| Some x -> x
let unopt_map ~f ~default = function
| None -> default
| Some x -> f x
2018-02-01 02:21:54 +04:00
let unopt_exn err = function
| Some x -> x
| _ -> raise err
let first_some a b = match a, b with
| None, None -> None
| None, Some v -> Some v
| Some v, _ -> Some v
2017-12-15 20:00:50 +04:00
let try_with f =
try Some (f ()) with _ -> None
2018-01-19 16:54:39 +04:00
let some x = Some x