Skip to content

Instantly share code, notes, and snippets.

@fitzgen
Created November 11, 2013 18:25
Show Gist options
  • Save fitzgen/7417845 to your computer and use it in GitHub Desktop.
Save fitzgen/7417845 to your computer and use it in GitHub Desktop.
function wu(thing) {
return new wu.Iterable(thing);
}
// This is known as @@iterator in the ES6 spec. Until it is bound to some
// well-known name or importable, find the @@iterator object by expecting it
// as the first property accessed on a for-of iterable.
//
// This shim is thanks to @andywingo via @gozala.
const iteratorSymbol = (function() {
try {
for (let _ of Proxy.create({get: function(_, name) { throw name; } }))
break;
} catch (name) {
return name;
}
throw new Error("Cannot find iterator symbol.");
}());
function makeIterator(thing) {
if (thing[iteratorSymbol]) {
return thing[iteratorSymbol];
}
if (typeof thing == "number") {
return wu.range(0, thing)[iteratorSymbol];
}
if (typeof thing == "object" && thing !== null) {
return TODO
}
throw new Error("Not iterable: " + thing);
}
wu.Iterable = function (thing) {
this[iteratorSymbol] = Function.isGenerator(thing)
? thing
: makeIterator(thing);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment