/** @module function/pipe.ts */ import { toArray } from '../array/toArray' import { ExtractFunctionArguments, ExtractFunctionReturnValue } from '../helper-types' export function pipe( entry: Fn ): ( ...args: ExtractFunctionArguments ) => ExtractFunctionReturnValue export function pipe( entry: Fn, first: ( a: ExtractFunctionReturnValue ) => A ): ( ...args: ExtractFunctionArguments ) => A export function pipe( entry: Fn, first: ( a: ExtractFunctionReturnValue ) => A , ab: ( a: A ) => B ): ( ...args: ExtractFunctionArguments ) => B export function pipe( entry: Fn, first: ( a: ExtractFunctionReturnValue ) => A , ab: ( a: A ) => B, bc: ( b: B ) => C ): ( ...args: ExtractFunctionArguments ) => C export function pipe( entry: Fn, first: ( a: ExtractFunctionReturnValue ) => A , ab: ( a: A ) => B, bc: ( b: B ) => C, cd: ( c: C ) => D ): ( ...args: ExtractFunctionArguments ) => D export function pipe( entry: Fn, first: ( a: ExtractFunctionReturnValue ) => A , ab: ( a: A ) => B, bc: ( b: B ) => C, cd: ( c: C ) => D, de: ( d: D ) => E ): ( ...args: ExtractFunctionArguments ) => E export function pipe( entry: Fn, first: ( a: ExtractFunctionReturnValue ) => A , ab: ( a: A ) => B, bc: ( b: B ) => C, cd: ( c: C ) => D, de: ( d: D ) => E, ef: ( e: E ) => F ): ( ...args: ExtractFunctionArguments ) => F export function pipe( entry: Fn, first: ( a: ExtractFunctionReturnValue ) => A , ab: ( a: A ) => B, bc: ( b: B ) => C, cd: ( c: C ) => D, de: ( d: D ) => E, ef: ( e: E ) => F, fg: ( f: F ) => G ): ( ...args: ExtractFunctionArguments ) => G export function pipe( entry: Fn, first: ( a: ExtractFunctionReturnValue ) => A , ab: ( a: A ) => B, bc: ( b: B ) => C, cd: ( c: C ) => D, de: ( d: D ) => E, ef: ( e: E ) => F, fg: ( f: F ) => G, gh: ( g: G ) => H ): ( ...args: ExtractFunctionArguments ) => H export function pipe( entry: Fn, first: ( a: ExtractFunctionReturnValue ) => A , ab: ( a: A ) => B, bc: ( b: B ) => C, cd: ( c: C ) => D, de: ( d: D ) => E, ef: ( e: E ) => F, fg: ( f: F ) => G, gh: ( g: G ) => H, hi: ( h: H ) => I ): ( ...args: ExtractFunctionArguments ) => I export function pipe( entry: Fn, first: ( a: ExtractFunctionReturnValue ) => A , ab: ( a: A ) => B, bc: ( b: B ) => C, cd: ( c: C ) => D, de: ( d: D ) => E, ef: ( e: E ) => F, fg: ( f: F ) => G, gh: ( g: G ) => H, hi: ( h: H ) => I, ij: ( i: I ) => J ): ( ...args: ExtractFunctionArguments ) => J export function pipe( entry: Fn, first: ( a: ExtractFunctionReturnValue ) => A , ab: ( a: A ) => B, bc: ( b: B ) => C, cd: ( c: C ) => D, de: ( d: D ) => E, ef: ( e: E ) => F, fg: ( f: F ) => G, gh: ( g: G ) => H, hi: ( h: H ) => I, ij: ( i: I ) => J, jk: ( j: J ) => K ): ( ...args: ExtractFunctionArguments ) => K export function pipe( entry: Fn, first: ( a: ExtractFunctionReturnValue ) => A , ab: ( a: A ) => B, bc: ( b: B ) => C, cd: ( c: C ) => D, de: ( d: D ) => E, ef: ( e: E ) => F, fg: ( f: F ) => G, gh: ( g: G ) => H, hi: ( h: H ) => I, ij: ( i: I ) => J, jk: ( j: J ) => K, kl: ( k: K ) => L ): ( ...args: ExtractFunctionArguments ) => L export function pipe( entry: Fn, first: ( a: ExtractFunctionReturnValue ) => A , ab: ( a: A ) => B, bc: ( b: B ) => C, cd: ( c: C ) => D, de: ( d: D ) => E, ef: ( e: E ) => F, fg: ( f: F ) => G, gh: ( g: G ) => H, hi: ( h: H ) => I, ij: ( i: I ) => J, jk: ( j: J ) => K, kl: ( k: K ) => L, lm: ( l: L ) => M ): ( ...args: ExtractFunctionArguments ) => M export function pipe( entry: Fn, first: ( a: ExtractFunctionReturnValue ) => A , ab: ( a: A ) => B, bc: ( b: B ) => C, cd: ( c: C ) => D, de: ( d: D ) => E, ef: ( e: E ) => F, fg: ( f: F ) => G, gh: ( g: G ) => H, hi: ( h: H ) => I, ij: ( i: I ) => J, jk: ( j: J ) => K, kl: ( k: K ) => L, lm: ( l: L ) => M, mn: ( m: M ) => N ): ( ...args: ExtractFunctionArguments ) => N export function pipe( entry: Fn, first: ( a: ExtractFunctionReturnValue ) => A , ab: ( a: A ) => B, bc: ( b: B ) => C, cd: ( c: C ) => D, de: ( d: D ) => E, ef: ( e: E ) => F, fg: ( f: F ) => G, gh: ( g: G ) => H, hi: ( h: H ) => I, ij: ( i: I ) => J, jk: ( j: J ) => K, kl: ( k: K ) => L, lm: ( l: L ) => M, mn: ( m: M ) => N, no: ( n: N ) => O ): ( ...args: ExtractFunctionArguments ) => O export function pipe( entry: Fn, first: ( a: ExtractFunctionReturnValue ) => A , ab: ( a: A ) => B, bc: ( b: B ) => C, cd: ( c: C ) => D, de: ( d: D ) => E, ef: ( e: E ) => F, fg: ( f: F ) => G, gh: ( g: G ) => H, hi: ( h: H ) => I, ij: ( i: I ) => J, jk: ( j: J ) => K, kl: ( k: K ) => L, lm: ( l: L ) => M, mn: ( m: M ) => N, no: ( n: N ) => O, op: ( o: O ) => P ): ( ...args: ExtractFunctionArguments ) => P export function pipe( entry: Fn, first: ( a: ExtractFunctionReturnValue ) => A , ab: ( a: A ) => B, bc: ( b: B ) => C, cd: ( c: C ) => D, de: ( d: D ) => E, ef: ( e: E ) => F, fg: ( f: F ) => G, gh: ( g: G ) => H, hi: ( h: H ) => I, ij: ( i: I ) => J, jk: ( j: J ) => K, kl: ( k: K ) => L, lm: ( l: L ) => M, mn: ( m: M ) => N, no: ( n: N ) => O, op: ( o: O ) => P, pq: ( p: P ) => Q ): ( ...args: ExtractFunctionArguments ) => Q export function pipe( entry: Fn, first: ( a: ExtractFunctionReturnValue ) => A , ab: ( a: A ) => B, bc: ( b: B ) => C, cd: ( c: C ) => D, de: ( d: D ) => E, ef: ( e: E ) => F, fg: ( f: F ) => G, gh: ( g: G ) => H, hi: ( h: H ) => I, ij: ( i: I ) => J, jk: ( j: J ) => K, kl: ( k: K ) => L, lm: ( l: L ) => M, mn: ( m: M ) => N, no: ( n: N ) => O, op: ( o: O ) => P, pq: ( p: P ) => Q, qr: ( q: Q ) => R ): ( ...args: ExtractFunctionArguments ) => R export function pipe( entry: Fn, first: ( a: ExtractFunctionReturnValue ) => A , ab: ( a: A ) => B, bc: ( b: B ) => C, cd: ( c: C ) => D, de: ( d: D ) => E, ef: ( e: E ) => F, fg: ( f: F ) => G, gh: ( g: G ) => H, hi: ( h: H ) => I, ij: ( i: I ) => J, jk: ( j: J ) => K, kl: ( k: K ) => L, lm: ( l: L ) => M, mn: ( m: M ) => N, no: ( n: N ) => O, op: ( o: O ) => P, pq: ( p: P ) => Q, qr: ( q: Q ) => R, rs: ( r: R ) => S ): ( ...args: ExtractFunctionArguments ) => S export function pipe( entry: Fn, first: ( a: ExtractFunctionReturnValue ) => A , ab: ( a: A ) => B, bc: ( b: B ) => C, cd: ( c: C ) => D, de: ( d: D ) => E, ef: ( e: E ) => F, fg: ( f: F ) => G, gh: ( g: G ) => H, hi: ( h: H ) => I, ij: ( i: I ) => J, jk: ( j: J ) => K, kl: ( k: K ) => L, lm: ( l: L ) => M, mn: ( m: M ) => N, no: ( n: N ) => O, op: ( o: O ) => P, pq: ( p: P ) => Q, qr: ( q: Q ) => R, rs: ( r: R ) => S, st: ( s: S ) => T ): ( ...args: ExtractFunctionArguments ) => T export function pipe( entry: Fn, first: ( a: ExtractFunctionReturnValue ) => A , ab: ( a: A ) => B, bc: ( b: B ) => C, cd: ( c: C ) => D, de: ( d: D ) => E, ef: ( e: E ) => F, fg: ( f: F ) => G, gh: ( g: G ) => H, hi: ( h: H ) => I, ij: ( i: I ) => J, jk: ( j: J ) => K, kl: ( k: K ) => L, lm: ( l: L ) => M, mn: ( m: M ) => N, no: ( n: N ) => O, op: ( o: O ) => P, pq: ( p: P ) => Q, qr: ( q: Q ) => R, rs: ( r: R ) => S, st: ( s: S ) => T, tu: ( t: T ) => U ): ( ...args: ExtractFunctionArguments ) => U export function pipe( entry: Fn, first: ( a: ExtractFunctionReturnValue ) => A , ab: ( a: A ) => B, bc: ( b: B ) => C, cd: ( c: C ) => D, de: ( d: D ) => E, ef: ( e: E ) => F, fg: ( f: F ) => G, gh: ( g: G ) => H, hi: ( h: H ) => I, ij: ( i: I ) => J, jk: ( j: J ) => K, kl: ( k: K ) => L, lm: ( l: L ) => M, mn: ( m: M ) => N, no: ( n: N ) => O, op: ( o: O ) => P, pq: ( p: P ) => Q, qr: ( q: Q ) => R, rs: ( r: R ) => S, st: ( s: S ) => T, tu: ( t: T ) => U, uv: ( u: U ) => V ): ( ...args: ExtractFunctionArguments ) => V export function pipe( entry: Fn, first: ( a: ExtractFunctionReturnValue ) => A , ab: ( a: A ) => B, bc: ( b: B ) => C, cd: ( c: C ) => D, de: ( d: D ) => E, ef: ( e: E ) => F, fg: ( f: F ) => G, gh: ( g: G ) => H, hi: ( h: H ) => I, ij: ( i: I ) => J, jk: ( j: J ) => K, kl: ( k: K ) => L, lm: ( l: L ) => M, mn: ( m: M ) => N, no: ( n: N ) => O, op: ( o: O ) => P, pq: ( p: P ) => Q, qr: ( q: Q ) => R, rs: ( r: R ) => S, st: ( s: S ) => T, tu: ( t: T ) => U, uv: ( u: U ) => V, vw: ( v: V ) => W ): ( ...args: ExtractFunctionArguments ) => W export function pipe( entry: Fn, first: ( a: ExtractFunctionReturnValue ) => A , ab: ( a: A ) => B, bc: ( b: B ) => C, cd: ( c: C ) => D, de: ( d: D ) => E, ef: ( e: E ) => F, fg: ( f: F ) => G, gh: ( g: G ) => H, hi: ( h: H ) => I, ij: ( i: I ) => J, jk: ( j: J ) => K, kl: ( k: K ) => L, lm: ( l: L ) => M, mn: ( m: M ) => N, no: ( n: N ) => O, op: ( o: O ) => P, pq: ( p: P ) => Q, qr: ( q: Q ) => R, rs: ( r: R ) => S, st: ( s: S ) => T, tu: ( t: T ) => U, uv: ( u: U ) => V, vw: ( v: V ) => W, wx: ( w: W ) => X ): ( ...args: ExtractFunctionArguments ) => X export function pipe( entry: Fn, first: ( a: ExtractFunctionReturnValue ) => A , ab: ( a: A ) => B, bc: ( b: B ) => C, cd: ( c: C ) => D, de: ( d: D ) => E, ef: ( e: E ) => F, fg: ( f: F ) => G, gh: ( g: G ) => H, hi: ( h: H ) => I, ij: ( i: I ) => J, jk: ( j: J ) => K, kl: ( k: K ) => L, lm: ( l: L ) => M, mn: ( m: M ) => N, no: ( n: N ) => O, op: ( o: O ) => P, pq: ( p: P ) => Q, qr: ( q: Q ) => R, rs: ( r: R ) => S, st: ( s: S ) => T, tu: ( t: T ) => U, uv: ( u: U ) => V, vw: ( v: V ) => W, wx: ( w: W ) => X, xy: ( x: X ) => Y ): ( ...args: ExtractFunctionArguments ) => Y export function pipe( entry: Fn, first: ( a: ExtractFunctionReturnValue ) => A , ab: ( a: A ) => B, bc: ( b: B ) => C, cd: ( c: C ) => D, de: ( d: D ) => E, ef: ( e: E ) => F, fg: ( f: F ) => G, gh: ( g: G ) => H, hi: ( h: H ) => I, ij: ( i: I ) => J, jk: ( j: J ) => K, kl: ( k: K ) => L, lm: ( l: L ) => M, mn: ( m: M ) => N, no: ( n: N ) => O, op: ( o: O ) => P, pq: ( p: P ) => Q, qr: ( q: Q ) => R, rs: ( r: R ) => S, st: ( s: S ) => T, tu: ( t: T ) => U, uv: ( u: U ) => V, vw: ( v: V ) => W, wx: ( w: W ) => X, xy: ( x: X ) => Y, yz: ( y: Y ) => Z ): ( ...args: ExtractFunctionArguments ) => Z /** * Functional pipe, chain applies functions starting from the left and moves right * * @example * const myFormula = pipe( multiply(2), add(5) ) * myFormula(10) // 25 * * :: f1 |> f2 |> f3 |> ... fn */ export function pipe( entry, ...funcs ) { return ( ...arg ) => funcs.reduce( ( acc, item ) => item.call( item, acc ), entry( ...arg ) ) } export default pipe