Function curry

  • Curries a function, allowing it to be called with a single argument at a time and returning a new function that takes the next argument. This process continues until all arguments have been provided, at which point the original function is called with all accumulated arguments.

    Type Parameters

    • R

    Parameters

    • func: (() => R)

      The function to curry.

        • (): R
        • Returns R

    Returns (() => R)

    A curried function.

      • (): R
      • Returns R

    function noArgFunc() {
    return 42;
    }
    const curriedNoArgFunc = curry(noArgFunc);
    console.log(curriedNoArgFunc()); // 42
  • Curries a function, allowing it to be called with a single argument at a time and returning a new function that takes the next argument. This process continues until all arguments have been provided, at which point the original function is called with all accumulated arguments.

    Type Parameters

    • P
    • R

    Parameters

    • func: ((p: P) => R)

      The function to curry.

        • (p): R
        • Parameters

          Returns R

    Returns ((p: P) => R)

    A curried function.

      • (p): R
      • Parameters

        Returns R

    function oneArgFunc(a: number) {
    return a * 2;
    }
    const curriedOneArgFunc = curry(oneArgFunc);
    console.log(curriedOneArgFunc(5)); // 10
  • Curries a function, allowing it to be called with a single argument at a time and returning a new function that takes the next argument. This process continues until all arguments have been provided, at which point the original function is called with all accumulated arguments.

    Type Parameters

    • P1
    • P2
    • R

    Parameters

    • func: ((p1: P1, p2: P2) => R)

      The function to curry.

        • (p1, p2): R
        • Parameters

          Returns R

    Returns ((p1: P1) => ((p2: P2) => R))

    A curried function.

      • (p1): ((p2: P2) => R)
      • Parameters

        Returns ((p2: P2) => R)

          • (p2): R
          • Parameters

            Returns R

    function twoArgFunc(a: number, b: number) {
    return a + b;
    }
    const curriedTwoArgFunc = curry(twoArgFunc);
    const add5 = curriedTwoArgFunc(5);
    console.log(add5(10)); // 15
  • Curries a function, allowing it to be called with a single argument at a time and returning a new function that takes the next argument. This process continues until all arguments have been provided, at which point the original function is called with all accumulated arguments.

    Type Parameters

    • P1
    • P2
    • P3
    • R

    Parameters

    • func: ((p1: P1, p2: P2, p3: P3) => R)

      The function to curry.

        • (p1, p2, p3): R
        • Parameters

          Returns R

    Returns ((p1: P1) => ((p2: P2) => ((p3: P3) => R)))

    A curried function.

      • (p1): ((p2: P2) => ((p3: P3) => R))
      • Parameters

        Returns ((p2: P2) => ((p3: P3) => R))

          • (p2): ((p3: P3) => R)
          • Parameters

            Returns ((p3: P3) => R)

              • (p3): R
              • Parameters

                Returns R

    function threeArgFunc(a: number, b: number, c: number) {
    return a + b + c;
    }
    const curriedThreeArgFunc = curry(threeArgFunc);
    const add1 = curriedThreeArgFunc(1);
    const add3 = add1(2);
    console.log(add3(3)); // 6
  • Curries a function, allowing it to be called with a single argument at a time and returning a new function that takes the next argument. This process continues until all arguments have been provided, at which point the original function is called with all accumulated arguments.

    Type Parameters

    • P1
    • P2
    • P3
    • P4
    • R

    Parameters

    • func: ((p1: P1, p2: P2, p3: P3, p4: P4) => R)

      The function to curry.

        • (p1, p2, p3, p4): R
        • Parameters

          Returns R

    Returns ((p1: P1) => ((p2: P2) => ((p3: P3) => ((p4: P4) => R))))

    A curried function.

      • (p1): ((p2: P2) => ((p3: P3) => ((p4: P4) => R)))
      • Parameters

        Returns ((p2: P2) => ((p3: P3) => ((p4: P4) => R)))

          • (p2): ((p3: P3) => ((p4: P4) => R))
          • Parameters

            Returns ((p3: P3) => ((p4: P4) => R))

              • (p3): ((p4: P4) => R)
              • Parameters

                Returns ((p4: P4) => R)

                  • (p4): R
                  • Parameters

                    Returns R

    function fourArgFunc(a: number, b: number, c: number, d: number) {
    return a + b + c + d;
    }
    const curriedFourArgFunc = curry(fourArgFunc);
    const add1 = curriedFourArgFunc(1);
    const add3 = add1(2);
    const add6 = add3(3);
    console.log(add6(4)); // 10
  • Curries a function, allowing it to be called with a single argument at a time and returning a new function that takes the next argument. This process continues until all arguments have been provided, at which point the original function is called with all accumulated arguments.

    Type Parameters

    • P1
    • P2
    • P3
    • P4
    • P5
    • R

    Parameters

    • func: ((p1: P1, p2: P2, p3: P3, p4: P4, p5: P5) => R)

      The function to curry.

        • (p1, p2, p3, p4, p5): R
        • Parameters

          Returns R

    Returns ((p1: P1) => ((p2: P2) => ((p3: P3) => ((p4: P4) => ((p5: P5) => R)))))

    A curried function.

      • (p1): ((p2: P2) => ((p3: P3) => ((p4: P4) => ((p5: P5) => R))))
      • Parameters

        Returns ((p2: P2) => ((p3: P3) => ((p4: P4) => ((p5: P5) => R))))

          • (p2): ((p3: P3) => ((p4: P4) => ((p5: P5) => R)))
          • Parameters

            Returns ((p3: P3) => ((p4: P4) => ((p5: P5) => R)))

              • (p3): ((p4: P4) => ((p5: P5) => R))
              • Parameters

                Returns ((p4: P4) => ((p5: P5) => R))

                  • (p4): ((p5: P5) => R)
                  • Parameters

                    Returns ((p5: P5) => R)

                      • (p5): R
                      • Parameters

                        Returns R

    function fiveArgFunc(a: number, b: number, c: number, d: number, e: number) {
    return a + b + c + d + e;
    }
    const curriedFiveArgFunc = curry(fiveArgFunc);
    const add1 = curriedFiveArgFunc(1);
    const add3 = add1(2);
    const add6 = add3(3);
    const add10 = add6(4);
    console.log(add10(5)); // 15
  • Curries a function, allowing it to be called with a single argument at a time and returning a new function that takes the next argument. This process continues until all arguments have been provided, at which point the original function is called with all accumulated arguments.

    Parameters

    • func: ((...args: any[]) => any)

      The function to curry.

        • (...args): any
        • Parameters

          • Rest...args: any[]

          Returns any

    Returns ((...args: any[]) => any)

    A curried function that can be called with a single argument at a time.

      • (...args): any
      • Parameters

        • Rest...args: any[]

        Returns any

    function sum(a: number, b: number, c: number) {
    return a + b + c;
    }

    const curriedSum = curry(sum);

    // The parameter `a` should be given the value `10`.
    const add10 = curriedSum(10);

    // The parameter `b` should be given the value `15`.
    const add25 = add10(15);

    // The parameter `c` should be given the value `5`. The function 'sum' has received all its arguments and will now return a value.
    const result = add25(5);