1

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?

Community
  • 1
  • 1
Andrei
  • 2,585
  • 1
  • 14
  • 17
  • @aryamccarthy, I haven't spotted that one, thank you. I am not insisting on having 'yield' in the code. I.e., if there is an easy way to achieve the same result with closures, I would like to see that. I can write something myself but it feels I need to design a specific lambda function for each filter, and the whole result is less elegant than in python. But maybe there is a better way... – Andrei May 19 '17 at 20:39
  • There are a ton of links online. Stack Overflow isn't for recommendation or research—it's explicitly out of scope. – Arya McCarthy May 19 '17 at 20:42

0 Answers0