module Count:sig
..end
Example: consider this function that makes a set from a list of integers:
foldl (flip S.add) S.empty
Suppose we want to convert the function to also count the number of ints we've added; with the Count module we just need to change the function definition to:
foldl (Count.update (flip S.add)) (Count.init S.empty)
In other words, we just initialize our initial accumulator using Count.init
,
and then apply the Count.update
combinator to our folding function.
Warning! Documentation bug: type t
is actually abstract!
type'a
t =int * 'a
val init : ?i:int -> 'a -> int * 'a
init ?i a
: convert initial accumulator value a
to a counting accumulatori
: initial value for counta
: initial accumulatorval n : 'a * 'b -> 'a
n ca
: extract count from counting accumulator ca
val acc : 'a * 'b -> 'b
acc ca
: extract accumulator from counting accumulator ca
val pair : 'a -> 'a
pair ca
: extract count and accumulator as a pairval update : ?i:int -> ('a -> 'b -> 'c) -> int * 'a -> 'b -> int * 'c
update ?i f ca
: return updated counting accumulator ca
by applying f
to ca
and incrementing the count by i
(default: 1
)val incr : ?i:int -> int * 'a -> int * 'a
incr ?i ca
: return counting accumulator ca
with count incremented by i
(default: 1
) but with accumulator unmodifiedval withcount : ('a -> 'b) -> 'a * 'c -> 'c
withcount f ca
: apply f
to the count in ca
(for side-effect, e.g. printing),
returning the accumulator