Adds aggregate function to MuranoPL (YAQL)
Aggregate is similar to python's reduce() built-in Change-Id: If1e3ed20172c40344b771627ce20739348b80163
This commit is contained in:
parent
c6d5a15a37
commit
96603bced3
@ -312,6 +312,16 @@ def _take(collection, count):
|
||||
return itertools.islice(collection, count)
|
||||
|
||||
|
||||
@yaql.context.EvalArg('collection', collections.Iterable)
|
||||
def _aggregate(collection, selector):
|
||||
return reduce(selector, collection)
|
||||
|
||||
|
||||
@yaql.context.EvalArg('collection', collections.Iterable)
|
||||
def _aggregate_with_seed(collection, selector, seed):
|
||||
return reduce(selector, collection, seed())
|
||||
|
||||
|
||||
def register(context):
|
||||
context.register_function(
|
||||
lambda json, mappings: _transform_json(json(), mappings()), 'bind')
|
||||
@ -347,3 +357,5 @@ def register(context):
|
||||
context.register_function(_merge_with, 'mergeWith')
|
||||
context.register_function(_skip, 'skip')
|
||||
context.register_function(_take, 'take')
|
||||
context.register_function(_aggregate, 'aggregate')
|
||||
context.register_function(_aggregate_with_seed, 'aggregate')
|
||||
|
@ -253,3 +253,20 @@ Methods:
|
||||
Contract: $.int()
|
||||
Body:
|
||||
- Return: $list.skip($start).take($count)
|
||||
|
||||
|
||||
testAggregate:
|
||||
Arguments:
|
||||
- list:
|
||||
Contract: [$.int()]
|
||||
Body:
|
||||
- Return: $list.aggregate($1 + $2)
|
||||
|
||||
testAggregateWithInitializer:
|
||||
Arguments:
|
||||
- list:
|
||||
Contract: [$.int()]
|
||||
- initializer:
|
||||
Contract: $.int()
|
||||
Body:
|
||||
- Return: $list.aggregate($1 + $2, $initializer)
|
||||
|
@ -224,3 +224,15 @@ class TestEngineYaqlFunctions(test_case.DslTestCase):
|
||||
[1, 2, 3, 4, 5, 6, 7, 8],
|
||||
2, 3)
|
||||
)
|
||||
|
||||
def test_aggregate(self):
|
||||
self.assertEqual(
|
||||
10,
|
||||
self._runner.testAggregate([1, 2, 3, 4])
|
||||
)
|
||||
|
||||
def test_aggregate_with_initializer(self):
|
||||
self.assertEqual(
|
||||
21,
|
||||
self._runner.testAggregateWithInitializer([1, 2, 3, 4], 11)
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user