UNPKG

@beenotung/tslib

Version:
69 lines (68 loc) 1.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Chain = exports.pipe = void 0; exports.peek = peek; exports.createChain = createChain; const curry_1 = require("./curry"); /** * the function<A,B> :: A,...* -> B * - first argument must be type A * - other arguments can be any type * - must return type B * * @example * pipe([ * [ x=>x+2 ] * , [ (x,y)=>x*2+y, [5] ] * ]) (2) ~> 13 * */ /** * auto curried Elixir style pipe * * pipe :: PipeArg p => [p] -> a -> * * - don't have to be of same type (like chain) * */ exports.pipe = (0, curry_1.curry)((ps, acc) => { for (const p of ps) { if (p.length === 2) { // has extra args acc = p[0](acc, ...p[1]); } else { // no extra args acc = p[0](acc); } } return acc; }); /** * non-curried version of echoF in functional.ts * * echo :: (a->*) -> a -> a * */ function peek(f) { return a => { f(a); return a; }; } class Chain { value; constructor(value) { this.value = value; } use(f) { f(this.value); return this; } map(f) { return new Chain(f(this.value)); } unwrap() { return this.value; } } exports.Chain = Chain; function createChain(a) { return new Chain(a); }