Iterator current/prev value
Marcus Stade wrote:
This is assuming that the
current
orprev
property is indeed implemented by the engine and not user land, as that indeed both carries implementation cost and the risk out running out of sync. Is there any way other than generator functions to implement iterators? Are any ol' object with a function callednext
an iterator?
Any old object. It's a structural or "duck-typed" protocol.
We won't therefore be adding complexity, which is also bad on its own. KISS wins here. More elaborate protocols for harder or let's say rarer cases are fine and not part of the mandatory minimum.
Le 23/03/2014 19:24, Brendan Eich a écrit :
Any old object. It's a structural or "duck-typed" protocol.
Longer form at : developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/The_Iterator_protocol (reviews welcome)
On 3/23/14 at 11:24 AM, brendan at mozilla.org (Brendan Eich) wrote:
Any old object. It's a structural or "duck-typed" protocol.
We won't therefore be adding complexity, which is also bad on its own. KISS wins here. More elaborate protocols for harder or let's say rarer cases are fine and not part of the mandatory minimum.
It seems it should be easy for any user or library to introduce
in generalized intermediate object which implements the
current
or 'prev' property, so it doesn't need to be in the language.
Could the iterator protocol be extended to also have a
current
orprev
property, which contains the result of the previous call tonext
? Ifnext
has never been called, presumably this property would returnundefined
.I've searched the archives for this question, and the only semi-satisfying answer I've found is this1:
Rehashed Java (hasMore/getNext) and C# (Current/MoveNext) protocols
My apologies if I'm beating a dead horse here, or my ignorance is keeping me from fully grasping the nuances of these discussions and decisions that have followed. Unless I've missed something – which is very likely – shouldn't the implementation cost for developers be virtually zero since it could/would/should be done by the very same mechanism that generators the iterator object in the first place? That is, the implementation cost is in the engine, not user land.
I can sort of understand the sync argument given that results include the
done
property, but couldn't the same be said for any result of callingnext
then? I.e. as soon as an iterator yields a result, thedone
property is effectively out of date and the only way to truly know whether you'll get another value is to callnext
and hope for the best? In any case, the return value of acurrent
orprev
property could be an object with the single propertyvalue
; ensuring there's nothing that can be out of sync.This is assuming that the
current
orprev
property is indeed implemented by the engine and not user land, as that indeed both carries implementation cost and the risk out running out of sync. Is there any way other than generator functions to implement iterators? Are any ol' object with a function callednext
an iterator?I'm probably just confused about the whole thing, and would very much appreciate to be set straight.