4 ms·
The Python problem can be solved with `itertools.tee`[0] from itertools import tee squares_min, squares_max = tee(x * x for x in range(10)) print(m
by merlincorey 8y ago
The Python problem can be solved with `itertools.tee`[0]
from itertools import tee
squares_min, squares_max = tee(x * x for x in range(10))
print(min(squares_min))
print(max(squares_max))
[0] https://docs.python.org/3/library/itertools.html#itertools.tee https://docs.python.org/3/library/itertools.html#itertools.t...
- c8g 8y agosquares = [x * x for x in range(10)] print(min(squares)) print(max(squares)) this one works and looks good.
- skbly7 8y agoIt will have memory impact though with larger range. A better approach might be: squares = lambda: (x * x for x in range(10)) print(min(squares)) print(max(squares()))
- rahimnathwani 8y agoYes, your solution is in the original post, under the heading 'Python solutions': def squares(): return (x * x for x in range(10))
- adrianN 8y agoIt also uses O(n) memory.
- sitkack 8y agoWe need a language that can schedule and fuse those folds over an infinite sequence concurrently. Joining a set of futures over an infinite sequence fuses folds
- deleted 8y ago[deleted]
- knappa 8y agoUnfortunately, tee's are backed by a queue and upon each read of a "fresh" value from one of the branches of the tee, the shared queue is appended to. There is no opportunity to remove an element from the queue until all branches of the tee have consumed it, so this will end up using O(n) memory as squares_min is completely consumed before squares_max is accessed. (Alternatingly calling next on the branches of the tee will solve the issue.) See the "Roughly equivalent to" and immediately previous text in your link.