camlspotter avatar camlspotter committed fccf2e7

added fmap

Comments (0)

Files changed (4)

 module Make(M:S) : T with type 'a t := 'a M.t = struct
   include M
 
-  let map ~f t = bind t (fun x -> return (f x))
+  let fmap f t = bind t (fun x -> return (f x))
+  let map ~f t = fmap f t
 
   module Open_ = struct
     let bind = M.bind
     let (>>=) = M.bind
-    let (>>|) t f = map ~f t
+    let fmap = fmap
+    let (>>|) t f = fmap f t
     let return = return
 
     (* Applicative style *)
 module Make2(M:S2) : T2 with type ('a, 'z) t := ('a, 'z) M.t = struct
   include M
 
-  let map ~f t = bind t (fun x -> return (f x))
+  let fmap f t = bind t (fun x -> return (f x))
+  let map ~f t = fmap f t
 
   module Open_ = struct
     let bind = M.bind
     let (>>=) = M.bind
+    let fmap = fmap
     let (>>|) t f = map ~f t
     let return = return
 

lib/monad_intf.ml

   type +'a t
   val bind : 'a t -> ('a -> 'b t) -> 'b t
   val ( >>= ) : 'a t -> ('a -> 'b t) -> 'b t
+  val fmap : ('a -> 'b) -> 'a t -> 'b t
   val ( >>| ) : 'a t -> ('a -> 'b) -> 'b t
   val return : 'a -> 'a t
     
   include Open
   module Open : Open with type 'a t = 'a t
 
+  val fmap : ('a -> 'b) -> 'a t -> 'b t
   val map : f:('a -> 'b) -> 'a t -> 'b t
   (** fmap in Haskell *)
 
   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
   val ( >>| ) : ('a, 'z) t -> ('a -> 'b) -> ('b, 'z) t
   val return : 'a -> ('a, 'z) t
     
   include Open2
   module Open : Open2 with type ('a, 'z) t = ('a, 'z) t
 
+  val fmap : ('a -> 'b) -> ('a, 'z) t -> ('b, 'z) t
   val map : f:('a -> 'b) -> ('a, 'z) t -> ('b, 'z) t
     (** fmap in Haskell *)
 
     | Some v -> v :: st
     | None -> st) [] lst)
 
+(** concatMap of Haskell *)
+let concat_map f l = List.concat (List.map f l)
+
 let take n xs =
   let rec take_ n st xs =
-    if n = 0 then st
+    if n <= 0 then st
     else match xs with
     | [] -> st
     | x::xs -> take_ (n-1) (x::st) xs
 
 let split_at n xs =
   let rec split_at_ n st xs =
-    if n = 0 then st, xs
+    if n <= 0 then st, xs
     else match xs with
     | [] -> st, []
     | x::xs -> split_at_ (n-1) (x::st) xs
   List.rev r, dropped
 
 let rec drop n xs =
-  if n = 0 then xs
+  if n <= 0 then xs
   else match xs with
   | [] -> []
   | _::xs -> drop (n-1) xs
     | l -> List.rev st, l
   in
   span_ [] xs
+
+let nub xs =
+  let rec nub st = function
+    | [] -> List.rev st
+    | x::xs ->
+        let st' = if List.mem x st then st else x::st in
+        nub st' xs
+  in
+  nub [] xs
 val find_map_opt : ('a -> 'b option) -> 'a list -> 'b option
 
 val filter_map : ('a -> 'b option) -> 'a list -> 'b list
+val concat_map : ('a -> 'b list) -> 'a list -> 'b list
 
 val take : int -> 'a list -> 'a list
 val drop : int -> 'a list -> 'a list
 val span : ('a -> bool) -> 'a list -> 'a list * 'a list
 val split_at : int -> 'a list -> 'a list * 'a list
+val nub : 'a list -> 'a 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.