Commits

Sebastien Mondet  committed c0ec0b8

lib: add function `ERROR_MONAD.destruct`

  • Participants
  • Parent commits f04bbc4

Comments (0)

Files changed (1)

   val fail: 'b -> ('a, 'b) t
   val map: ('a, 'b) t -> ('a -> 'c) -> ('c, 'b) t
   val (>>|): ('a, 'b) t -> ('a -> 'c) -> ('c, 'b) t
+  val destruct:
+    ('a, 'b) t -> ([> `Ok of 'a | `Error of 'b] -> ('c, 'd) t) -> ('c, 'd) t
+  val (>><):
+    ('a, 'b) t -> ([> `Ok of 'a | `Error of 'b] -> ('c, 'd) t) -> ('c, 'd) t
 end
 
 (** The signature of the [Result] module: [ERROR_MONAD] + exposed result type. *)
 
   let (>>|) = map
 
+  let destruct t f = f (t :> [> `Error of 'b | `Ok of 'a ])
+  let (>><) = destruct
 end
 
 (** The signature of a basic “thread” module called [Deferred] (like [Lwt]). *)
   let some ~or_fail = function
   | None -> fail or_fail
   | Some s -> return s
+  let destruct t f =
+    Deferred.bind t (function `Ok o -> f (`Ok o) | `Error e -> f (`Error e))
+  let (>><) = destruct
 
 end