camlspotter avatar camlspotter committed fbc2b00

update

Comments (0)

Files changed (10)

 - Added Monad.fmap2 and Monad.liftM2
 - Added scan*_left functions
 - Added Dllist for doubly linked list
+- Added Result in Spot
+- Added Base.{result, from_Ok, from_Result} 
 
 2.1.1
 ------------
   r := v;
   protect ~f () ~finally:(fun () -> r := back_v)
 
+let from_Ok = Result.from_Ok
+let result = Result.result
+let from_Some = Option.from_Some
     raises an exceptoin.
 *)
 
+(** {6 Option } *)
+val from_Some : 'a option -> 'a
+
+(** {6 Result } *)
+val from_Ok : ('e -> exn) -> [< ('a, 'e) Result.t ] -> 'a
+val result : ('a -> 'b) -> ('c -> 'b) -> [< ('a, 'c) Result.t] -> 'b
+
 (** {6 Misc things } *)
 
 val sprintf : ('a, unit, string) format -> 'a
     include Open_
   end
 
-  let ignore a = a >>= fun _ -> return ()
-  let void = ignore
+  let void a = a >>= fun _ -> return ()
 
   let rec seq = function
     | [] -> return []

lib/monad_intf.ml

 
 (** Minimum monad signature with an additional type parameter ['z]  *)
 module type S2 = sig
-  type ('a, 'z) t
+  type (+'a, 'z) t
   val return : 'a -> ('a, 'z) t
   val bind : ('a, 'z) t -> ('a -> ('b, 'z) t) -> ('b, 'z) t
 end
 
 (** Open name space for [S2] *)
 module type Open2 = sig
-  type ('a, 'z) t
+  type (+'a, 'z) t
   val bind : ('a, 'z) t -> ('a -> ('b, 'z) t) -> ('b, 'z) t
   val ( >>= ) : ('a, 'z) t -> ('a -> ('b, 'z) t) -> ('b, 'z) t
   val fmap : ('a -> 'b) -> ('a, 'z) t -> ('b, 'z) t
   try Some (f ~fail) with Error.Error -> None
 
 let catch_exn f = catch (fun ~fail -> try f () with _ -> fail ())
+
+let from_Some = function
+  | Some v -> v
+  | _ -> invalid_arg "Optino.from_Some"
+
 
 val catch : (fail:(unit -> 'exn) -> 'a) -> 'a option
 val catch_exn : (unit -> 'a) -> 'a option
+
+val from_Some : 'a option -> 'a
+(** may raise [Invalid_argument] *)
+
 type ('a, 'error) t = [`Ok of 'a | `Error of 'error]
 
+include Monad.Make2(struct
+  type ('a, 'error) t = [`Ok of 'a | `Error of 'error]
+
+  let return v = `Ok v
+
+  let bind t f = match t with
+    | `Ok v -> f v
+    | `Error e -> `Error e
+end)
+
+let fail e = `Error e
+
 let catch f = 
   let module Error = struct exception Error end in
   let error = ref None in
 let from_Ok to_exn = function
   | `Ok v -> v
   | `Error e -> raise (to_exn e)
+
+let result left right = function
+  | `Ok v -> left v
+  | `Error e -> right e
-type ('a, 'error) t = [`Ok of 'a | `Error of 'error]
+include Monad_intf.T2 with type ('a,'error) t = [`Ok of 'a | `Error of 'error]
+
+val fail : 'a -> [> `Error of 'a ]
 
 val catch : (fail:('error -> 'exn) -> 'a) -> [> ('a, 'error) t]
 (** [catch f] runs [f ~fail]. 
 *)
 
 val from_Ok : ('error -> exn) -> [< ('a, 'error) t] -> 'a
+
+val result : ('a -> 'b) -> ('c -> 'b) -> [< ('a, 'c) t] -> 'b
+(** Haskell's either *)
+
 module Overload   = Overload
 module Mtypes     = Mtypes
 module Stream     = Stream
+module Result     = Result
 
 module List = struct
   include List
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.