Module agg
source code
agg [-g GROUPING_FUNCTION] INITIAL_VALUE AGGREGATOR
agg [-c GROUPING_FUNCTION] INITIAL_VALUE AGGREGATOR
Combines inputs into a smaller number of outputs. For example, if the
input is a stream of numbers, then agg
can be used to
generate output comprising the sum of the inputs.
If GROUPING_FUNCTION
is omitted, then one output object
is generated by initializing an accumulator to INITIAL_VALUE
and then combining the accumulator with input objects using
AGGREGATOR
. The inputs to AGGREGATOR
are the
current values of the accumulator followed by the members of the input
object.
Example: If the input objects are integers 1, 2,
3
, then the sum of the integers is computed by:
agg 0 'sum, x: sum + x'
which yields 6
.
Example: The sum and sum of squares of a sequence of input
number can be computed by:
agg '(0, 0)' 'sum, sum_squares, input: (sum + input, sum + input ** 2)'
In this case, the accumulator is a tuple containing two integers, the
sum and the sum of squares. The initial value of this accumulator is
(0, 0)
. Each input consists of a single number. The
aggregator function takes as input the two parts of the accumulator
followed by the next number of the input, and outputs a tuple containing
the updated accumulator value.
If GROUPING_FUNCTION
is specified, then a set of
accumulators is maintained, one for each value of
GROUPING_FUNCTION
. Each output value consists of the group
value followed by the accumulated values for the group,
("values" not "value" because the accumulator may be
a sequence.)
Example: If the input objects are ('a', 1), ('a', 2), ('b', 3),
('b', 4)
, then the sum of ints for each string is computed by:
agg -g 'label, number: label' 0 'sum, label, number: sum + number'
which yields ('a', 3), ('b', 7)
.
If GROUPING_FUNCTION
is specified with the
-g
flag, then agg
generates its output when the
input stream has ended. (It can't generate output earlier, because group
members map appear in any order.) In some situations however, group
members appear consecutively, and it is useful to get output earlier. If
group members are known to be consecutive, then
GROUPING_FUNCTION
can be specified using the -c
flag.
|
agg(initial_value,
aggregator,
group=None,
consecutive=None)
Combine inputs into a smaller number of outputs. |
source code
|
|
agg(initial_value,
aggregator,
group=None,
consecutive=None)
| source code
|
Combine inputs into a smaller number of outputs. If neither
group nor consecutive is specified, then there
is one accumulator, initialized to initial_value . The
aggregator function is used to combine the current value of
the accumulator with the input to yield the next value of the
accumulator. The arguments to aggregator are the elements of
the accumulator followed by the elements of one piece of input. If
group is specified, then there is one accumulator for each
group value, defined by applying the function group to each
input. consecutive is just like group except
that it is assumed that group values are adjacent in the input sequence.
At most one of group and consecutive may be
specified.
|