Skip to content

Instantly share code, notes, and snippets.

@steinuil
Created May 10, 2018 09:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steinuil/bdc233cc9fe7f92855227c8bd9315f1b to your computer and use it in GitHub Desktop.
Save steinuil/bdc233cc9fe7f92855227c8bd9315f1b to your computer and use it in GitHub Desktop.
Option module
let get = function
| Some x -> x
| None -> raise Not_found
let with_default def = function
| Some x -> x
| None -> def
let map f = function
| Some x -> Some (f x)
| None -> None
let bind f = function
| Some x -> f x
| None -> None
let apply f = function
| Some x -> f x
| None -> ()
let with_default_lazy def = function
| Some x -> x
| None -> Lazy.force def
let or_lazy y x = match x with
| Some _ -> x
| None -> Lazy.force y
let of_result = function
| Ok v -> Some v
| Error _ -> None
let to_result msg = function
| Some v -> Ok v
| None -> Error msg
val get : 'a option -> 'a
(** Unwrap the option.
@raise Not_found if the argument is [None]. *)
val with_default : 'a -> 'a option -> 'a
(** [with_default def (Some x)] is [x],
* [with_default def None] is [def]. *)
val map : ('a -> 'b) -> 'a option -> 'b option
(** [map f o] applies [f] to when [o] *)
val bind : ('a -> 'b option) -> 'a option -> 'b option
(** *)
val apply : ('a -> unit) -> 'a option -> unit
(** *)
val with_default_lazy : 'a Lazy.t -> 'a option -> 'a
(** [with_default_lazy (lazy y) x] unwraps [x] when [x] is [Some _], and
evaluates [y] otherwise. *)
val or_lazy : 'a option Lazy.t -> 'a option -> 'a option
(** [or_lazy (lazy y) x] returns [x] when [x] is [Some _], and
forces [y] otherwise. *)
val of_result : ('a, 'b) result -> 'a option
(** Convert a [result] into an [option], dropping the argument of [Error]. *)
val to_result : 'b -> 'a option -> ('a, 'b) result
(** *)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment