package tezos-lwt-result-stdlib

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file result.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2020 Nomadic Labs <contact@nomadic-labs.com>                *)
(*                                                                           *)
(* Permission is hereby granted, free of charge, to any person obtaining a   *)
(* copy of this software and associated documentation files (the "Software"),*)
(* to deal in the Software without restriction, including without limitation *)
(* the rights to use, copy, modify, merge, publish, distribute, sublicense,  *)
(* and/or sell copies of the Software, and to permit persons to whom the     *)
(* Software is furnished to do so, subject to the following conditions:      *)
(*                                                                           *)
(* The above copyright notice and this permission notice shall be included   *)
(* in all copies or substantial portions of the Software.                    *)
(*                                                                           *)
(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)
(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  *)
(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL   *)
(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)
(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING   *)
(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER       *)
(* DEALINGS IN THE SOFTWARE.                                                 *)
(*                                                                           *)
(*****************************************************************************)

open Lwt.Infix

type ('a, 'e) t = ('a, 'e) result = Ok of 'a | Error of 'e

let ok x = Ok x

let ok_s x = Lwt.return (Ok x)

let error x = Error x

let error_s x = Lwt.return (Error x)

let value r ~default = match r with Ok v -> v | Error _ -> default

let value_f r ~default = match r with Ok v -> v | Error _ -> default ()

let bind r f = match r with Ok v -> f v | Error _ as error -> error

let bind_s r f =
  match r with Ok v -> f v | Error _ as error -> Lwt.return error

let bind_error r f = match r with Ok _ as ok -> ok | Error e -> f e

let bind_error_s r f =
  match r with Ok _ as ok -> Lwt.return ok | Error e -> f e

let join = function
  | (Error _ as error) | Ok (Error _ as error) -> error
  | Ok (Ok _ as ok) -> ok

let map f = function Ok v -> Ok (f v) | Error _ as error -> error

let map_e f r = bind r f

let map_s f = function
  | Ok v -> f v >>= fun v -> Lwt.return (Ok v)
  | Error _ as error -> Lwt.return error

let map_es f r = bind_s r f

let map_error f = function Ok _ as ok -> ok | Error e -> Error (f e)

let map_error_e f r = bind_error r f

let map_error_s f = function
  | Ok v -> Lwt.return (Ok v)
  | Error e -> f e >>= fun e -> Lwt.return (Error e)

let map_error_es f r = bind_error_s r f

let fold ~ok ~error = function Ok v -> ok v | Error e -> error e

let iter f = function Ok v -> f v | Error _ -> ()

let iter_s f = function Ok v -> f v | Error _ -> Lwt.return_unit

let iter_error f = function Ok _ -> () | Error e -> f e

let iter_error_s f = function Ok _ -> Lwt.return_unit | Error e -> f e

let is_ok = function Ok _ -> true | Error _ -> false

let is_error = function Ok _ -> false | Error _ -> true

let equal ~ok ~error x y =
  match (x, y) with
  | (Ok x, Ok y) -> ok x y
  | (Error x, Error y) -> error x y
  | (Ok _, Error _) | (Error _, Ok _) -> false

let compare ~ok ~error x y =
  match (x, y) with
  | (Ok x, Ok y) -> ok x y
  | (Error x, Error y) -> error x y
  | (Ok _, Error _) -> -1
  | (Error _, Ok _) -> 1

let to_option = function Ok v -> Some v | Error _ -> None

let of_option ~error = function Some v -> Ok v | None -> Error error

let to_list = function Ok v -> [v] | Error _ -> []

let to_seq = function
  | Ok v -> Stdlib.Seq.return v
  | Error _ -> Stdlib.Seq.empty
OCaml

Innovation. Community. Security.