map and reduce functions implemented in PHP extension.
0.999 - API unstable
$rows = group_by([
[ 'category' => 'Food', 'type' => 'pasta', 'amount' => 1, 'foo' => 10 ],
[ 'category' => 'Food', 'type' => 'pasta', 'amount' => 1 ],
[ 'category' => 'Food', 'type' => 'juice', 'amount' => 1 ],
[ 'category' => 'Food', 'type' => 'juice', 'amount' => 1 ],
[ 'category' => 'Book', 'type' => 'programming', 'amount' => 5 ],
[ 'category' => 'Book', 'type' => 'programming', 'amount' => 2 ],
[ 'category' => 'Book', 'type' => 'cooking', 'amount' => 6 ],
[ 'category' => 'Book', 'type' => 'cooking', 'amount' => 2 ],
], ['category','type'], [
'total_amount' => [
'selector' => 'amount',
'aggregator' => REDUCER_SUM,
],
'cnt' => REDUCER_COUNT,
]);
print_r($ret);
Aggregator definition syntax
[
'{alias}' => [
'selector' => '{selector}',
'aggregator' => {constant | function},
],
'{alias}' => {constant | function},
]
REDUCER_SUM
REDUCER_COUNT
REDUCER_MIN
REDUCER_MAX
REDUCER_AVG
REDUCER_FIRST
REDUCER_LAST
Aggregating with custom reduce function:
$ret = group_by($rows, ['category','type'], [
'amount' => function($carry, $current) {
return $carry + $current;
}
]);
Aggregating with selector:
$result = group_by($rows, ['category','type'], [
'total_amount' => [
'selector' => 'amount',
'aggregator' => function($carry, $current) { return $carry + $current; }
],
]);
Rows(N) | PHP | Extension | Memroy |
---|---|---|---|
100,000 | 264ms | 98ms | 50MB |
1,000,000 | 2,862ms | 565ms | 438MB |
$ phpize
$ ./configure --enable-reducer
$ make install
© 2017-01-01 Yo-an Lin ALL RIGHTS RESERVED