Enum.reduce

This commit is contained in:
Simon Cruanes 2013-03-21 17:19:50 +01:00
parent ebbf4a9138
commit dfbce71324
2 changed files with 11 additions and 2 deletions

View file

@ -144,6 +144,11 @@ let fold2 f acc e1 e2 =
with EOG -> ()); with EOG -> ());
!acc !acc
let reduce f enum =
let gen = enum () in
let acc = try gen () with EOG -> raise (Invalid_argument "reduce") in
Gen.fold f acc gen
(** Successive values of the accumulator *) (** Successive values of the accumulator *)
let scan f acc e = let scan f acc e =
fun () -> fun () ->

View file

@ -95,7 +95,11 @@ val fold : ('b -> 'a -> 'b) -> 'b -> 'a t -> 'b
(** Fold on the generator *) (** Fold on the generator *)
val fold2 : ('c -> 'a -> 'b -> 'c) -> 'c -> 'a t -> 'b t -> 'c val fold2 : ('c -> 'a -> 'b -> 'c) -> 'c -> 'a t -> 'b t -> 'c
(** Fold on the two enums in parallel *) (** Fold on the two enums in parallel. Stops once one of the enums
is exhausted. *)
val reduce : ('a -> 'a -> 'a) -> 'a t -> 'a
(** Fold on non-empty sequences (otherwise raise Invalid_argument) *)
val scan : ('b -> 'a -> 'b) -> 'b -> 'a t -> 'b t val scan : ('b -> 'a -> 'b) -> 'b -> 'a t -> 'b t
(** Successive values of the accumulator *) (** Successive values of the accumulator *)
@ -104,7 +108,7 @@ val iter : ('a -> unit) -> 'a t -> unit
(** Iterate on the enum *) (** Iterate on the enum *)
val iter2 : ('a -> 'b -> unit) -> 'a t -> 'b t -> unit val iter2 : ('a -> 'b -> unit) -> 'a t -> 'b t -> unit
(** Iterate on the two sequences *) (** Iterate on the two sequences. Stops once one of them is exhausted.*)
val length : _ t -> int val length : _ t -> int
(** Length of an enum (linear time) *) (** Length of an enum (linear time) *)