From https://github.com/keyvan-m-sadeghi/pythonic
Slightly modified in order to be usable in browser (i.e. not as a node.js module)
Copyright (c) 2016 Assister.Ai
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
this[Symbol.iterator] = generator;
async * [Symbol.asyncIterator]() {
for (const element of this) {
for (const element of this) {
for (const element of this) {
result.push(callback(element));
for (const element of this) {
reduce(callback, initialValue) {
let empty = typeof initialValue === 'undefined';
let accumulator = initialValue;
for (const currentValue of this) {
accumulator = currentValue;
accumulator = callback(accumulator, currentValue, index, this);
throw new TypeError('Reduce of empty Iterator with no initial value');
for (const element of this) {
for (const element of this) {
if (!callback(element)) {
static fromIterable(iterable) {
return new Iterator(function * () {
for (const element of iterable) {
if (!this.currentInvokedGenerator) {
this.currentInvokedGenerator = this[Symbol.iterator]();
return this.currentInvokedGenerator.next();
delete this.currentInvokedGenerator;
function rangeSimple(stop) {
return new Iterator(function * () {
for (let i = 0; i < stop; i++) {
function rangeOverload(start, stop, step = 1) {
return new Iterator(function * () {
for (let i = start; i < stop; i += step) {
function range(...args) {
return rangeSimple(...args);
return rangeOverload(...args);
function enumerate(iterable) {
return new Iterator(function * () {
for (const element of iterable) {
const _zip = longest => (...iterables) => {
if (iterables.length == 0) {
// works starting with 1 iterable
// [a,b,c] -> [[a],[b],[c]]
// [a,b,c],[d,e,f] -> [[a,d],[b,e],[c,f]]
throw new TypeError("zip takes 1 iterables at least, "+iterables.length+" given");
return new Iterator(function * () {
const iterators = iterables.map(iterable => Iterator.fromIterable(iterable));
const row = iterators.map(iterator => iterator.next());
const check = longest ? row.every.bind(row) : row.some.bind(row);
if (check(next => next.done)) {
yield row.map(next => next.value);
const zip = _zip(false), zipLongest= _zip(true);
if (obj instanceof Map) {
return new Iterator(function * () {
for (const key of keys()) {
module.exports = {Iterator, range, enumerate, zip: _zip(false), zipLongest: _zip(true), items};