From 8c44f7c63ee0c57936759408943ff611d630dda3 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Mon, 18 Feb 2013 17:04:24 +0100 Subject: [PATCH] added flatMap to the combinators --- sequence.ml | 10 ++++++++-- sequence.mli | 4 ++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/sequence.ml b/sequence.ml index 632fab9..b1a562c 100644 --- a/sequence.ml +++ b/sequence.ml @@ -99,10 +99,16 @@ let append s1 s2 = (** Concatenate a sequence of sequences into one sequence *) let concat s = - fun k -> + from_iter (fun k -> (* function that is called on every sub-sequence *) 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 diff --git a/sequence.mli b/sequence.mli index ed1a640..3c377d0 100644 --- a/sequence.mli +++ b/sequence.mli @@ -95,6 +95,10 @@ val append : 'a t -> 'a t -> 'a t val concat : 'a t t -> 'a t (** 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 (** Take at most [n] elements from the sequence *)