Re: [Python-ideas] A cute Python implementation of itertools.tee

2018-05-14 Thread Franklin? Lee
The topic reminded me of Stephan Houben's streamtee. https://github.com/stephanh42/streamtee/blob/master/streamtee.py It was an attempt at a memory-efficient tee, but it turned out tee was efficient enough. It uses a thunk-like method and recursion to remove the need for an explicit linked list.

Re: [Python-ideas] A cute Python implementation of itertools.tee

2018-05-07 Thread Tim Peters
I posted this several weeks ago, just for fun - an obscure but surprisingly brief Python implementation of itertools.tee, sharing a single stream (as a singly linked list) among all the returned iterables. Didn't think about it again until today, when recent discussions of lexical scoping made me

Re: [Python-ideas] A cute Python implementation of itertools.tee

2018-04-16 Thread Steven D'Aprano
On Sun, Apr 15, 2018 at 08:35:51PM +0300, Serhiy Storchaka wrote: > I have ideas about implementing zero-overhead try/except, but I have > doubts that it is worth. The benefit seems too small. It is conventional wisdom that catching exceptions is expensive, and that in performance critical

Re: [Python-ideas] A cute Python implementation of itertools.tee

2018-04-16 Thread Koos Zevenhoven
On Sun, Apr 15, 2018 at 11:55 PM, Chris Angelico wrote: > On Mon, Apr 16, 2018 at 6:46 AM, Koos Zevenhoven > wrote: > > Anyway, the whole linked list is unnecessary if the iterable can be > iterated > > over multiple times. But "tee" won't know when to do

Re: [Python-ideas] A cute Python implementation of itertools.tee

2018-04-16 Thread Koos Zevenhoven
On Mon, Apr 16, 2018 at 12:06 AM, Tim Peters wrote: > [Koos Zevenhoven ] > > It's definitely possible to write the above in a more > > readable way, and FWIW I don't think it involves "assignments as > > expressions". > > Of course it is. The point

Re: [Python-ideas] A cute Python implementation of itertools.tee

2018-04-15 Thread Tim Peters
[Koos Zevenhoven ] > It's definitely possible to write the above in a more > readable way, and FWIW I don't think it involves "assignments as > expressions". Of course it is. The point was brevity and speed, not readability. It was presented partly as a puzzle :-) >> What

Re: [Python-ideas] A cute Python implementation of itertools.tee

2018-04-15 Thread Chris Angelico
On Mon, Apr 16, 2018 at 6:46 AM, Koos Zevenhoven wrote: > Anyway, the whole linked list is unnecessary if the iterable can be iterated > over multiple times. But "tee" won't know when to do that. *That* is what I > call overhead (unless of course all the tee branches are

Re: [Python-ideas] A cute Python implementation of itertools.tee

2018-04-15 Thread Koos Zevenhoven
On Sun, Apr 15, 2018 at 8:05 AM, Tim Peters wrote: ​[...]​ > Then I thought "this is stupid! Python already does reference > counting." Voila! Vast swaths of tedious code vanished, giving this > remarkably simple implementation: > > def mytee(xs, n): > last

Re: [Python-ideas] A cute Python implementation of itertools.tee

2018-04-15 Thread Antoine Pitrou
On Sun, 15 Apr 2018 00:05:58 -0500 Tim Peters wrote: > Just for fun - no complaint, no suggestion, just sharing a bit of code > that tickled me. > > The docs for `itertools.tee()` contain a Python work-alike, which is > easy to follow. It gives each derived generator its

[Python-ideas] A cute Python implementation of itertools.tee

2018-04-14 Thread Tim Peters
Just for fun - no complaint, no suggestion, just sharing a bit of code that tickled me. The docs for `itertools.tee()` contain a Python work-alike, which is easy to follow. It gives each derived generator its own deque, and when a new value is obtained from the original iterator it pushes that