Joined May 2011
·

Vincent Driessen

3rd Cloud
·
Netherlands
·
·
·

Posted to Efficient sifting iterator for Python over 1 year ago

(1) "Once tee() has made a split, the original iterable should not be used anywhere else; otherwise, the iterable could get advanced without the tee objects being informed."

(2) tee() actually duplicates all elements from the input, making it much less memory-efficient as you need to consume both returned iterators relatively simultaneously, in order to prevent storing the complete input in memory. My version only keeps the minimum amount of input in memory.

>>> truthy, falsey = sift(xrange(10**6), lambda x: True)
>>> for x in truthy: pass  # as long as you consume `truthy` first, this costs no memory at all

The tee() based version keeps the full input in memory until falsey is consumed, too.

Posted to map function in shell over 1 year ago

@avdleeuw In this case, I don't want the ./ prefixes on the output, so yes. I wouldn't use them in an uncontrolled environment :)

Achievements
481 Karma
19,699 Total ProTip Views