Re: [Python-ideas] Integrate some itertools into the Python syntax

The following syntax is not a real proposal, because there are way too many places it intrudes on, and you wouldn't be able to define a generator which is also a callable. That said... Slicing a list is fundamentally different from slicing an iterable. One changes state, necessarily. Every time you say "foo[:2]", it will give a different result, until you get no result at all. Instead, I'll write it "foo(:2)", and ignore the fact that that's what functions look like, and whether we CAN extend function syntax to make this legal. Using parens reflects the syntax difference between list comprehensions and generator expressions. On Mar 21, 2016 7:36 PM, "Chris Angelico" <rosuav@gmail.com> wrote:
I like this. I don't think it should happen, due to existing parts of the language it might step on, but I like it. foo(f:g:h) will mean filter(h, takewhile(g, dropwhile(negate(f), foo))) (possibly clearer if we imagine being able to write: Itertooler(foo).dropwhile(negate(f)).takewhile(g).filter(h) ) where def negate(f): def negate_f(*args, **kwargs): return not f(*args, **kwargs) return negate_f
What if `foo(3:5)` always returned a lazy iterator? (Then `iter(o)` == `o(:)`?)

On 23 March 2016 at 13:01, Franklin? Lee <leewangzhong+python@gmail.com> wrote:
As an alternative, you could use a "pipeline" style of approach, as shown in Steven D'Aprano's recipe at http://code.activestate.com/recipes/580625-collection-pipeline-in-python/ foo | SkipUntil(f) | While(g) | Filter(h) (the recipe doesn't define SkipUntil or While, but it's not hard to see how you would write them). As a side note, your code does takewhile(g), where I think the original proposal would have done takeuntil(g) (takewhile(negate(g)) - which is another argument for having explicit named filtering operations rather than implicit behaviour based on syntax. Honestly, something like this looks much more readable to me than playing games with the slice syntax. Paul

On 23 March 2016 at 13:01, Franklin? Lee <leewangzhong+python@gmail.com> wrote:
As an alternative, you could use a "pipeline" style of approach, as shown in Steven D'Aprano's recipe at http://code.activestate.com/recipes/580625-collection-pipeline-in-python/ foo | SkipUntil(f) | While(g) | Filter(h) (the recipe doesn't define SkipUntil or While, but it's not hard to see how you would write them). As a side note, your code does takewhile(g), where I think the original proposal would have done takeuntil(g) (takewhile(negate(g)) - which is another argument for having explicit named filtering operations rather than implicit behaviour based on syntax. Honestly, something like this looks much more readable to me than playing games with the slice syntax. Paul
participants (2)
-
Franklin? Lee
-
Paul Moore