Skip to Content

Return a curried function

Home | Coding Interviews | Miscellaneous | Return a curried function

Given a function fn, return a curried version of that function.

A curried function is a function that accepts fewer or an equal number of parameters as the original function and returns either another curried function or the same value the original function would have returned.

In practical terms, if you called the original function like sum(1,2,3), you would call the curried version like csum(1)(2)(3), csum(1)(2,3), csum(1,2)(3), or csum(1,2,3). All these methods of calling the curried function should return the same value as the original.

function curry(fn: Function): Function {
    const n = fn.length;
    const allArgs: any[] = [];

    return function curried(...args: any[]) {
        allArgs.push(...args);
        if (allArgs.length < n) {
            return curried;
        }
        return fn(...allArgs);
    };
}
def curry(fn):

    def curried(*args):

        if len(args) >= fn.__code__.co_argcount:

            return fn(*args)

        else:

            def curried_more(*more_args):

                return curried(*(args + more_args))

            return curried_more

    return curried


def sum(a, b):

    return a + b



curried_sum = curry(sum)


result = curried_sum(1)(2) # Returns 3

print(result)  # Output will be 3

Currying is a technique in functional programming where a function, instead of taking all its arguments at once, takes them one by one. When you call a curried function, it returns a new function that "remembers" the argument it was given, allowing you to supply the remaining arguments later. This process continues until all arguments are provided, at which point the final result is computed. For example, if you have a function f(a, b, c), currying transforms it into f(a)(b)(c). This is useful when you want to create more specialized functions from a general one by partially applying arguments.

Higher-order functions are functions that can take other functions as arguments or return them. Currying is an example of this, as the function returns another function until all arguments are passed. Variadic functions, which can take a varying number of arguments, complement currying because they allow flexibility in how many arguments you pass at each step. Combining these ideas, curried variadic functions offer both flexibility and functional composition, enabling powerful abstractions in code.

Posted by grwgreg 21 days ago

Related Problems

The Hamming Distance between two integers is the number of positions at which the corresponding bits are different.

Given two integers x and y, return the Hamming distance between them.

Given an array nums of size n, return the majority element.

The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array.

Given an array of asyncronous functions functions and a pool limit n, return an asyncronous function promisePool. It should return a promise that resolves when all the input functions resolve.

Pool limit is defined as the maximum number promises that can be pending at once. promisePool should begin execution of as many functions as possible and continue executing new functions when old promises resolve. promisePool should execute functions[i] then functions[i + 1] then functions[i + 2], etc. When the last promise resolves, promisePool should also resolve.

For example, if n = 1, promisePool will execute one function at a time in series. However, if n = 2, it first executes two functions. When either of the two functions resolve, a 3rd function should be executed (if available), and so on until there are no functions left to execute.

You can assume all functions never reject. It is acceptable for promisePool to return a promise that resolves any value.

You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]).

Find two lines that together with the x-axis form a container, such that the container contains the most water.

Return the maximum amount of water a container can store.