I repeat the list, creating a state when I go, and sometimes when I meet a certain sentinel, I return the result. If I did this in Python, I would be lazy at the yieldresults, the tracking state in the local area of the function when I go:
def yielder(input_list):
state = 0
for item in input_list:
if item = 'SENTINEL':
yield state * 2
state = 0
else:
state += item
yielder([1, 5, 2, 5, 'SENTINEL', 4, 6, 7])
It’s used in my first implementation reduce, but it’s not as good as yieldbecause:
- The value of I passing between iterations has both the state of the loop and the elements I want to get, which seems awkward
- It's not lazy
iterate can be used to mitigate the latter, but actually I don’t want to return something for each input element, so this will require more munging.
What is the idiomatic way to do this in Clojure?