Re: How about adding slice notation to iterators/generators?

2009-10-27 Thread Steven D'Aprano
On Mon, 26 Oct 2009 09:51:11 -0700, Anh Hai Trinh wrote: I've written something that is better than you could've imagine. Get it here: http://github.com/aht/stream.py It works with anything iterable, no need to alter anything. Looks nice. In my Copious Spare Time I will try to look at

Re: How about adding slice notation to iterators/generators?

2009-10-26 Thread Anh Hai Trinh
I've written something that is better than you could've imagine. Get it here: http://github.com/aht/stream.py It works with anything iterable, no need to alter anything. from itertools import count from stream import item c = count() c item[1:10:2] -[1, 3, 5, 7, 9] c item[:5] -[10,

Re: How about adding slice notation to iterators/generators?

2009-10-20 Thread Jaime Buelta
El 16/10/2009 3:29, Eloff escribió: I was just working with a generator for a tree that I wanted to skip the first result (root node.) And it occurs to me, why do we need to do: import sys from itertools import islice my_iter = islice(my_iter, 1, sys.maxint) When we could simply add

Re: How about adding slice notation to iterators/generators?

2009-10-18 Thread Eloff
On Oct 16, 7:38 pm, Carl Banks pavlovevide...@gmail.com wrote: You would burden everyone who writes a custom iterator to provide a __getitem__ method just because you're too lazy to type out the word islice? No, of course not. That would be stupid. Custom iterators are iterators, so they

Re: How about adding slice notation to iterators/generators?

2009-10-18 Thread Gabriel Genellina
En Sun, 18 Oct 2009 19:53:20 -0200, Eloff dan.el...@gmail.com escribió: On Oct 16, 7:38 pm, Carl Banks pavlovevide...@gmail.com wrote: You would burden everyone who writes a custom iterator to provide a __getitem__ method just because you're too lazy to type out the word islice? No, of

Re: How about adding slice notation to iterators/generators?

2009-10-18 Thread Carl Banks
On Oct 18, 2:53 pm, Eloff dan.el...@gmail.com wrote: On Oct 16, 7:38 pm, Carl Banks pavlovevide...@gmail.com wrote: You would burden everyone who writes a custom iterator to provide a __getitem__ method just because you're too lazy to type out the word islice? No, of course not. That

Re: How about adding slice notation to iterators/generators?

2009-10-17 Thread Steven D'Aprano
On Thu, 15 Oct 2009 18:29:51 -0700, Eloff wrote: I was just working with a generator for a tree that I wanted to skip the first result (root node.) And it occurs to me, why do we need to do: import sys from itertools import islice my_iter = islice(my_iter, 1, sys.maxint) When we

Re: How about adding slice notation to iterators/generators?

2009-10-16 Thread Terry Reedy
Eloff wrote: I was just working with a generator for a tree that I wanted to skip the first result (root node.) There is already an obvious standard way to do this. it = whatever next(it) #toss first item for item in it: And it occurs to me, why do we need to do: import sys from

Re: How about adding slice notation to iterators/generators?

2009-10-16 Thread Bearophile
Terry Reedy: 1. islice works with any iterator; generator method would only work with generators A slice syntax that's syntactic sugar for islice(some_iter,1,None) may be added to all iterators. 2. iterator protocol is intentionally simple. Slice syntax is already available for lists,

Re: How about adding slice notation to iterators/generators?

2009-10-16 Thread Eloff
On Oct 16, 3:54 am, Terry Reedy tjre...@udel.edu wrote: There is already an obvious standard way to do this. it = whatever next(it) #toss first item for item in it:   That fails if there is no first item. You're taking one corner case and saying there's an easy way to do it, which is

Re: How about adding slice notation to iterators/generators?

2009-10-16 Thread Carl Banks
On Oct 16, 2:02 am, Bearophile bearophileh...@lycos.com wrote: Terry Reedy: 1. islice works with any iterator; generator method would only work with generators A slice syntax that's syntactic sugar for islice(some_iter,1,None) may be added to all iterators. All custom iterators would

Re: How about adding slice notation to iterators/generators?

2009-10-16 Thread ryles
On Oct 16, 5:28 pm, Eloff dan.el...@gmail.com wrote: By giving iterators a default, overridable, __getitem__ that is just syntactic sugar for islice, they would share a slightly larger interface subset with the builtin container types. In a duck-typed language like python, that's almost

Re: How about adding slice notation to iterators/generators?

2009-10-16 Thread Carl Banks
On Oct 16, 2:28 pm, Eloff dan.el...@gmail.com wrote: As long as it breaks no rationally existing code, I can think of no good reason why not to do this in a future python. You would burden everyone who writes a custom iterator to provide a __getitem__ method just because you're too lazy to type

How about adding slice notation to iterators/generators?

2009-10-15 Thread Eloff
I was just working with a generator for a tree that I wanted to skip the first result (root node.) And it occurs to me, why do we need to do: import sys from itertools import islice my_iter = islice(my_iter, 1, sys.maxint) When we could simply add slice operations to generators? for x in