Skip to content

Instantly share code, notes, and snippets.

@hcarty
Last active August 29, 2015 14:23
Show Gist options
  • Save hcarty/a1766866cb29d885c43b to your computer and use it in GitHub Desktop.
Save hcarty/a1766866cb29d885c43b to your computer and use it in GitHub Desktop.
A small amount of Rresult + Lwt, somewhat in the style of Pvem's Lwt support
open Rresult
include R
type ('a, 'b) result_lwt = ('a, 'b) result Lwt.t
let ok x = Lwt.return @@ Ok x
let error e = Lwt.return @@ Error e
let return = ok
let bind x f =
Lwt.bind x (
function
| Error _ as e -> Lwt.return e
| Ok o -> f o
)
let ( >>= ) = bind
let map m f =
m >>= fun x -> return (f x)
let ( >>| ) = map
open Rresult
include module type of R
type ('a, 'b) result_lwt = ('a, 'b) result Lwt.t
val ok : 'a -> ('a, 'b) result_lwt
val error : 'b -> ('a, 'b) result_lwt
val return : 'a -> ('a, 'b) result_lwt
val bind :
('a, 'b) result_lwt -> ('a -> ('c, 'b) result_lwt) -> ('c, 'b) result_lwt
val ( >>= ) :
('a, 'b) result_lwt -> ('a -> ('c, 'b) result_lwt) -> ('c, 'b) result_lwt
(** aka {!bind} *)
val map : ('a, 'b) result_lwt -> ('a -> 'c) -> ('c, 'b) result_lwt
val ( >>| ) : ('a, 'b) result_lwt -> ('a -> 'c) -> ('c, 'b) result_lwt
(** aka {!map} *)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment