Enumerator#next

1 view
Skip to first unread message

Anthony

unread,
Jan 10, 2012, 8:55:03 PM1/10/12
to jsclass-users
It would be fantastic if JS.Enumerator supported an internal current
iteration index a la http://www.ruby-doc.org/core-1.9.3/Enumerator.html#next-method

Doesn't look like it does, though... Am I wrong?

James Coglan

unread,
Jan 11, 2012, 5:45:04 AM1/11/12
to jsclas...@googlegroups.com
On 11 January 2012 01:55, Anthony <draft...@gmail.com> wrote:
It would be fantastic if JS.Enumerator supported an internal current
iteration index a la http://www.ruby-doc.org/core-1.9.3/Enumerator.html#next-method

I don't think this is possible. In Ruby you can do:

class Foo
  include Enumerable
  def each
    yield 3
    yield 2
    yield 1
  end
end

And it knows how to 'pause' the method after a yield and resume next time you call #next. There's no way to simulate this in JavaScript without using generators, which aren't widely supported.

Anthony Rowlands

unread,
Jan 11, 2012, 6:36:43 PM1/11/12
to jsclas...@googlegroups.com
Interesting... I looks like Iterators and Generators were introduced to JavaScript in version 1.7 (2008!). And support is still so poor that you haven't used them? How lame...

Anthony Rowlands

unread,
Jan 11, 2012, 6:53:02 PM1/11/12
to jsclas...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages