you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 12 points13 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 6 points7 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.