ramda-generators
Version:
A collection of lazily evaluated generators and functions to work with generators in Javascript and Typescript, following the style of Ramda's functions
1,246 lines (1,198 loc) • 80.9 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = global || self, global.ramdaGenerators = factory());
}(this, (function () { 'use strict';
/*! *****************************************************************************
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions
and limitations under the License.
***************************************************************************** */
var __assign = function() {
__assign = Object.assign || function __assign(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
function __awaiter(thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
}
function __generator(thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
}
function __values(o) {
var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0;
if (m) return m.call(o);
return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
}
function __read(o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
}
function __await(v) {
return this instanceof __await ? (this.v = v, this) : new __await(v);
}
function __asyncGenerator(thisArg, _arguments, generator) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var g = generator.apply(thisArg, _arguments || []), i, q = [];
return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
function fulfill(value) { resume("next", value); }
function reject(value) { resume("throw", value); }
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
}
function __asyncDelegator(o) {
var i, p;
return i = {}, verb("next"), verb("throw", function (e) { throw e; }), verb("return"), i[Symbol.iterator] = function () { return this; }, i;
function verb(n, f) { i[n] = o[n] ? function (v) { return (p = !p) ? { value: __await(o[n](v)), done: n === "return" } : f ? f(v) : v; } : f; }
}
function __asyncValues(o) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var m = o[Symbol.asyncIterator], i;
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
}
function _isPlaceholder(a) {
return a != null && typeof a === 'object' && a['@@functional/placeholder'] === true;
}
/**
* Optimized internal one-arity curry function.
*
* @private
* @category Function
* @param {Function} fn The function to curry.
* @return {Function} The curried function.
*/
function _curry1(fn) {
return function f1(a) {
if (arguments.length === 0 || _isPlaceholder(a)) {
return f1;
} else {
return fn.apply(this, arguments);
}
};
}
/**
* Optimized internal two-arity curry function.
*
* @private
* @category Function
* @param {Function} fn The function to curry.
* @return {Function} The curried function.
*/
function _curry2(fn) {
return function f2(a, b) {
switch (arguments.length) {
case 0:
return f2;
case 1:
return _isPlaceholder(a) ? f2 : _curry1(function (_b) {
return fn(a, _b);
});
default:
return _isPlaceholder(a) && _isPlaceholder(b) ? f2 : _isPlaceholder(a) ? _curry1(function (_a) {
return fn(_a, b);
}) : _isPlaceholder(b) ? _curry1(function (_b) {
return fn(a, _b);
}) : fn(a, b);
}
};
}
function _arity(n, fn) {
/* eslint-disable no-unused-vars */
switch (n) {
case 0:
return function () {
return fn.apply(this, arguments);
};
case 1:
return function (a0) {
return fn.apply(this, arguments);
};
case 2:
return function (a0, a1) {
return fn.apply(this, arguments);
};
case 3:
return function (a0, a1, a2) {
return fn.apply(this, arguments);
};
case 4:
return function (a0, a1, a2, a3) {
return fn.apply(this, arguments);
};
case 5:
return function (a0, a1, a2, a3, a4) {
return fn.apply(this, arguments);
};
case 6:
return function (a0, a1, a2, a3, a4, a5) {
return fn.apply(this, arguments);
};
case 7:
return function (a0, a1, a2, a3, a4, a5, a6) {
return fn.apply(this, arguments);
};
case 8:
return function (a0, a1, a2, a3, a4, a5, a6, a7) {
return fn.apply(this, arguments);
};
case 9:
return function (a0, a1, a2, a3, a4, a5, a6, a7, a8) {
return fn.apply(this, arguments);
};
case 10:
return function (a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) {
return fn.apply(this, arguments);
};
default:
throw new Error('First argument to _arity must be a non-negative integer no greater than ten');
}
}
/**
* Internal curryN function.
*
* @private
* @category Function
* @param {Number} length The arity of the curried function.
* @param {Array} received An array of arguments received thus far.
* @param {Function} fn The function to curry.
* @return {Function} The curried function.
*/
function _curryN(length, received, fn) {
return function () {
var combined = [];
var argsIdx = 0;
var left = length;
var combinedIdx = 0;
while (combinedIdx < received.length || argsIdx < arguments.length) {
var result;
if (combinedIdx < received.length && (!_isPlaceholder(received[combinedIdx]) || argsIdx >= arguments.length)) {
result = received[combinedIdx];
} else {
result = arguments[argsIdx];
argsIdx += 1;
}
combined[combinedIdx] = result;
if (!_isPlaceholder(result)) {
left -= 1;
}
combinedIdx += 1;
}
return left <= 0 ? fn.apply(this, combined) : _arity(left, _curryN(length, combined, fn));
};
}
/**
* Returns a curried equivalent of the provided function, with the specified
* arity. The curried function has two unusual capabilities. First, its
* arguments needn't be provided one at a time. If `g` is `R.curryN(3, f)`, the
* following are equivalent:
*
* - `g(1)(2)(3)`
* - `g(1)(2, 3)`
* - `g(1, 2)(3)`
* - `g(1, 2, 3)`
*
* Secondly, the special placeholder value [`R.__`](#__) may be used to specify
* "gaps", allowing partial application of any combination of arguments,
* regardless of their positions. If `g` is as above and `_` is [`R.__`](#__),
* the following are equivalent:
*
* - `g(1, 2, 3)`
* - `g(_, 2, 3)(1)`
* - `g(_, _, 3)(1)(2)`
* - `g(_, _, 3)(1, 2)`
* - `g(_, 2)(1)(3)`
* - `g(_, 2)(1, 3)`
* - `g(_, 2)(_, 3)(1)`
*
* @func
* @memberOf R
* @since v0.5.0
* @category Function
* @sig Number -> (* -> a) -> (* -> a)
* @param {Number} length The arity for the returned function.
* @param {Function} fn The function to curry.
* @return {Function} A new, curried function.
* @see R.curry
* @example
*
* const sumArgs = (...args) => R.sum(args);
*
* const curriedAddFourNumbers = R.curryN(4, sumArgs);
* const f = curriedAddFourNumbers(1, 2);
* const g = f(3);
* g(4); //=> 10
*/
var curryN = /*#__PURE__*/_curry2(function curryN(length, fn) {
if (length === 1) {
return _curry1(fn);
}
return _arity(length, _curryN(length, [], fn));
});
function _identity(x) {
return x;
}
/**
* A function that does nothing but return the parameter supplied to it. Good
* as a default or placeholder function.
*
* @func
* @memberOf R
* @since v0.1.0
* @category Function
* @sig a -> a
* @param {*} x The value to return.
* @return {*} The input value, `x`.
* @example
*
* R.identity(1); //=> 1
*
* const obj = {};
* R.identity(obj) === obj; //=> true
* @symb R.identity(a) = a
*/
var identity = /*#__PURE__*/_curry1(_identity);
/**
* Takes two arguments, `fst` and `snd`, and returns `[fst, snd]`.
*
* @func
* @memberOf R
* @since v0.18.0
* @category List
* @sig a -> b -> (a,b)
* @param {*} fst
* @param {*} snd
* @return {Array}
* @see R.objOf, R.of
* @example
*
* R.pair('foo', 'bar'); //=> ['foo', 'bar']
*/
var pair = /*#__PURE__*/_curry2(function pair(fst, snd) {
return [fst, snd];
});
/**
* Returns a function of arity `n` from a (manually) curried function.
*
* @func
* @memberOf R
* @since v0.14.0
* @category Function
* @sig Number -> (a -> b) -> (a -> c)
* @param {Number} length The arity for the returned function.
* @param {Function} fn The function to uncurry.
* @return {Function} A new function.
* @see R.curry
* @example
*
* const addFour = a => b => c => d => a + b + c + d;
*
* const uncurriedAddFour = R.uncurryN(4, addFour);
* uncurriedAddFour(1, 2, 3, 4); //=> 10
*/
var uncurryN = /*#__PURE__*/_curry2(function uncurryN(depth, fn) {
return curryN(depth, function () {
var currentDepth = 1;
var value = fn;
var idx = 0;
var endIdx;
while (currentDepth <= depth && typeof value === 'function') {
endIdx = currentDepth === depth ? arguments.length : idx + value.length;
value = value.apply(this, Array.prototype.slice.call(arguments, idx, endIdx));
currentDepth += 1;
idx = endIdx;
}
return value;
});
});
function _scan(fn, init, gen) {
var gen_1, gen_1_1, value, e_1_1;
var e_1, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 5, 6, 7]);
gen_1 = __values(gen), gen_1_1 = gen_1.next();
_b.label = 1;
case 1:
if (!!gen_1_1.done) return [3 /*break*/, 4];
value = gen_1_1.value;
return [4 /*yield*/, init = fn(init, value)];
case 2:
_b.sent();
_b.label = 3;
case 3:
gen_1_1 = gen_1.next();
return [3 /*break*/, 1];
case 4: return [3 /*break*/, 7];
case 5:
e_1_1 = _b.sent();
e_1 = { error: e_1_1 };
return [3 /*break*/, 7];
case 6:
try {
if (gen_1_1 && !gen_1_1.done && (_a = gen_1.return)) _a.call(gen_1);
}
finally { if (e_1) throw e_1.error; }
return [7 /*endfinally*/];
case 7: return [2 /*return*/];
}
});
}
function _scanAsync(fn, init, gen) {
return __asyncGenerator(this, arguments, function _scanAsync_1() {
var gen_2, gen_2_1, value, e_2_1;
var e_2, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 7, 8, 13]);
gen_2 = __asyncValues(gen);
_b.label = 1;
case 1: return [4 /*yield*/, __await(gen_2.next())];
case 2:
if (!(gen_2_1 = _b.sent(), !gen_2_1.done)) return [3 /*break*/, 6];
value = gen_2_1.value;
return [4 /*yield*/, __await(init = fn(init, value))];
case 3: return [4 /*yield*/, _b.sent()];
case 4:
_b.sent();
_b.label = 5;
case 5: return [3 /*break*/, 1];
case 6: return [3 /*break*/, 13];
case 7:
e_2_1 = _b.sent();
e_2 = { error: e_2_1 };
return [3 /*break*/, 13];
case 8:
_b.trys.push([8, , 11, 12]);
if (!(gen_2_1 && !gen_2_1.done && (_a = gen_2.return))) return [3 /*break*/, 10];
return [4 /*yield*/, __await(_a.call(gen_2))];
case 9:
_b.sent();
_b.label = 10;
case 10: return [3 /*break*/, 12];
case 11:
if (e_2) throw e_2.error;
return [7 /*endfinally*/];
case 12: return [7 /*endfinally*/];
case 13: return [2 /*return*/];
}
});
});
}
var scan = curryN(3, _scan);
var scanAsync = curryN(3, _scanAsync);
var scan$1 = /*#__PURE__*/Object.freeze({
__proto__: null,
scan: scan,
scanAsync: scanAsync
});
var accumCounters = function (fn) { return function (acc, el) {
var _a;
return (__assign(__assign({}, acc), (_a = {}, _a[fn(el)] = (acc[fn(el)] || 0) + 1, _a)));
}; };
var _countBy = function (fn) { return scan(accumCounters(fn), {}); };
var _countByAsync = function (fn) { return scanAsync(accumCounters(fn), {}); };
var countBy = uncurryN(2, _countBy);
var countByAsync = uncurryN(2, _countByAsync);
var countBy$1 = /*#__PURE__*/Object.freeze({
__proto__: null,
countBy: countBy,
countByAsync: countByAsync
});
function _debounceBy(fn, gen) {
var lastKey, gen_1, gen_1_1, value, e_1_1;
var e_1, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
lastKey = NaN;
_b.label = 1;
case 1:
_b.trys.push([1, 6, 7, 8]);
gen_1 = __values(gen), gen_1_1 = gen_1.next();
_b.label = 2;
case 2:
if (!!gen_1_1.done) return [3 /*break*/, 5];
value = gen_1_1.value;
if (!(lastKey !== (lastKey = fn(value)))) return [3 /*break*/, 4];
return [4 /*yield*/, value];
case 3:
_b.sent();
_b.label = 4;
case 4:
gen_1_1 = gen_1.next();
return [3 /*break*/, 2];
case 5: return [3 /*break*/, 8];
case 6:
e_1_1 = _b.sent();
e_1 = { error: e_1_1 };
return [3 /*break*/, 8];
case 7:
try {
if (gen_1_1 && !gen_1_1.done && (_a = gen_1.return)) _a.call(gen_1);
}
finally { if (e_1) throw e_1.error; }
return [7 /*endfinally*/];
case 8: return [2 /*return*/];
}
});
}
function _debounceByAsync(fn, gen) {
return __asyncGenerator(this, arguments, function _debounceByAsync_1() {
var lastKey, gen_2, gen_2_1, value, e_2_1;
var e_2, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
lastKey = NaN;
_b.label = 1;
case 1:
_b.trys.push([1, 8, 9, 14]);
gen_2 = __asyncValues(gen);
_b.label = 2;
case 2: return [4 /*yield*/, __await(gen_2.next())];
case 3:
if (!(gen_2_1 = _b.sent(), !gen_2_1.done)) return [3 /*break*/, 7];
value = gen_2_1.value;
if (!(lastKey !== (lastKey = fn(value)))) return [3 /*break*/, 6];
return [4 /*yield*/, __await(value)];
case 4: return [4 /*yield*/, _b.sent()];
case 5:
_b.sent();
_b.label = 6;
case 6: return [3 /*break*/, 2];
case 7: return [3 /*break*/, 14];
case 8:
e_2_1 = _b.sent();
e_2 = { error: e_2_1 };
return [3 /*break*/, 14];
case 9:
_b.trys.push([9, , 12, 13]);
if (!(gen_2_1 && !gen_2_1.done && (_a = gen_2.return))) return [3 /*break*/, 11];
return [4 /*yield*/, __await(_a.call(gen_2))];
case 10:
_b.sent();
_b.label = 11;
case 11: return [3 /*break*/, 13];
case 12:
if (e_2) throw e_2.error;
return [7 /*endfinally*/];
case 13: return [7 /*endfinally*/];
case 14: return [2 /*return*/];
}
});
});
}
var debounceBy = curryN(2, _debounceBy);
var debounceByAsync = curryN(2, _debounceByAsync);
var debounceBy$1 = /*#__PURE__*/Object.freeze({
__proto__: null,
debounceBy: debounceBy,
debounceByAsync: debounceByAsync
});
var debounce = debounceBy(identity);
var debounceAsync = debounceByAsync(identity);
var debounce$1 = /*#__PURE__*/Object.freeze({
__proto__: null,
debounce: debounce,
debounceAsync: debounceAsync
});
function _dropWhile(cond, gen) {
var taking, gen_1, gen_1_1, value, e_1_1;
var e_1, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
taking = false;
_b.label = 1;
case 1:
_b.trys.push([1, 6, 7, 8]);
gen_1 = __values(gen), gen_1_1 = gen_1.next();
_b.label = 2;
case 2:
if (!!gen_1_1.done) return [3 /*break*/, 5];
value = gen_1_1.value;
if (!(taking = taking || !cond(value))) return [3 /*break*/, 4];
return [4 /*yield*/, value];
case 3:
_b.sent();
_b.label = 4;
case 4:
gen_1_1 = gen_1.next();
return [3 /*break*/, 2];
case 5: return [3 /*break*/, 8];
case 6:
e_1_1 = _b.sent();
e_1 = { error: e_1_1 };
return [3 /*break*/, 8];
case 7:
try {
if (gen_1_1 && !gen_1_1.done && (_a = gen_1.return)) _a.call(gen_1);
}
finally { if (e_1) throw e_1.error; }
return [7 /*endfinally*/];
case 8: return [2 /*return*/];
}
});
}
function _dropWhileAsync(cond, gen) {
return __asyncGenerator(this, arguments, function _dropWhileAsync_1() {
var taking, gen_2, gen_2_1, value, e_2_1;
var e_2, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
taking = false;
_b.label = 1;
case 1:
_b.trys.push([1, 8, 9, 14]);
gen_2 = __asyncValues(gen);
_b.label = 2;
case 2: return [4 /*yield*/, __await(gen_2.next())];
case 3:
if (!(gen_2_1 = _b.sent(), !gen_2_1.done)) return [3 /*break*/, 7];
value = gen_2_1.value;
if (!(taking = taking || !cond(value))) return [3 /*break*/, 6];
return [4 /*yield*/, __await(value)];
case 4: return [4 /*yield*/, _b.sent()];
case 5:
_b.sent();
_b.label = 6;
case 6: return [3 /*break*/, 2];
case 7: return [3 /*break*/, 14];
case 8:
e_2_1 = _b.sent();
e_2 = { error: e_2_1 };
return [3 /*break*/, 14];
case 9:
_b.trys.push([9, , 12, 13]);
if (!(gen_2_1 && !gen_2_1.done && (_a = gen_2.return))) return [3 /*break*/, 11];
return [4 /*yield*/, __await(_a.call(gen_2))];
case 10:
_b.sent();
_b.label = 11;
case 11: return [3 /*break*/, 13];
case 12:
if (e_2) throw e_2.error;
return [7 /*endfinally*/];
case 13: return [7 /*endfinally*/];
case 14: return [2 /*return*/];
}
});
});
}
var dropWhile = curryN(2, _dropWhile);
var dropWhileAsync = curryN(2, _dropWhileAsync);
var dropWhile$1 = /*#__PURE__*/Object.freeze({
__proto__: null,
dropWhile: dropWhile,
dropWhileAsync: dropWhileAsync
});
function _drop(amount, gen) {
var gen_1, gen_1_1, value, e_1_1;
var e_1, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 5, 6, 7]);
gen_1 = __values(gen), gen_1_1 = gen_1.next();
_b.label = 1;
case 1:
if (!!gen_1_1.done) return [3 /*break*/, 4];
value = gen_1_1.value;
if (!(amount-- <= 0)) return [3 /*break*/, 3];
return [4 /*yield*/, value];
case 2:
_b.sent();
_b.label = 3;
case 3:
gen_1_1 = gen_1.next();
return [3 /*break*/, 1];
case 4: return [3 /*break*/, 7];
case 5:
e_1_1 = _b.sent();
e_1 = { error: e_1_1 };
return [3 /*break*/, 7];
case 6:
try {
if (gen_1_1 && !gen_1_1.done && (_a = gen_1.return)) _a.call(gen_1);
}
finally { if (e_1) throw e_1.error; }
return [7 /*endfinally*/];
case 7: return [2 /*return*/];
}
});
}
function _dropAsync(amount, gen) {
return __asyncGenerator(this, arguments, function _dropAsync_1() {
var gen_2, gen_2_1, value, e_2_1;
var e_2, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 7, 8, 13]);
gen_2 = __asyncValues(gen);
_b.label = 1;
case 1: return [4 /*yield*/, __await(gen_2.next())];
case 2:
if (!(gen_2_1 = _b.sent(), !gen_2_1.done)) return [3 /*break*/, 6];
value = gen_2_1.value;
if (!(amount-- <= 0)) return [3 /*break*/, 5];
return [4 /*yield*/, __await(value)];
case 3: return [4 /*yield*/, _b.sent()];
case 4:
_b.sent();
_b.label = 5;
case 5: return [3 /*break*/, 1];
case 6: return [3 /*break*/, 13];
case 7:
e_2_1 = _b.sent();
e_2 = { error: e_2_1 };
return [3 /*break*/, 13];
case 8:
_b.trys.push([8, , 11, 12]);
if (!(gen_2_1 && !gen_2_1.done && (_a = gen_2.return))) return [3 /*break*/, 10];
return [4 /*yield*/, __await(_a.call(gen_2))];
case 9:
_b.sent();
_b.label = 10;
case 10: return [3 /*break*/, 12];
case 11:
if (e_2) throw e_2.error;
return [7 /*endfinally*/];
case 12: return [7 /*endfinally*/];
case 13: return [2 /*return*/];
}
});
});
}
var drop = curryN(2, _drop);
var dropAsync = curryN(2, _dropAsync);
var drop$1 = /*#__PURE__*/Object.freeze({
__proto__: null,
drop: drop,
dropAsync: dropAsync
});
function _filter(cond, gen) {
var gen_1, gen_1_1, value, e_1_1;
var e_1, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 5, 6, 7]);
gen_1 = __values(gen), gen_1_1 = gen_1.next();
_b.label = 1;
case 1:
if (!!gen_1_1.done) return [3 /*break*/, 4];
value = gen_1_1.value;
if (!cond(value)) return [3 /*break*/, 3];
return [4 /*yield*/, value];
case 2:
_b.sent();
_b.label = 3;
case 3:
gen_1_1 = gen_1.next();
return [3 /*break*/, 1];
case 4: return [3 /*break*/, 7];
case 5:
e_1_1 = _b.sent();
e_1 = { error: e_1_1 };
return [3 /*break*/, 7];
case 6:
try {
if (gen_1_1 && !gen_1_1.done && (_a = gen_1.return)) _a.call(gen_1);
}
finally { if (e_1) throw e_1.error; }
return [7 /*endfinally*/];
case 7: return [2 /*return*/];
}
});
}
function _filterAsync(cond, gen) {
return __asyncGenerator(this, arguments, function _filterAsync_1() {
var gen_2, gen_2_1, value, e_2_1;
var e_2, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 7, 8, 13]);
gen_2 = __asyncValues(gen);
_b.label = 1;
case 1: return [4 /*yield*/, __await(gen_2.next())];
case 2:
if (!(gen_2_1 = _b.sent(), !gen_2_1.done)) return [3 /*break*/, 6];
value = gen_2_1.value;
if (!cond(value)) return [3 /*break*/, 5];
return [4 /*yield*/, __await(value)];
case 3: return [4 /*yield*/, _b.sent()];
case 4:
_b.sent();
_b.label = 5;
case 5: return [3 /*break*/, 1];
case 6: return [3 /*break*/, 13];
case 7:
e_2_1 = _b.sent();
e_2 = { error: e_2_1 };
return [3 /*break*/, 13];
case 8:
_b.trys.push([8, , 11, 12]);
if (!(gen_2_1 && !gen_2_1.done && (_a = gen_2.return))) return [3 /*break*/, 10];
return [4 /*yield*/, __await(_a.call(gen_2))];
case 9:
_b.sent();
_b.label = 10;
case 10: return [3 /*break*/, 12];
case 11:
if (e_2) throw e_2.error;
return [7 /*endfinally*/];
case 12: return [7 /*endfinally*/];
case 13: return [2 /*return*/];
}
});
});
}
var filter = curryN(2, _filter);
var filterAsync = curryN(2, _filterAsync);
var filter$1 = /*#__PURE__*/Object.freeze({
__proto__: null,
filter: filter,
filterAsync: filterAsync
});
var _find = function (cond, gen) {
var e_1, _a;
try {
for (var gen_1 = __values(gen), gen_1_1 = gen_1.next(); !gen_1_1.done; gen_1_1 = gen_1.next()) {
var value = gen_1_1.value;
if (cond(value))
return value;
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (gen_1_1 && !gen_1_1.done && (_a = gen_1.return)) _a.call(gen_1);
}
finally { if (e_1) throw e_1.error; }
}
return null;
};
var _findAsync = function (cond, gen) { var gen_2, gen_2_1; return __awaiter(void 0, void 0, void 0, function () {
var value, e_2_1;
var e_2, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 5, 6, 11]);
gen_2 = __asyncValues(gen);
_b.label = 1;
case 1: return [4 /*yield*/, gen_2.next()];
case 2:
if (!(gen_2_1 = _b.sent(), !gen_2_1.done)) return [3 /*break*/, 4];
value = gen_2_1.value;
if (cond(value))
return [2 /*return*/, value];
_b.label = 3;
case 3: return [3 /*break*/, 1];
case 4: return [3 /*break*/, 11];
case 5:
e_2_1 = _b.sent();
e_2 = { error: e_2_1 };
return [3 /*break*/, 11];
case 6:
_b.trys.push([6, , 9, 10]);
if (!(gen_2_1 && !gen_2_1.done && (_a = gen_2.return))) return [3 /*break*/, 8];
return [4 /*yield*/, _a.call(gen_2)];
case 7:
_b.sent();
_b.label = 8;
case 8: return [3 /*break*/, 10];
case 9:
if (e_2) throw e_2.error;
return [7 /*endfinally*/];
case 10: return [7 /*endfinally*/];
case 11: return [2 /*return*/, null];
}
});
}); };
var find = curryN(2, _find);
var findAsync = curryN(2, _findAsync);
var find$1 = /*#__PURE__*/Object.freeze({
__proto__: null,
find: find,
findAsync: findAsync
});
function _flatMap(fn, gen) {
var gen_1, gen_1_1, value, e_1_1;
var e_1, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 5, 6, 7]);
gen_1 = __values(gen), gen_1_1 = gen_1.next();
_b.label = 1;
case 1:
if (!!gen_1_1.done) return [3 /*break*/, 4];
value = gen_1_1.value;
return [5 /*yield**/, __values(fn(value))];
case 2:
_b.sent();
_b.label = 3;
case 3:
gen_1_1 = gen_1.next();
return [3 /*break*/, 1];
case 4: return [3 /*break*/, 7];
case 5:
e_1_1 = _b.sent();
e_1 = { error: e_1_1 };
return [3 /*break*/, 7];
case 6:
try {
if (gen_1_1 && !gen_1_1.done && (_a = gen_1.return)) _a.call(gen_1);
}
finally { if (e_1) throw e_1.error; }
return [7 /*endfinally*/];
case 7: return [2 /*return*/];
}
});
}
function _flatMapAsync(fn, gen) {
return __asyncGenerator(this, arguments, function _flatMapAsync_1() {
var gen_2, gen_2_1, value, e_2_1;
var e_2, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 7, 8, 13]);
gen_2 = __asyncValues(gen);
_b.label = 1;
case 1: return [4 /*yield*/, __await(gen_2.next())];
case 2:
if (!(gen_2_1 = _b.sent(), !gen_2_1.done)) return [3 /*break*/, 6];
value = gen_2_1.value;
return [5 /*yield**/, __values(__asyncDelegator(__asyncValues(fn(value))))];
case 3: return [4 /*yield*/, __await.apply(void 0, [_b.sent()])];
case 4:
_b.sent();
_b.label = 5;
case 5: return [3 /*break*/, 1];
case 6: return [3 /*break*/, 13];
case 7:
e_2_1 = _b.sent();
e_2 = { error: e_2_1 };
return [3 /*break*/, 13];
case 8:
_b.trys.push([8, , 11, 12]);
if (!(gen_2_1 && !gen_2_1.done && (_a = gen_2.return))) return [3 /*break*/, 10];
return [4 /*yield*/, __await(_a.call(gen_2))];
case 9:
_b.sent();
_b.label = 10;
case 10: return [3 /*break*/, 12];
case 11:
if (e_2) throw e_2.error;
return [7 /*endfinally*/];
case 12: return [7 /*endfinally*/];
case 13: return [2 /*return*/];
}
});
});
}
var flatMap = curryN(2, _flatMap);
var flatMapAsync = curryN(2, _flatMapAsync);
var flatMap$1 = /*#__PURE__*/Object.freeze({
__proto__: null,
flatMap: flatMap,
flatMapAsync: flatMapAsync
});
/* eslint-disable no-plusplus,no-console */
var generate = function (fn, limit) { return ({
start: function () {
var i;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
i = 0;
_a.label = 1;
case 1:
if (!(!limit || i < limit)) return [3 /*break*/, 4];
return [4 /*yield*/, fn(i)];
case 2:
_a.sent();
_a.label = 3;
case 3:
i++;
return [3 /*break*/, 1];
case 4:
console.log("The stream has reached the limit of " + limit + " elements");
return [2 /*return*/];
}
});
},
}); };
var generateAsync = function (fn, limit) { return ({
start: function () {
return __asyncGenerator(this, arguments, function () {
var i;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
i = 0;
_a.label = 1;
case 1:
if (!(!limit || i < limit)) return [3 /*break*/, 6];
return [4 /*yield*/, __await(fn(i))];
case 2: return [4 /*yield*/, __await.apply(void 0, [_a.sent()])];
case 3: return [4 /*yield*/, _a.sent()];
case 4:
_a.sent();
_a.label = 5;
case 5:
i++;
return [3 /*break*/, 1];
case 6:
console.log("The stream has reached the limit of " + limit + " elements");
return [2 /*return*/];
}
});
});
},
}); };
var generateFromArray = function (v) { return ({
start: function () {
var v_1, v_1_1, el, e_1_1;
var e_1, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 5, 6, 7]);
v_1 = __values(v), v_1_1 = v_1.next();
_b.label = 1;
case 1:
if (!!v_1_1.done) return [3 /*break*/, 4];
el = v_1_1.value;
return [4 /*yield*/, el];
case 2:
_b.sent();
_b.label = 3;
case 3:
v_1_1 = v_1.next();
return [3 /*break*/, 1];
case 4: return [3 /*break*/, 7];
case 5:
e_1_1 = _b.sent();
e_1 = { error: e_1_1 };
return [3 /*break*/, 7];
case 6:
try {
if (v_1_1 && !v_1_1.done && (_a = v_1.return)) _a.call(v_1);
}
finally { if (e_1) throw e_1.error; }
return [7 /*endfinally*/];
case 7: return [2 /*return*/];
}
});
},
}); };
var generate$1 = /*#__PURE__*/Object.freeze({
__proto__: null,
generate: generate,
generateAsync: generateAsync,
generateFromArray: generateFromArray
});
var head = function (gen) { var _a; return (_a = gen.next()) === null || _a === void 0 ? void 0 : _a.value; };
var headAsync = function (gen) { return __awaiter(void 0, void 0, void 0, function () { var _a; return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, gen.next()];
case 1: return [2 /*return*/, (_a = (_b.sent())) === null || _a === void 0 ? void 0 : _a.value];
}
}); }); };
var head$1 = /*#__PURE__*/Object.freeze({
__proto__: null,
head: head,
headAsync: headAsync
});
function last(gen) {
var e_1, _a;
var lastElement;
try {
for (var gen_1 = __values(gen), gen_1_1 = gen_1.next(); !gen_1_1.done; gen_1_1 = gen_1.next()) {
var value = gen_1_1.value;
lastElement = value;
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (gen_1_1 && !gen_1_1.done && (_a = gen_1.return)) _a.call(gen_1);
}
finally { if (e_1) throw e_1.error; }
}
return lastElement;
}
function lastAsync(gen) {
var gen_2, gen_2_1;
var e_2, _a;
return __awaiter(this, void 0, void 0, function () {
var lastElement, value, e_2_1;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 5, 6, 11]);
gen_2 = __asyncValues(gen);
_b.label = 1;
case 1: return [4 /*yield*/, gen_2.next()];
case 2:
if (!(gen_2_1 = _b.sent(), !gen_2_1.done)) return [3 /*break*/, 4];
value = gen_2_1.value;
lastElement = value;
_b.label = 3;
case 3: return [3 /*break*/, 1];
case 4: return [3 /*break*/, 11];
case 5:
e_2_1 = _b.sent();
e_2 = { error: e_2_1 };
return [3 /*break*/, 11];
case 6:
_b.trys.push([6, , 9, 10]);
if (!(gen_2_1 && !gen_2_1.done && (_a = gen_2.return))) return [3 /*break*/, 8];
return [4 /*yield*/, _a.call(gen_2)];
case 7:
_b.sent();
_b.label = 8;
case 8: return [3 /*break*/, 10];
case 9:
if (e_2) throw e_2.error;
return [7 /*endfinally*/];
case 10: return [7 /*endfinally*/];
case 11: return [2 /*return*/, lastElement];
}
});
});
}
var last$1 = /*#__PURE__*/Object.freeze({
__proto__: null,
last: last,
lastAsync: lastAsync
});
function _map(fn, gen) {
var gen_1, gen_1_1, value, e_1_1;
var e_1, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 5, 6, 7]);
gen_1 = __values(gen), gen_1_1 = gen_1.next();
_b.label = 1;
case 1:
if (!!gen_1_1.done) return [3 /*break*/, 4];
value = gen_1_1.value;
return [4 /*yield*/, fn(value)];
case 2:
_b.sent();
_b.label = 3;
case 3:
gen_1_1 = gen_1.next();
return [3 /*break*/, 1];
case 4: return [3 /*break*/, 7];
case 5:
e_1_1 = _b.sent();
e_1 = { error: e_1_1 };
return [3 /*break*/, 7];
case 6:
try {
if (gen_1_1 && !gen_1_1.done && (_a = gen_1.return)) _a.call(gen_1);
}
finally { if (e_1) throw e_1.error; }
return [7 /*endfinally*/];
case 7: return [2 /*return*/];
}
});
}
function _mapAsync(fn, gen) {
return __asyncGenerator(this, arguments, function _mapAsync_1() {
var gen_2, gen_2_1, value, e_2_1;
var e_2, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 7, 8, 13]);
gen_2 = __asyncValues(gen);
_b.label = 1;
case 1: return [4 /*yield*/, __await(gen_2.next())];
case 2:
if (!(gen_2_1 = _b.sent(), !gen_2_1.done)) return [3 /*break*/, 6];
value = gen_2_1.value;
return [4 /*yield*/, __await(fn(value))];
case 3: return [4 /*yield*/, _b.sent()];
case 4:
_b.sent();
_b.label = 5;
case 5: return [3 /*break*/, 1];
case 6: return [3 /*break*/, 13];
case 7:
e_2_1 = _b.sent();
e_2 = { error: e_2_1 };
return [3 /*break*/, 13];