def _reduce_nested(elem, agg, func):
"""
Reduces a nested sequence by applying a function to each
of its elements and returns an aggregation.
Arguments:
elem: The object to be reduced, either a sequence
or a singleton.
agg: A variable holding information collected
as the sequence is collapsed.
func: A function to augment the aggregate by processing
a singleton. Should have the form func(agg, elem) -> agg
Returns:
agg: The final aggregate returned by the function.
"""
if isinstance(elem, collections.Iterable):
for sub in elem:
agg = _reduce_nested(sub, agg, func)
return agg
else:
return func(agg, elem)
评论列表
文章目录