What is Currying?
This technique converts N arguments function calls into N function call chains, with each function call having a single argument. Currying returns a function with an argument until all the arguments are applied. Then, you have to continue to call the returned function until the arguments have been exhausted and it returns the final value.
Note that curry takes binary functions and returns unary functions that also return unary functions. In addition, curried functions often have built-in iterator behaviors. It applies one argument at a time, which then returns to the call function used for the next step.
- As mentioned in a research paper writing service, everyday use of curried functions is function composition such as p(x) = q(r(x)). This is building a new function from an older function by passing the arguments. The function (q) has the return value since it’s an argument from the (r) function. However, because a function can return only one value, the return value must have a unary function applied to it.
- You can also use curried functions for a project’s infrastructure setup where there are numerous possibilities of creating generic functions. So it’s possible to configure and reuse little pieces easily and without creating clutters.
- The function Ramda.js lib is curried automatically, and lodash also has a function known as curry capable of forming curry function.
- The curry function can also be used for Memoization.
- Curry is also used to handle error throwing functions while also exiting immediately there’s an error.
- It is used to catch multiple errors and use them as validators on client-side code and APIs.
- It can be used for creating first-class functions, making it possible to use the functions as return values and arguments.
This technique fixes multiple arguments into a function to produce another function with a minor argument. So it binds values to one or several other of these arguments while the function chain progresses.
Here’s the syntax for its invocation: function.bind(thisValue, [arg1], [arg2], …)
Note that when you are using lodash or an underscore, it’s much easier to employ this partial function than using the primary bind method.
Comparing Currying and Partial Application
The partial application can fix one argument or even more in the returned function. Then the returned function will take the other parameters as arguments to complete its function application.
On the other hand, currying is when a function takes another function with numerous parameters as input, then gives a return function with exactly a parameter.
The differences between currying and partial application
Although they both have their similarities, they are different from one another and have different usages too. These are some of the other differences that you will find about currying and partial application.
- Currying tends to produce nested unary(1-ary) functions. However, the transformed functions are still broadly similar to the original one.
- Partial application tends to produce functions that have an arbitrary number of arguments, and the transformed functions usually have a lot of differences from the original one. It doesn’t need many arguments.
- Currying is far different from the partial application. However, it is possible to use partial applications to implement currying. However, it’s impossible to curry a function taking a random number of arguments (until you are sure about the arguments).
Interestingly, it is possible to use curried invocation and curried function to drive at the same effect you’ll get when binding an argument (if you perform this operation numerous times, it will yield a general case). To bind a value to the first argument, the matter must have the outermost part of nested functions applied to it before it can give you the new function you desire.
Why does this matter?
While curried functions always return another function with 1-arity until all the arguments are applied, partial applications may not necessarily give predictable return types.
As a programmer, you may be able to abstract the logic dependence of your program on time with promises. However, promises can do this because they lift the functions you’re calling to make the return type uniform. As a result, returning promises return the same types making it possible to use them in a standard way.
So dealing with the containers rather than the values allows you to create numerous generic functions that work universally for everything using the container’s interface.
Similar to promises, the curried functions also return containers that share a similar interface.