- Source:
Trait for any iterable type.
Uses the Symbol.iterator
Symbol, so this is implemented for any
type that implements the iterator protocol.
You generally won't want to use this, implement the iterator protocol instead.
const { strictEqual: assertIs } = require('assert');
const { Sequence, iter, assertEquals } = require('ferrum');
// Sequence extends the iterator protocol; they use the same
// symbols
assertIs(Sequence.sym, Symbol.iterator)
// Usually you want to implement the Sequence trait by implementing
// the iterator protocol.
class Foo {
[Symbol.iterator]() {
function* gen() {
yield 2;
yield 3;
yield 4;
}
return gen();
}
};
// You can use iter() as a shorthand for getting the iterator.
// It is much less cumbersome than using mySeq[Symbol.iterator]().
it = iter(new Foo());
assertEquals(it.next(), { value: 2, done: false });
assertEquals(it.next(), { value: 3, done: false });
assertEquals(it.next(), { value: 4, done: false });
assertEquals(it.next(), { value: undefined, done: true });