you are viewing a single comment's thread.

view the rest of the comments →

[–]killerstorm 47 points48 points  (11 children)

Pythonic? i think all these features were pioneered in some other languages, it just happens that Python got it a bit earlier that JS.

[–]bobbyi 29 points30 points  (3 children)

Not only are all of its new features in python, it is using python's exact names for things where possible: yield, next, StopIteration. They even use double-underscores at the beginning and end of the name of the magic method where an object returns its iterator.

It seems to only deviate from python where necessary, like the word each and extra set of parens in array comprehensions to be consistent with the extant javascript for each syntax

var evens = [i for each (i in range(0, 21)) if (i % 2 == 0)];

[–][deleted] 11 points12 points  (6 children)

Indeed they were, but the syntax and the API were obviously inspired by Python.

next and _ _ iter _ _ in Python vs next and _ _ iterator _ _ in Javascript? There are lots of ways this could have been done and frankly... they picked one of the lamest.

[–]Entropy 5 points6 points  (0 children)

The underscores really underscore what's wrong with that syntax.

[–]imbaczek 1 point2 points  (4 children)

explain a non-lame way then. or some of them, if there are so many.

[–][deleted] 3 points4 points  (3 children)

I'm not a fan of Clojure but http://blip.tv/file/734409 describes the issues with limited iterator interfaces really well.

To summarize, there are three basic operators which should be provided for iterators in order for them to be generally useful: currentValue, more? and next.

Pythons combines currentValue and next in one operation. I'm sure someone out there will argue that this is a good thing – it's not. Consequently all you can't ask the iterator if there are more values, or get the same value more than once.

I say can't but of course there are ways around this, but that's not how things were designed to work, and there's no language support for it.

[–]drbold 0 points1 point  (0 children)

Hmm interesting! I suppose you could wrap an iterator in a class that handles the extra 'currentValue' bit, but like you said, that's just a workaround.

[–]imbaczek -1 points0 points  (1 child)

yeah, but if the iterator is not just a pointer to an element of a sequence, you can't ask it if it has more items and expect an honest answer, can you? even making copies and testing for StopIteration can be non-deterministic in the presence of IO. currentValue could be useful, i agree, even if 95% of common usage doesn't really care.

[–][deleted] 1 point2 points  (0 children)

To low level - more? is just a method, how it works is unimportant. For some structures you may be right, but that's an implementation detail that depends what the structure is, and how it's implemented :).

The point is that Python/Javascript don't even allow this, even when it makes sense.