added scan combinator

This commit is contained in:
Simon Cruanes 2013-03-07 19:30:57 +01:00
parent b2c3de33a5
commit b308a8ae87
2 changed files with 11 additions and 0 deletions

View file

@ -226,6 +226,14 @@ let unfoldr f b =
in
from_iter (fun k -> unfold k b)
(** Sequence of intermediate results *)
let scan f acc seq =
from_iter
(fun k ->
k acc;
let acc = ref acc in
seq (fun elt -> let acc' = f !acc elt in k acc'; acc := acc'))
(** Max element of the sequence, using the given comparison
function. A default element has to be provided. *)
let max ?(lt=fun x y -> x < y) seq m =

View file

@ -119,6 +119,9 @@ val unfoldr : ('b -> ('a * 'b) option) -> 'b -> 'a t
yields [Some (x,b')] then [x] is returned
and unfoldr recurses with [b']. *)
val scan : ('b -> 'a -> 'b) -> 'b -> 'a t -> 'b t
(** Sequence of intermediate results *)
val max : ?lt:('a -> 'a -> bool) -> 'a t -> 'a -> 'a
(** Max element of the sequence, using the given comparison
function. A default element has to be provided. *)