@effectful/transducers
Version:
JS syntax transformation framework for @effectful/js
147 lines (140 loc) • 4.74 kB
JavaScript
;
exports.__esModule = true;
exports.cons = cons;
exports.curry = curry;
exports.curryN = curryN;
exports.forEach = exports.flatMap = exports.filter = void 0;
exports.group = group;
exports.groupUniq = groupUniq;
exports.groupWith = groupWith;
exports.map = void 0;
exports.mapAdd = mapAdd;
exports.mapArr = mapArr;
exports.mapPush = mapPush;
exports.pipe = pipe;
exports.reverse = reverse;
var _config = require("./config");
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
/**
* similar to Ramda.curryN, likely less efficient,
* but Ramda dependency can be dropped
*/
function curryN(num, fun) {
function step(args) {
return function curryStep() {
const next = args.concat(Array.from(arguments));
return next.length >= num ? fun.apply(undefined, next) : step(next);
};
}
return step([]);
}
/**
* similar to Ramda.curry, likely less efficient,
* but Ramda dependency can be dropped
*/
function curry(fun, trace) {
return curryN(fun.length, fun, trace);
}
const filter = exports.filter = curry(function* filter(pred, s) {
for (const i of s) if (pred(i)) yield i;
});
const flatMap = exports.flatMap = curry(function* flatMap(act, s) {
for (const i of s) yield* act(i);
});
const map = exports.map = curry(function* map(fun, s) {
for (const i of s) yield fun(i);
});
const forEach = exports.forEach = curry(function forEach(act, s) {
for (const i of s) act(i);
});
/**
* reverse array iterator
*/
function reverse(arr) {
arr = Array.isArray(arr) ? arr : Array.from(arr);
let i = arr.length;
return {
[Symbol.iterator]() {
return {
next() {
return i === 0 ? {
value: null,
done: true
} : {
value: arr[--i],
done: false
};
}
};
}
};
}
/**
* adds value `v` to an Array in a map `m` value by `k`
* creates the Array if needed
*/
function mapPush(m, k, v) {
let l = m.get(k);
if (l == null) m.set(k, l = []);
l.push(v);
}
/**
* Returns Map's `m` value by `k` if it doesn't exist inserts an empty array,
* and returns it.
*/
function mapArr(m, k) {
let l = m.get(k);
if (l == null) m.set(k, l = []);
return l;
}
/**
* adds value `v` to a Set in a map `m` value by `k`
* creates the Set if needed
*/
function mapAdd(m, k, v) {
let l = m.get(k);
if (l == null) m.set(k, l = new Set());
l.add(v);
}
function groupWith /*::<K,V,W>*/(i /*:Iterable<[K,V]>*/, accum /*:(w:W,v:V,k?:K) => W*/, init /*::?: (k?:K) => W*/) {
/*: Map<K,W>*/const ret /*:Map<K,W>*/ = new Map();
if (init == null) init = () => null;
for (const [k, v] of i) {
const l = ret.get(k);
ret.set(k, l == null ? accum(init(k), v, k) : accum(l, v, k));
}
return ret;
}
function group /*::<K,V>*/(i /*:Iterable<[K,V]>*/) /*: Map<K,V[]>*/{
const ret /*:Map<K,V[]>*/ = new Map();
for (const [k, v] of i) mapPush(ret, k, v);
return ret;
// return groupWith<K,V,V[]>(i,(w:V[],v:V) => (w.push(v),w), () => [])
}
function groupUniq /*::<K,V>*/(i /*:Iterable<[K,V]>*/) /*: Map<K,Set<V>>*/{
const ret /*:Map<K,V[]>*/ = new Map();
for (const [k, v] of i) mapAdd(ret, k, v);
return ret;
// return groupWith<K,V,V[]>(i,(v:V,w:V[]) => (w.add(v),w), () => new Set())
}
/**
* similar to Ramda.pipe, likely less efficient,
* but Ramda dependency can be dropped
*/
function pipe() {
var args = arguments;
if (_config.default.debug && !_toConsumableArray(args).every(Boolean)) throw new Error(`pipe with an empty function`);
return function pipeImpl(cur) {
for (let i = 0, len = args.length; i < len; ++i) cur = args[i](cur);
return cur;
};
}
function* cons(a, s) {
yield a;
yield* s;
}