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AY,UAC3B,GAeT,QAAQ,SAAW,GAAS,IAAI,IAAS,QAAQ,KAC/C,CAAC,EAAO,IAAU,KAAS,GAAM,CAAC,KAAM,IAAM,EAAI,EAAM,GAAK,KAAS,OAUxE,QAAQ,aAAe,GAAkB,IAAI,IAAS,CAAC,EAAO,IAAU,KAAkB,GAAM,KAAK,EAAO,GAM5G,QAAQ,YAAc,IACpB,IAAI,EAAM,GAEV,OADA,GAAK,IAAI,IAAS,EAAI,KAAK,KACpB,GAIT,OAAO,QAAU,IACZ,QAAQ,cACR,QACH,kBAAU,UACV,MAAI,QAAK,YAAO,cAAQ,YAAO","file":"/npm/cpsfy@6.1.2/index.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.37.0.\n * Original file: /npm/cpsfy@6.1.2/index.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","const { isNil, mergeArray, inheritPrototype } = require('./utils')\n\n/* ----- General purpose utils ----- */\n\nexports.curryGroups2 = f => (...a) => (...b) => f(...a, ...b)\n\nexports.curryGroupsN = n => f => {\n  while(--n > 0) {\n    f = exports.curryGroups2(f)\n  }\n  return f\n}\n\n/**\n * Transform `reducer: (state,next) => state` into update function inside closure.\n * \n * @param {Function} reducer: (state,next) -> state\n * @returns {Function} update(reducer): initState -> (x0,...,xn) -> reducer(prevState,x0,...,xn)\n * \n * @example updater = update((state,x) => state+x)\n * acc = updater(5) // initialize updater function with initial state = 5\n * acc(2) //=> 5+2\n * acc(2) //=> 5+2+2\n * acc(4) //=> 5+2+2+4\n */\nexports.update = reducer => state => (...vals) => {state = reducer(state, ...vals); return state}\n\n/**\n * Same as `update` but spread return values of curried reducer.\n * This allows to update tuples rather than single values as `update` does.\n * Reducer is curried for clean separation of arguments.\n * As JavaScript has no tuples, use arrays instead.\n * \n * @param {Function} reducer: (s0,...,sn) -> (x0,...,xm) -> (s0,...,sn)\n * @returns {Function} updateSpread(reducer): (s0,...,sn) -> (x0,...,xn) -> reducer(s0,...,sn)(x0,...,xm)\n */\nexports.updateSpread = reducer => (...state) => (...vals) => {state = reducer(...state)(...vals); return state}\n\n/**\n * Pass tuple of values to sequence of functions similar to UNIX pipe\n * `(x0,...,xn) | f0 | f1 | ... | fm`.\n *\n * @param {...*} args - tuple of arbitrary values.\n * @param {...Function} fns - functions `(f0,f1,...,fn)`.\n * @returns {*} `pipeline(...args)(...fns)`\n *    - Result of functions applied one after another, equivalent to\n *    `fn(...f1(f0(...args))...)`\n *\n * @example\n * pipeline(x,y)(f0,f1,f2)\n *   // is equivalent to\n * f2(f1(f0(x,y)))\n */\nconst pipeline = (...args) => (...fns) => fns.slice(1).reduce(\n  (acc, fn) => fn(acc),\n  fns[0](...args)\n)\n\n/**\n * Compose functions left to right as in ramda's `pipe`.\n * \n * @param {...Function} fns - functions `(f0,f1,...,fn)`.\n * @returns {*} `pipe(...fns)`\n *    - Composite function `(...args) => fn(...f1(f0(...args))...)`.\n * \n * @example\n * pipe((a,b)=>a+b, x=>x*2)\n *   // is equivalent to\n * (a,b)=>(a+b)*2\n * \n */\nconst pipe = (...fns) => (...args) => pipeline(...args)(...fns)\n\n\n/* ----- CPS operators ----- */\n\n/**\n * Create CPS function with provided tuple as immediate output.\n *\n * @param {...*} (x0,...,xn) - tuple of arbitrary values.\n * @returns {Function} `of(x1,...,xn)` - CPS function\n *    that immediately calls it 1st callback `cb` with outputs `(x0,...,xn)`.\n *    No other callback is called. (For multi-callback version see `ofN`.)\n *\n * @example\n * of(x0,x1,x2)\n *   // is equivalent to the CPS function\n * cb => cb(x0,x1,x2)\n *\n */\nconst of = (...args) => cb => cb(...args)\n\n\n/**\n * Multi-callback version of the `of` operator,\n * passing provided tuple into the nth callback.\n *\n * @param {Number} n - position number of the callback used.\n * @param {...*} args - tuple of arbitrary values.\n * @returns {Function} `ofN(n)(...args)` - CPS function\n *    that outputs `(...args)`` into its nth callback\n *    no other output is passed to any other callback.\n *\n * @example\n * ofN(1)(x0,x1)\n *   // is equivalent to the CPS function\n * (cb0,cb1) => cb1(x0,x1)\n */\nconst ofN = n => (...args) => (...cbs) => cbs[n](...args)\n\n\n\n/**\n * Chain is the most basic CPS operator.\n * It chains outputs of CPS function with\n * tuple of functions returning CPS functions,\n * where the nth function applies to each output from the nth callback\n * and the resulting outputs are gathered by index.\n * If fewever functions are passed in the tuple,\n * outputs from remaining callbacks are preserved unchanged.\n *\n * @signature (...fns) -> CPS -> CPS\n *\n * @param {...Function} (f0,...,fn)\n *    - tuple of functions, each returning CPS function.\n * @param {Function} cpsFn - CPS function.\n * @returns {Function} `chain(f0,...,fn)(cpsFn)`\n *    - CPS function whose nth callback's output is gathered from\n *    the nth callback's outputs of each function fns[j] for each j\n *    evaluated for each output of the jth callback of `cpsFn`.\n *    If 'fns' has fewever functions than the number of callbacks passed,\n *    the extra callbacks receive the same output as from cpsFn\n *\n * @example\n *   // callbacks `cb0,cb1` receive outputs respectively `(2,3)` and `(7,9)`\n * const cpsFn = (cb1,cb2) => {cb1(2,3); cb2(7,9)}\n * const f1 = (x,y) => (cb1,cb2) => {cb1(x+y); cb2(x-y)}\n * const f2 = (x,y) => cb => {cb(x,-y)}\n *\n * chain(f1,f2)(cpsFn)\n *   // cpsFn -> (2,3) -> f1 -> (2+3) -> c1  /->(7,-9) -> c1\n *   //      \\              \\-> (2-3) -> c2 /\n *   //      \\-> (7,9) -> f2 -------------- \n *   // is equivalent to the CPS function\n * (c1,c2) => {c1(2+3); c2(2-3); c1(7,-9)}\n *\n * @example\n *   // convert to CPS function with 2 callbacks\n * const readFile = file => (onRes, onErr) =>\n *   fs.readFile(file, (e, name) => {\n *     e ? onErr(e) : onRes(name)\n *   })\n * const readName = readFile('index.txt') // CPS function\n *\n * const readFileByName = chain(name => readFile(name))(readName)\n *   // or equivalently\n * const readFileByName = chain(readFile)(readName)\n *\n */\nconst chain = (...fns) => cpsFn => {\n  fns = fns.map((f,ind) => isNil(f) ? ofN(ind) : f)\n  let cpsNew = (...cbs) => {\n    // all callbacks from the chain get passed to each cpsFn\n    let newCallbacks = fns.map(f =>\n      (...args) => f(...args)(...cbs)\n    )\n    // add missing callbacks unchanged from the same positions\n    return cpsFn(...mergeArray(newCallbacks, cbs))\n  }\n  inheritPrototype(cpsNew, cpsFn)\n  return cpsNew\n}\n\n\n\n/**\n * Map CPS function over arbitrary tuple of functions, where for each n,\n * the nth function from the tuple transforms the output of the nth callback.\n * If fewever functions are passed in the tuple,\n * outputs from remaining callbacks are preserved unchanged.\n * The pair `(map,of)` conforms to the Pointed Functor spec,\n * see {@link https://stackoverflow.com/a/41816326/1614973}.\n *\n * @signature (...fns) -> CPS -> CPS\n *\n * @param {...Function} (f0,...,fn) - tuple of functions.\n * @param {Function} cpsFn - CPS function.\n * @returns {function} `map(f0,...,fn)`\n *  - function taking CPS function `cpsFn`\n *    and returning new CPS function whose nth callback's output equals\n *    the jth callback's output of `cpsFun` transformed with function `fj`.\n *    If `fj` is undefined or null, the output is passed unchanged.\n *\n * @example\n *   // 2 callbacks receive respective outputs (2,3) and (7)\n * const cpsFn = (cb0,cb1) => {cb0(2,3); cb1(7)}\n * const f0 = (x,y) => x+y\n * const f1 = z => z*6\n * map(f0,f1)(cpsFn)\n *   // is equivalent to the CPS function\n * (cb0,cb1) => {cb0(2+3); cb1(f1(7*6))}\n *\n * @example\n * const cpsFromPromise = promise => (onRes,onErr) => promise.then(onRes,onErr)\n * map(f0,f1)(cpsFromPromise(promise))\n *   // is equivalent to\n * cpsFromPromise(promise.then(f0).catch(f1))\n */\n// precompose every callback with fn from array matched by index\n// if no function provided, default to the identity\nexports.map = (...fns) => chain(...fns.map((f,idx) => isNil(f) ? null :\n  (...args) => ofN(idx)(f(...args))\n))\n\n\n/**\n * Same as `map` but spread return values of transforming functions.\n * This allows to transform output tuples into tuples rather than single values as `map` does.\n * As JavaScript has no tuples, use arrays instead.\n * \n * @example\n *   // 1 callback receives output `(2,3)`\n * const cpsFn = cb => cb(2,3)\n *   // `f` transforms `(x,y)` to `(x+y,x-y)` (written as array)\n * const f = (x,y) => [x+y,x-y]\n * map(f)(cpsFn)\n *   // is equivalent to the CPS function\n * cb => cb(2+3,2-3)\n */\nexports.mapSpread = (...fns) => chain(...fns.map((f,idx) =>\n  (...args) => ofN(idx)(...f(...args))\n))\n\n\n\n/**\n * Filter outputs making predicates `(pred0,...,predn)` truthy.\n *  Pass through only outputs from jth callback making `predj` truthy.\n */\nconst filter = (...preds) => {\n  // call `chain` with the list of functions, one per each predicate\n  let transformer = (pred, idx) => (...inputs) =>\n    (...cbs) => (pred(...inputs)) && cbs[idx](...inputs)\n  return chain(...preds.map(transformer))\n}\n\n\n/**\n * Iterate tuple of reducers over tuple of vals\n * and outputs from CPS function regarded as actions.\n * `reducers` and `vals` are matched by index.\n *\n * @signature (...reducers, init) -> cpsAction -> cpsState\n *\n * @param {...Function} reducers\n *    - functions of the form `red = (acc, ...vals) => newAcc`\n * @param {*} init - initial value for the iteration.\n * @param {Function} cpsFn - CPS function.\n * @returns {Function} `scan(...reducers, init)(cpsFn)`\n *    - CPS function whose output from the first callback\n *   is the accumd value. For each output `(y1, y2, ...)`\n *   from the `n`th callback of `cpsFn, the `n`th reducer `redn`\n *   is used to compute the new acculated value\n *   `redn(acc, y1, y2, ...)`, where `acc` starts with `init`,\n *   similar to `reduce`.\n */\n// const scan = (...args) => {\n//   if (args.length < 2) throw Error(`Scan needs at least 2 args, curently: ${JSON.stringify(args)}`)\n//   let reducers = args.slice(0,-1),\n//     acc = args.at(-1)\n//   // chain receives tuple of functions, one per reducer\n//   // nth CPS function inside chain receives nth callback output of cpsAction\n//   let cpsTrasformer = reducer => isNil(reducer) ? undefined : (...action) => cb => {\n//       // accessing vals and reducers by index\n//       acc = reducer(acc, ...action)\n//       cb(acc)\n//     }\n//   // chaining outputs of cpsAction with multiple reducers, one per state\n//   return chain(...reducers.map(cpsTrasformer))\n// }\n\nexports.scan = (...initStates) => (...reducers) => exports.map(...reducers.map(\n  (reducer,idx) => isNil(reducer) ? null : exports.update(reducer)(initStates[idx])\n))\n\n// simplified scan dropping the seed\nconst scanS = (...args) => exports.scan()(...args)\n\n\n/**\n * Ap is the core operator to run CPS functions in parallel.\n * It applies functions to values,\n * where both functions and values are delivered separately as CPS outputs.\n *\n * @signature (...fns) -> CPS -> CPS (curried)\n *\n * @param {...Function} fns\n *    - tuple of CPS functions, each returning a function.\n * @param {Function} cpsFn - CPS function.\n * @returns {Function} `ap(...fns)(cpsFn)`\n *    - CPS function whose nth callback's output is\n *    the results of function call `f(...args)`, where\n *    function `f` is the latest nth callback's output from fns[j] for some j\n *    and `(...args)` is the latest output from the jth callback of `cpsFn`.\n *    Only the latest outputs are stored for each callback\n *    and no output is emitted unless both function and arguments are available.\n *\n * @example\n * const readFile = file => (onRes, onErr) =>\n *   fs.readFile(file, (e, name) => {\n *     e ? onErr(e) : onRes(name)\n *   })\n * const appendToFile = cb => addition => {\n *    readFile('old.txt')(content => cb(content + addition))\n * }\n *\n * const readFilesCombined = ap(appendToFile)(readFile('new.txt'))\n * readFilesCombined(res => console.log(res), err => console.err(err))\n *\n */\nconst ap = (...fns) => cpsFn => {\n  let fCache = {},\n    argsCache = {}\n  let cpsNew = (...cbs) => {\n    let newCallbacks = fns.map((f, idxF) => (...output) => {\n      argsCache[idxF] = output\n      if (fCache[idxF]) {\n        Object.keys(fCache[idxF]).forEach(idxCb =>\n        cbs[idxCb](fCache[idxF][idxCb](...output))\n      )}\n    })\n    fns.forEach((f, idxF) => f(...cbs.map((cb, idxCb) => outputF => {\n      if(!fCache[idxF]) fCache[idxF] = {}\n      fCache[idxF][idxCb] = outputF\n      // look over previously cached arguments from cpsFn\n      let output = argsCache[idxF]\n      if (output) cb(outputF(...output))\n    })))\n    // add missing callbacks from the same positions\n    return cpsFn(...mergeArray(newCallbacks, cbs))\n  }\n  inheritPrototype(cpsNew, cpsFn)\n  return cpsNew\n}\n\n\n/**\n * Lift binary function to act on values wraped inside CPS functions\n */\nexports.lift2 = f => (F1, F2) => pipeline(F2)(\n  ap(exports.map(exports.curryGroupsN(2)(f))(F1))\n)\n\n\n\n/* ----- CPS methods ----- */\n\nconst apply2this = fn =>\n  function(...args) {return fn(...args)(this)}\n\n// apply function to all values of object\nconst objMap = fn => obj =>\n  Object.keys(obj).reduce((acc, key) => {\n    acc[key] = fn(obj[key])\n    return acc\n  }, {})\n\n// Prototype methods\nconst protoObj = objMap(apply2this)({\n  ...exports,\n  chain,\n  filter,\n})\n\n/**\n * Wraps CPS function into object providing all CPS operators as methods\n */\nexports.CPS = cpsFn => {\n  // clone the function\n  let cpsWrapped = (...args) => cpsFn(...args)\n  Object.setPrototypeOf(cpsWrapped, protoObj)\n  return cpsWrapped\n}\n\n\n\n/* ------- CPS utils ------ */\n\n/**\n * Convert NodeJS function to CPS factory\n * \n * @param {Function} nodeF - function with Node style callback `cb` as last argument:\n *     cb(error, result)\n * @returns {Function} node2cps(nodeF) - CPS factory function taking all args but last\n *     that returns CPS function with 2 callbacks similar to Promise\n */\nexports.node2cps = nodeF => (...args) => exports.CPS(\n  (onRes, onErr) => nodeF(...args, (e, ...x) => e ? onErr(e) : onRes(...x))\n)\n\n/**\n * Convert Promise factory to CPS factory\n *     makes promise lazy by defering promise creation\n * \n * @param {Function} promiseFactory - function that returns Promise\n * @returns {Function} promiseF2cps(promiseFactory) - CPS factory function\n */\nexports.promiseF2cps = promiseFactory => (...args) => (onRes, onErr) => promiseFactory(...args).then(onRes, onErr)\n\n/**\n * convert syncrounous outputs of CPS function to array of output arrays\n *  output (x1,...,xn) in jth callback adds [x1,...,xn] or x1 if n=1 to jth\n */\nexports.cpsSync2arr = cpsF => {\n  let arr = []\n  cpsF((...args) => arr.push(args))\n  return arr\n}\n\n\nmodule.exports = {\n  ...require('./utils'),\n  ...exports,\n  pipeline, pipe,\n  of, ofN, chain, filter, scanS, ap,\n}\n"]}