This question is related to Python generators in various languages, but I want to ask something more concrete.
Suppose I have some data, I want to read that data element by element, and then apply filters to the resulting stream of elements. The filters might drop some elements, aggregate or disaggregate some other elements. Here is a Python example:
# Very big data
data = list(range(10))
# Feed data element by element
def source(data):
for i in data:
yield i
# Several input elements get aggregated into one output element
def filter1(source):
for e1 in source:
e2 = next(source, 0)
yield e1 + e2
# One input element gets disaggregated into several output elements
def filter2(source):
for e in source:
for c in str(e):
yield int(c)
# Collect the final results into a list
rslt = [e for e in filter2(filter1(source(data)))]
The result is [1, 5, 9, 1, 3, 1, 7]
. Is there an idiomatic lisp approach for such cases?