ligo/src/lib_stdlib/option.ml

46 lines
1.2 KiB
OCaml
Raw Normal View History

2016-09-08 19:13:10 +02:00
(**************************************************************************)
(* *)
2018-02-05 21:17:03 +01:00
(* Copyright (c) 2014 - 2018. *)
2016-09-08 19:13:10 +02: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 19:13:10 +02:00
let apply ~f = function
| None -> None
| Some x -> f x
2018-02-01 17:31:08 +01: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 19:13:10 +02:00
let unopt ~default = function
| None -> default
| Some x -> x
let unopt_map ~f ~default = function
| None -> default
| Some x -> f x
2018-01-31 23:21:54 +01: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 17:00:50 +01:00
let try_with f =
try Some (f ()) with _ -> None
2018-01-19 13:54:39 +01:00
let some x = Some x