Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple library with lazy es6 iterables. If you use es5 - don't forget to add es5 polyfill
function range(start, finish, step = 1) {
if (step < 1) throw "Step must be ge 1"
return {
[Symbol.iterator]: function* () {
let count = 1;
yield start
if (start !== finish) {
let tmp = start;
if (start < finish) {
while (tmp < finish) {
yield tmp += step
}
} else {
while (tmp > finish) {
yield tmp -= step
}
}
}
}
}
}
function infiniteArrayIterator(array) {
return {
[Symbol.iterator]: function* () {
for (;;) yield* array;
}
}
}
function infinite(repeat) {
return {
[Symbol.iterator]: function* () {
for (;;) {
yield repeat
}
}
}
}
class Iterables {
static asArray(iterable) {
let tempArray = []
for (let temp of iterable) tempArray.push(temp)
return tempArray
}
static partitionedArrays(iterable, size = 1) {
let finalArray = []
let tempArray = []
for (let item of iterable) {
if (tempArray.length === size) {
finalArray.push(tempArray)
tempArray = []
}
tempArray.push(item)
}
if (tempArray.length > 0) {
finalArray.push(tempArray)
}
return finalArray
}
static findOne(iterable, object) {
let iteration = 0
for (let item of iterable) {
if (item === object) return true
}
return false
}
static uniq(iterable) {
let alreadyThere = []
let underlyingIterator = iterable[Symbol.iterator]()
return {
[Symbol.iterator]: function* () {
for (;;) {
let next = underlyingIterator.next()
if (next.done) break
if (alreadyThere.indexOf(next.value) != -1) continue
alreadyThere.push(next.value)
yield next.value
};
}
}
}
static partition(iterable, partitionSize = 1) {
return {
[Symbol.iterator]: function* () {
let tempArray = []
let count = 0
let underlyingIterator = iterable[Symbol.iterator]()
for (;;) {
if (tempArray.length === partitionSize) {
yield tempArray
tempArray = []
} else {
let nextValue = underlyingIterator.next()
if (nextValue.done) {
break
}
tempArray.push(nextValue.value)
}
}
if (tempArray.length > 0) {
yield tempArray
}
}
}
}
static skip(iterable, count = 0) {
return {
[Symbol.iterator]: function* () {
let index = 0
for (let item of iterable) {
if (index >= count) {
yield item
}
index++
}
}
}
}
static take(iterable, count = 0) {
return {
[Symbol.iterator]: function* () {
let index = 0
for (let item of iterable) {
if (index < count) {
yield item
index++
}
if (index >= count) {
break
}
}
}
}
}
static filter(iterable, func = () => true) {
return {
[Symbol.iterator]: function* () {
let count = 0
for (let item of iterable) {
if (func(item)) {
yield item
}
}
}
}
}
static zip(iter1, iter2) {
let iterator1 = iter1[Symbol.iterator]()
let iterator2 = iter2[Symbol.iterator]()
return {
[Symbol.iterator]: function* () {
for (;;) {
let it1next = iterator1.next()
let it2next = iterator2.next()
if (it1next.done && it2next.done)
break
yield[it1next.value, it2next.value]
}
}
}
}
static flatten(arrayOfArrays) {
return {
[Symbol.iterator]: function* () {
for (let ar of arrayOfArrays) yield* ar
}
}
}
static map(iterable, func = it => it) {
return {
[Symbol.iterator]: function* () {
let count = 0
for (let item of iterable) {
yield func(item)
}
}
}
}
static reduce(iterable, func) {
let count = 0;
let tempVariable;
for (let item of iterable) {
if (count === 0) {
tempVariable = item
count++
} else {
tempVariable = func(tempVariable, item)
}
}
return tempVariable
}
static all(iterable, predicate) {
for (let item of iterable) {
if (!predicate(item)) {
return false
}
}
return true
}
static any(iterable, predicate) {
for (let item of iterable) {
if (predicate(item)) {
return true
}
}
return false
}
static concat(source, ...targets) {
return {
[Symbol.iterator]: function* () {
yield* source
for (let target of targets) yield* target;
}
}
}
static cycle(iterable) {
return {
[Symbol.iterator]: function* () {
for (;;) yield* iterable
}
}
}
static elementsEqual(iterable1, iterable2) {
let array1 = Iterables.asArray(iterable1)
let array2 = Iterables.asArray(iterable2)
if (array1.length !== array2.length) return false;
for (let i = 0; i < array1.length; i++) {
if (array1[i] !== array2[i]) return false
}
return true
}
}
class FluentIterable {
static of(iterable) {
return new FluentIterable(iterable)
}
[Symbol.iterator]() {
return this._innerIterable[Symbol.iterator]()
}
constructor(iterable) {
this._innerIterable = iterable
}
map(func) {
return FluentIterable.of(Iterables.map(this, func))
}
filter(func) {
return FluentIterable.of(Iterables.filter(this, func))
}
skip(count) {
return FluentIterable.of(Iterables.skip(this, count))
}
take(count) {
return FluentIterable.of(Iterables.take(this, count))
}
asArray() {
return Iterables.asArray(this)
}
partition(count) {
return FluentIterable.of(Iterables.partition(this, count))
}
zip(other) {
return FluentIterable.of(Iterables.zip(this, other))
}
flatten(other) {
return FluentIterable.of(Iterables.flatten(this))
}
reduce(func) {
return Iterables.reduce(this, func)
}
all(predicate) {
return Iterables.all(this, predicate)
}
any(predicate) {
return Iterables.any(this, predicate)
}
concat(...targets) {
return FluentIterable.of(Iterables.concat(this, targets))
}
cycle() {
return FluentIterable.of(Iterables.cycle(this))
}
elementsEqual(other) {
return Iterables.elementsEqual(this, other)
}
uniq(){
return FluentIterable.of(Iterables.uniq(this))
}
}
/**
* example usage
*/
console.log(
FluentIterable
.of(range(1, 666))
.skip(2)
.map(x => x % 2 === 0 ? x / 2 : x * 2)
.filter(x => x % 3 === 0)
.take(15)
.partition(2)
.asArray()
)
console.log(
FluentIterable
.of(infiniteArrayIterator([3, 4, 5]))
.filter(x => x % 2 === 1)
.take(4)
.asArray()
)
"use strict";
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function range(start, finish) {
var _ref;
var step = arguments[2] === undefined ? 1 : arguments[2];
if (step < 1) throw "Step must be ge 1";
return (_ref = {}, _ref[Symbol.iterator] = regeneratorRuntime.mark(function callee$1$0() {
var count, tmp;
return regeneratorRuntime.wrap(function callee$1$0$(context$2$0) {
while (1) switch (context$2$0.prev = context$2$0.next) {
case 0:
count = 1;
context$2$0.next = 3;
return start;
case 3:
if (!(start !== finish)) {
context$2$0.next = 19;
break;
}
tmp = start;
if (!(start < finish)) {
context$2$0.next = 13;
break;
}
case 6:
if (!(tmp < finish)) {
context$2$0.next = 11;
break;
}
context$2$0.next = 9;
return tmp += step;
case 9:
context$2$0.next = 6;
break;
case 11:
context$2$0.next = 19;
break;
case 13:
if (!(start > finish)) {
context$2$0.next = 19;
break;
}
case 14:
if (!(tmp > finish)) {
context$2$0.next = 19;
break;
}
context$2$0.next = 17;
return tmp -= step;
case 17:
context$2$0.next = 14;
break;
case 19:
case "end":
return context$2$0.stop();
}
}, callee$1$0, this);
}), _ref);
}
function infiniteArrayIterator(array) {
var _ref2;
return (_ref2 = {}, _ref2[Symbol.iterator] = regeneratorRuntime.mark(function callee$1$0() {
return regeneratorRuntime.wrap(function callee$1$0$(context$2$0) {
while (1) switch (context$2$0.prev = context$2$0.next) {
case 0:
return context$2$0.delegateYield(array, "t0", 1);
case 1:
context$2$0.next = 0;
break;
case 3:
case "end":
return context$2$0.stop();
}
}, callee$1$0, this);
}), _ref2);
}
function infinite(repeat) {
var _ref3;
return (_ref3 = {}, _ref3[Symbol.iterator] = regeneratorRuntime.mark(function callee$1$0() {
return regeneratorRuntime.wrap(function callee$1$0$(context$2$0) {
while (1) switch (context$2$0.prev = context$2$0.next) {
case 0:
context$2$0.next = 2;
return repeat;
case 2:
context$2$0.next = 0;
break;
case 4:
case "end":
return context$2$0.stop();
}
}, callee$1$0, this);
}), _ref3);
}
var Iterables = (function () {
function Iterables() {
_classCallCheck(this, Iterables);
}
Iterables.asArray = function asArray(iterable) {
var tempArray = [];
for (var _iterator = iterable, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
var _ref4;
if (_isArray) {
if (_i >= _iterator.length) break;
_ref4 = _iterator[_i++];
} else {
_i = _iterator.next();
if (_i.done) break;
_ref4 = _i.value;
}
var temp = _ref4;
tempArray.push(temp);
}return tempArray;
};
Iterables.partitionedArrays = function partitionedArrays(iterable) {
var size = arguments[1] === undefined ? 1 : arguments[1];
var finalArray = [];
var tempArray = [];
for (var _iterator2 = iterable, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
var _ref5;
if (_isArray2) {
if (_i2 >= _iterator2.length) break;
_ref5 = _iterator2[_i2++];
} else {
_i2 = _iterator2.next();
if (_i2.done) break;
_ref5 = _i2.value;
}
var item = _ref5;
if (tempArray.length === size) {
finalArray.push(tempArray);
tempArray = [];
}
tempArray.push(item);
}
if (tempArray.length > 0) {
finalArray.push(tempArray);
}
return finalArray;
};
Iterables.findOne = function findOne(iterable, object) {
var iteration = 0;
for (var _iterator3 = iterable, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) {
var _ref6;
if (_isArray3) {
if (_i3 >= _iterator3.length) break;
_ref6 = _iterator3[_i3++];
} else {
_i3 = _iterator3.next();
if (_i3.done) break;
_ref6 = _i3.value;
}
var item = _ref6;
if (item === object) return true;
}
return false;
};
Iterables.uniq = function uniq(iterable) {
var _ref7;
var alreadyThere = [];
var underlyingIterator = iterable[Symbol.iterator]();
return (_ref7 = {}, _ref7[Symbol.iterator] = regeneratorRuntime.mark(function callee$2$0() {
var next;
return regeneratorRuntime.wrap(function callee$2$0$(context$3$0) {
while (1) switch (context$3$0.prev = context$3$0.next) {
case 0:
next = underlyingIterator.next();
if (!next.done) {
context$3$0.next = 3;
break;
}
return context$3$0.abrupt("break", 10);
case 3:
if (!(alreadyThere.indexOf(next.value) != -1)) {
context$3$0.next = 5;
break;
}
return context$3$0.abrupt("continue", 8);
case 5:
alreadyThere.push(next.value);
context$3$0.next = 8;
return next.value;
case 8:
context$3$0.next = 0;
break;
case 10:
;
case 11:
case "end":
return context$3$0.stop();
}
}, callee$2$0, this);
}), _ref7);
};
Iterables.partition = function partition(iterable) {
var _ref8;
var partitionSize = arguments[1] === undefined ? 1 : arguments[1];
return (_ref8 = {}, _ref8[Symbol.iterator] = regeneratorRuntime.mark(function callee$2$0() {
var tempArray, count, underlyingIterator, nextValue;
return regeneratorRuntime.wrap(function callee$2$0$(context$3$0) {
while (1) switch (context$3$0.prev = context$3$0.next) {
case 0:
tempArray = [];
count = 0;
underlyingIterator = iterable[Symbol.iterator]();
case 3:
if (!(tempArray.length === partitionSize)) {
context$3$0.next = 9;
break;
}
context$3$0.next = 6;
return tempArray;
case 6:
tempArray = [];
context$3$0.next = 13;
break;
case 9:
nextValue = underlyingIterator.next();
if (!nextValue.done) {
context$3$0.next = 12;
break;
}
return context$3$0.abrupt("break", 15);
case 12:
tempArray.push(nextValue.value);
case 13:
context$3$0.next = 3;
break;
case 15:
if (!(tempArray.length > 0)) {
context$3$0.next = 18;
break;
}
context$3$0.next = 18;
return tempArray;
case 18:
case "end":
return context$3$0.stop();
}
}, callee$2$0, this);
}), _ref8);
};
Iterables.skip = function skip(iterable) {
var _ref10;
var count = arguments[1] === undefined ? 0 : arguments[1];
return (_ref10 = {}, _ref10[Symbol.iterator] = regeneratorRuntime.mark(function callee$2$0() {
var index, _iterator4, _isArray4, _i4, _ref9, item;
return regeneratorRuntime.wrap(function callee$2$0$(context$3$0) {
while (1) switch (context$3$0.prev = context$3$0.next) {
case 0:
index = 0;
_iterator4 = iterable, _isArray4 = Array.isArray(_iterator4), _i4 = 0, _iterator4 = _isArray4 ? _iterator4 : _iterator4[Symbol.iterator]();
case 2:
if (!_isArray4) {
context$3$0.next = 8;
break;
}
if (!(_i4 >= _iterator4.length)) {
context$3$0.next = 5;
break;
}
return context$3$0.abrupt("break", 19);
case 5:
_ref9 = _iterator4[_i4++];
context$3$0.next = 12;
break;
case 8:
_i4 = _iterator4.next();
if (!_i4.done) {
context$3$0.next = 11;
break;
}
return context$3$0.abrupt("break", 19);
case 11:
_ref9 = _i4.value;
case 12:
item = _ref9;
if (!(index >= count)) {
context$3$0.next = 16;
break;
}
context$3$0.next = 16;
return item;
case 16:
index++;
case 17:
context$3$0.next = 2;
break;
case 19:
case "end":
return context$3$0.stop();
}
}, callee$2$0, this);
}), _ref10);
};
Iterables.take = function take(iterable) {
var _ref12;
var count = arguments[1] === undefined ? 0 : arguments[1];
return (_ref12 = {}, _ref12[Symbol.iterator] = regeneratorRuntime.mark(function callee$2$0() {
var index, _iterator5, _isArray5, _i5, _ref11, item;
return regeneratorRuntime.wrap(function callee$2$0$(context$3$0) {
while (1) switch (context$3$0.prev = context$3$0.next) {
case 0:
index = 0;
_iterator5 = iterable, _isArray5 = Array.isArray(_iterator5), _i5 = 0, _iterator5 = _isArray5 ? _iterator5 : _iterator5[Symbol.iterator]();
case 2:
if (!_isArray5) {
context$3$0.next = 8;
break;
}
if (!(_i5 >= _iterator5.length)) {
context$3$0.next = 5;
break;
}
return context$3$0.abrupt("break", 21);
case 5:
_ref11 = _iterator5[_i5++];
context$3$0.next = 12;
break;
case 8:
_i5 = _iterator5.next();
if (!_i5.done) {
context$3$0.next = 11;
break;
}
return context$3$0.abrupt("break", 21);
case 11:
_ref11 = _i5.value;
case 12:
item = _ref11;
if (!(index < count)) {
context$3$0.next = 17;
break;
}
context$3$0.next = 16;
return item;
case 16:
index++;
case 17:
if (!(index >= count)) {
context$3$0.next = 19;
break;
}
return context$3$0.abrupt("break", 21);
case 19:
context$3$0.next = 2;
break;
case 21:
case "end":
return context$3$0.stop();
}
}, callee$2$0, this);
}), _ref12);
};
Iterables.filter = function filter(iterable) {
var _ref14;
var func = arguments[1] === undefined ? function () {
return true;
} : arguments[1];
return (_ref14 = {}, _ref14[Symbol.iterator] = regeneratorRuntime.mark(function callee$2$0() {
var count, _iterator6, _isArray6, _i6, _ref13, item;
return regeneratorRuntime.wrap(function callee$2$0$(context$3$0) {
while (1) switch (context$3$0.prev = context$3$0.next) {
case 0:
count = 0;
_iterator6 = iterable, _isArray6 = Array.isArray(_iterator6), _i6 = 0, _iterator6 = _isArray6 ? _iterator6 : _iterator6[Symbol.iterator]();
case 2:
if (!_isArray6) {
context$3$0.next = 8;
break;
}
if (!(_i6 >= _iterator6.length)) {
context$3$0.next = 5;
break;
}
return context$3$0.abrupt("break", 18);
case 5:
_ref13 = _iterator6[_i6++];
context$3$0.next = 12;
break;
case 8:
_i6 = _iterator6.next();
if (!_i6.done) {
context$3$0.next = 11;
break;
}
return context$3$0.abrupt("break", 18);
case 11:
_ref13 = _i6.value;
case 12:
item = _ref13;
if (!func(item)) {
context$3$0.next = 16;
break;
}
context$3$0.next = 16;
return item;
case 16:
context$3$0.next = 2;
break;
case 18:
case "end":
return context$3$0.stop();
}
}, callee$2$0, this);
}), _ref14);
};
Iterables.zip = function zip(iter1, iter2) {
var _ref15;
var iterator1 = iter1[Symbol.iterator]();
var iterator2 = iter2[Symbol.iterator]();
return (_ref15 = {}, _ref15[Symbol.iterator] = regeneratorRuntime.mark(function callee$2$0() {
var it1next, it2next;
return regeneratorRuntime.wrap(function callee$2$0$(context$3$0) {
while (1) switch (context$3$0.prev = context$3$0.next) {
case 0:
it1next = iterator1.next();
it2next = iterator2.next();
if (!(it1next.done && it2next.done)) {
context$3$0.next = 4;
break;
}
return context$3$0.abrupt("break", 8);
case 4:
context$3$0.next = 6;
return [it1next.value, it2next.value];
case 6:
context$3$0.next = 0;
break;
case 8:
case "end":
return context$3$0.stop();
}
}, callee$2$0, this);
}), _ref15);
};
Iterables.flatten = function flatten(arrayOfArrays) {
var _ref17;
return (_ref17 = {}, _ref17[Symbol.iterator] = regeneratorRuntime.mark(function callee$2$0() {
var _iterator7, _isArray7, _i7, _ref16, ar;
return regeneratorRuntime.wrap(function callee$2$0$(context$3$0) {
while (1) switch (context$3$0.prev = context$3$0.next) {
case 0:
_iterator7 = arrayOfArrays, _isArray7 = Array.isArray(_iterator7), _i7 = 0, _iterator7 = _isArray7 ? _iterator7 : _iterator7[Symbol.iterator]();
case 1:
if (!_isArray7) {
context$3$0.next = 7;
break;
}
if (!(_i7 >= _iterator7.length)) {
context$3$0.next = 4;
break;
}
return context$3$0.abrupt("break", 15);
case 4:
_ref16 = _iterator7[_i7++];
context$3$0.next = 11;
break;
case 7:
_i7 = _iterator7.next();
if (!_i7.done) {
context$3$0.next = 10;
break;
}
return context$3$0.abrupt("break", 15);
case 10:
_ref16 = _i7.value;
case 11:
ar = _ref16;
return context$3$0.delegateYield(ar, "t0", 13);
case 13:
context$3$0.next = 1;
break;
case 15:
case "end":
return context$3$0.stop();
}
}, callee$2$0, this);
}), _ref17);
};
Iterables.map = function map(iterable) {
var _ref19;
var func = arguments[1] === undefined ? function (it) {
return it;
} : arguments[1];
return (_ref19 = {}, _ref19[Symbol.iterator] = regeneratorRuntime.mark(function callee$2$0() {
var count, _iterator8, _isArray8, _i8, _ref18, item;
return regeneratorRuntime.wrap(function callee$2$0$(context$3$0) {
while (1) switch (context$3$0.prev = context$3$0.next) {
case 0:
count = 0;
_iterator8 = iterable, _isArray8 = Array.isArray(_iterator8), _i8 = 0, _iterator8 = _isArray8 ? _iterator8 : _iterator8[Symbol.iterator]();
case 2:
if (!_isArray8) {
context$3$0.next = 8;
break;
}
if (!(_i8 >= _iterator8.length)) {
context$3$0.next = 5;
break;
}
return context$3$0.abrupt("break", 17);
case 5:
_ref18 = _iterator8[_i8++];
context$3$0.next = 12;
break;
case 8:
_i8 = _iterator8.next();
if (!_i8.done) {
context$3$0.next = 11;
break;
}
return context$3$0.abrupt("break", 17);
case 11:
_ref18 = _i8.value;
case 12:
item = _ref18;
context$3$0.next = 15;
return func(item);
case 15:
context$3$0.next = 2;
break;
case 17:
case "end":
return context$3$0.stop();
}
}, callee$2$0, this);
}), _ref19);
};
Iterables.reduce = function reduce(iterable, func) {
var count = 0;
var tempVariable = undefined;
for (var _iterator9 = iterable, _isArray9 = Array.isArray(_iterator9), _i9 = 0, _iterator9 = _isArray9 ? _iterator9 : _iterator9[Symbol.iterator]();;) {
var _ref20;
if (_isArray9) {
if (_i9 >= _iterator9.length) break;
_ref20 = _iterator9[_i9++];
} else {
_i9 = _iterator9.next();
if (_i9.done) break;
_ref20 = _i9.value;
}
var item = _ref20;
if (count === 0) {
tempVariable = item;
count++;
} else {
tempVariable = func(tempVariable, item);
}
}
return tempVariable;
};
Iterables.all = function all(iterable, predicate) {
for (var _iterator10 = iterable, _isArray10 = Array.isArray(_iterator10), _i10 = 0, _iterator10 = _isArray10 ? _iterator10 : _iterator10[Symbol.iterator]();;) {
var _ref21;
if (_isArray10) {
if (_i10 >= _iterator10.length) break;
_ref21 = _iterator10[_i10++];
} else {
_i10 = _iterator10.next();
if (_i10.done) break;
_ref21 = _i10.value;
}
var item = _ref21;
if (!predicate(item)) {
return false;
}
}
return true;
};
Iterables.any = function any(iterable, predicate) {
for (var _iterator11 = iterable, _isArray11 = Array.isArray(_iterator11), _i11 = 0, _iterator11 = _isArray11 ? _iterator11 : _iterator11[Symbol.iterator]();;) {
var _ref22;
if (_isArray11) {
if (_i11 >= _iterator11.length) break;
_ref22 = _iterator11[_i11++];
} else {
_i11 = _iterator11.next();
if (_i11.done) break;
_ref22 = _i11.value;
}
var item = _ref22;
if (predicate(item)) {
return true;
}
}
return false;
};
Iterables.concat = function concat(source) {
var _ref24;
for (var _len = arguments.length, targets = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
targets[_key - 1] = arguments[_key];
}
return (_ref24 = {}, _ref24[Symbol.iterator] = regeneratorRuntime.mark(function callee$2$0() {
var _iterator12, _isArray12, _i12, _ref23, target;
return regeneratorRuntime.wrap(function callee$2$0$(context$3$0) {
while (1) switch (context$3$0.prev = context$3$0.next) {
case 0:
return context$3$0.delegateYield(source, "t0", 1);
case 1:
_iterator12 = targets, _isArray12 = Array.isArray(_iterator12), _i12 = 0, _iterator12 = _isArray12 ? _iterator12 : _iterator12[Symbol.iterator]();
case 2:
if (!_isArray12) {
context$3$0.next = 8;
break;
}
if (!(_i12 >= _iterator12.length)) {
context$3$0.next = 5;
break;
}
return context$3$0.abrupt("break", 16);
case 5:
_ref23 = _iterator12[_i12++];
context$3$0.next = 12;
break;
case 8:
_i12 = _iterator12.next();
if (!_i12.done) {
context$3$0.next = 11;
break;
}
return context$3$0.abrupt("break", 16);
case 11:
_ref23 = _i12.value;
case 12:
target = _ref23;
return context$3$0.delegateYield(target, "t1", 14);
case 14:
context$3$0.next = 2;
break;
case 16:
case "end":
return context$3$0.stop();
}
}, callee$2$0, this);
}), _ref24);
};
Iterables.cycle = function cycle(iterable) {
var _ref25;
return (_ref25 = {}, _ref25[Symbol.iterator] = regeneratorRuntime.mark(function callee$2$0() {
return regeneratorRuntime.wrap(function callee$2$0$(context$3$0) {
while (1) switch (context$3$0.prev = context$3$0.next) {
case 0:
return context$3$0.delegateYield(iterable, "t0", 1);
case 1:
context$3$0.next = 0;
break;
case 3:
case "end":
return context$3$0.stop();
}
}, callee$2$0, this);
}), _ref25);
};
Iterables.elementsEqual = function elementsEqual(iterable1, iterable2) {
var array1 = Iterables.asArray(iterable1);
var array2 = Iterables.asArray(iterable2);
if (array1.length !== array2.length) return false;
for (var i = 0; i < array1.length; i++) {
if (array1[i] !== array2[i]) return false;
}
return true;
};
return Iterables;
})();
var FluentIterable = (function () {
function FluentIterable(iterable) {
_classCallCheck(this, FluentIterable);
this._innerIterable = iterable;
}
FluentIterable.of = function of(iterable) {
return new FluentIterable(iterable);
};
FluentIterable.prototype[Symbol.iterator] = function () {
return this._innerIterable[Symbol.iterator]();
};
FluentIterable.prototype.map = function map(func) {
return FluentIterable.of(Iterables.map(this, func));
};
FluentIterable.prototype.filter = function filter(func) {
return FluentIterable.of(Iterables.filter(this, func));
};
FluentIterable.prototype.skip = function skip(count) {
return FluentIterable.of(Iterables.skip(this, count));
};
FluentIterable.prototype.take = function take(count) {
return FluentIterable.of(Iterables.take(this, count));
};
FluentIterable.prototype.asArray = function asArray() {
return Iterables.asArray(this);
};
FluentIterable.prototype.partition = function partition(count) {
return FluentIterable.of(Iterables.partition(this, count));
};
FluentIterable.prototype.zip = function zip(other) {
return FluentIterable.of(Iterables.zip(this, other));
};
FluentIterable.prototype.flatten = function flatten(other) {
return FluentIterable.of(Iterables.flatten(this));
};
FluentIterable.prototype.reduce = function reduce(func) {
return Iterables.reduce(this, func);
};
FluentIterable.prototype.all = function all(predicate) {
return Iterables.all(this, predicate);
};
FluentIterable.prototype.any = function any(predicate) {
return Iterables.any(this, predicate);
};
FluentIterable.prototype.concat = function concat() {
for (var _len2 = arguments.length, targets = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
targets[_key2] = arguments[_key2];
}
return FluentIterable.of(Iterables.concat(this, targets));
};
FluentIterable.prototype.cycle = function cycle() {
return FluentIterable.of(Iterables.cycle(this));
};
FluentIterable.prototype.elementsEqual = function elementsEqual(other) {
return Iterables.elementsEqual(this, other);
};
FluentIterable.prototype.uniq = function uniq() {
return FluentIterable.of(Iterables.uniq(this));
};
return FluentIterable;
})();
/**
* example usage
*/
console.log(FluentIterable.of(range(1, 666)).skip(2).map(function (x) {
return x % 2 === 0 ? x / 2 : x * 2;
}).filter(function (x) {
return x % 3 === 0;
}).take(15).partition(2).asArray());
console.log(FluentIterable.of(infiniteArrayIterator([3, 4, 5])).filter(function (x) {
return x % 2 === 1;
}).take(4).asArray());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment