added flatMap to the combinators

This commit is contained in:
Simon Cruanes 2013-02-18 17:04:24 +01:00
parent 55ff51f2cb
commit 8c44f7c63e
2 changed files with 12 additions and 2 deletions

View file

@ -99,10 +99,16 @@ let append s1 s2 =
(** Concatenate a sequence of sequences into one sequence *) (** Concatenate a sequence of sequences into one sequence *)
let concat s = let concat s =
fun k -> from_iter (fun k ->
(* function that is called on every sub-sequence *) (* function that is called on every sub-sequence *)
let k_seq seq = iter k seq in let k_seq seq = iter k seq in
s k_seq s k_seq)
(** Monadic bind. It applies the function to every element of the
initial sequence, and calls [concat]. *)
let flatMap f seq =
from_iter
(fun k -> seq (fun x -> (f x) k))
exception ExitSequence exception ExitSequence

View file

@ -95,6 +95,10 @@ val append : 'a t -> 'a t -> 'a t
val concat : 'a t t -> 'a t val concat : 'a t t -> 'a t
(** Concatenate a sequence of sequences into one sequence *) (** Concatenate a sequence of sequences into one sequence *)
val flatMap : ('a -> 'b t) -> 'a t -> 'b t
(** Monadic bind. It applies the function to every element of the
initial sequence, and calls [concat]. *)
val take : int -> 'a t -> 'a t val take : int -> 'a t -> 'a t
(** Take at most [n] elements from the sequence *) (** Take at most [n] elements from the sequence *)