Here's some made up code:
pruned = map(
# Remove all elems whose foo is less than the number of elems
lambda elems: list(filter(
lambda elem: elem.foo < len(elems),
elems
))
)
Now let's say we want a running total of all the foos. We can insert an expression to do this: total_foos = [0]
pruned = map(
# Remove all elems whose foo is less than the number of elems
lambda elems: list(filter(
lambda elem: (
# Add elem.foo to our total_foos accumulator
total_foos.append(total_foos.pop() + elem.foo),
elem.foo < len(elems)
)[-1],
elems
))
)
total_foos = total_foos.pop()
This is rather awkward and "non-Pythonic"; ideally we would use 'total_foos += elem.foo', but that can't exist in a lambda. Hence: total_foos = 0
# Have to define this outside the map, since it's a statement
def checkAndAccumulate(elems):
"""This checkAndAccumulate function is just a wrapper, for closing-over
the elems variable (since it's not in-scope outside the map call).
Returns the actual checking+accumulating function."""
def checker(elem):
"""The actual function we want to filter with"""
total_foos += elem.foo
return elem.foo < len(elems)
return checker
pruned = map(
# Remove all elems whose foo is less than the number of elems
lambda elems: list(filter(
checkAndAccumulate(elems),
elems
))
)