diff --git a/core/CCBatch.ml b/core/CCBatch.ml index 036860cc..0102f163 100644 --- a/core/CCBatch.ml +++ b/core/CCBatch.ml @@ -45,6 +45,12 @@ module type S = sig val apply : ('a,'b) op -> 'a t -> 'b t val apply' : 'a t -> ('a,'b) op -> 'b t + val length : (_,_) op -> int + (** Number of intermediate structures needed to compute this operation *) + + val optimize : ('a,'b) op -> ('a,'b) op + (** Try to minimize the length of the operation *) + (** {6 Combinators} *) val id : ('a, 'a) op @@ -154,6 +160,13 @@ module Make(C : COLLECTION) = struct | (Compose _) as op -> Same op (* cannot optimize *) + let rec length : type a b. (a,b) op -> int = function + | Id -> 0 + | Compose (_, Id) -> 0 + | Compose (_, cont) -> 1 + length cont + + let optimize = _optimize + let apply op a = let rec _apply : type a b. (a,b) op -> a t -> b t = fun op a -> match op with diff --git a/core/CCBatch.mli b/core/CCBatch.mli index 9931929f..8f7c0b12 100644 --- a/core/CCBatch.mli +++ b/core/CCBatch.mli @@ -51,6 +51,12 @@ module type S = sig val apply : ('a,'b) op -> 'a t -> 'b t val apply' : 'a t -> ('a,'b) op -> 'b t + val length : (_,_) op -> int + (** Number of intermediate structures needed to compute this operation *) + + val optimize : ('a,'b) op -> ('a,'b) op + (** Try to minimize the length of the operation *) + (** {6 Combinators} *) val id : ('a, 'a) op