-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #247 from c-cube/result
add `Lwt_result`
- Loading branch information
Showing
7 changed files
with
193 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,7 @@ B _build/** | |
|
||
|
||
S tests/ | ||
S examples/ | ||
S examples/ | ||
|
||
PKG result | ||
PKG bytes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
(* Lightweight thread library for OCaml | ||
* http://www.ocsigen.org/lwt | ||
* Interface Lwt | ||
* Copyright (C) 2005-2008 Jérôme Vouillon | ||
* Laboratoire PPS - CNRS Université Paris Diderot | ||
* 2009-2012 Jérémie Dimino | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation, with linking exceptions; | ||
* either version 2.1 of the License, or (at your option) any later | ||
* version. See COPYING file for details. | ||
* | ||
* This program 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 | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | ||
* 02111-1307, USA. | ||
*) | ||
|
||
(** Module [Lwt_result]: explicit error handling *) | ||
|
||
open Result | ||
|
||
type (+'a, +'b) t = ('a, 'b) Result.result Lwt.t | ||
|
||
let return x = Lwt.return (Ok x) | ||
let fail e = Lwt.return (Error e) | ||
|
||
let lift = Lwt.return | ||
let ok x = Lwt.map (fun y -> Ok y) x | ||
|
||
let map f e = | ||
Lwt.map | ||
(function | ||
| Error e -> Error e | ||
| Ok x -> Ok (f x)) | ||
e | ||
|
||
let map_err f e = | ||
Lwt.map | ||
(function | ||
| Error e -> Error (f e) | ||
| Ok x -> Ok x) | ||
e | ||
|
||
let catch e = | ||
Lwt.catch | ||
(fun () -> ok e) | ||
fail | ||
|
||
let get_exn e = | ||
Lwt.bind e | ||
(function | ||
| Ok x -> Lwt.return x | ||
| Error e -> Lwt.fail e) | ||
|
||
let bind e f = | ||
Lwt.bind e | ||
(function | ||
| Error e -> Lwt.return (Error e) | ||
| Ok x -> f x) | ||
|
||
let bind_lwt e f = | ||
Lwt.bind e | ||
(function | ||
| Ok x -> ok (f x) | ||
| Error e -> fail e) | ||
|
||
let bind_result e f = | ||
Lwt.map | ||
(function | ||
| Error e -> Error e | ||
| Ok x -> f x) | ||
e | ||
|
||
let bind_lwt_err e f = | ||
Lwt.bind e | ||
(function | ||
| Error e -> Lwt.bind (f e) fail | ||
| Ok x -> return x) | ||
|
||
module Infix = struct | ||
let (>>=) = bind | ||
let (>|=) e f = map f e | ||
end | ||
|
||
include Infix |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
(* Lightweight thread library for OCaml | ||
* http://www.ocsigen.org/lwt | ||
* Interface Lwt | ||
* Copyright (C) 2005-2008 Jérôme Vouillon | ||
* Laboratoire PPS - CNRS Université Paris Diderot | ||
* 2009-2012 Jérémie Dimino | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation, with linking exceptions; | ||
* either version 2.1 of the License, or (at your option) any later | ||
* version. See COPYING file for details. | ||
* | ||
* This program 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 | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | ||
* 02111-1307, USA. | ||
*) | ||
|
||
(** Module [Lwt_result]: explicit error handling *) | ||
|
||
(** This module provides helpers for values of type [('a, 'b) result Lwt.t] *) | ||
|
||
type (+'a, +'b) t = ('a, 'b) Result.result Lwt.t | ||
|
||
val return : 'a -> ('a, _) t | ||
|
||
val fail : 'b -> (_, 'b) t | ||
|
||
val lift : ('a, 'b) Result.result -> ('a, 'b) t | ||
|
||
val ok : 'a Lwt.t -> ('a, _) t | ||
|
||
val catch : 'a Lwt.t -> ('a, exn) t | ||
(** [catch x] behaves like [return y] if [x] evaluates to [y], | ||
and like [fail e] if [x] raises [e] *) | ||
|
||
val get_exn : ('a, exn) t -> 'a Lwt.t | ||
(** [get_exn] is the opposite of {!catch}: it unwraps the result type, | ||
returning the value in case of success, calls {!Lwt.fail} in | ||
case of error. *) | ||
|
||
val map : ('a -> 'b) -> ('a,'e) t -> ('b,'e) t | ||
|
||
val map_err : ('e1 -> 'e2) -> ('a,'e1) t -> ('a,'e2) t | ||
|
||
val bind : ('a,'e) t -> ('a -> ('b,'e) t) -> ('b,'e) t | ||
|
||
val bind_lwt : ('a,'e) t -> ('a -> 'b Lwt.t) -> ('b,'e) t | ||
|
||
val bind_lwt_err : ('a,'e1) t -> ('e1 -> 'e2 Lwt.t) -> ('a,'e2) t | ||
|
||
val bind_result : ('a,'e) t -> ('a -> ('b,'e) Result.result) -> ('b,'e) t | ||
|
||
module Infix : sig | ||
val (>|=) : ('a,'e) t -> ('a -> 'b) -> ('b,'e) t | ||
val (>>=) : ('a,'e) t -> ('a -> ('b,'e) t) -> ('b,'e) t | ||
end | ||
|
||
include module type of Infix |