Curried functions in JavaScript
Curried functions exists in some programming languages and they are functions which have been called without all their arguments. This yields a new function which has the remaining arguments, which can be called with those arguments to get a return value from the original function.
Lets look at a simple JavaScript case, with a function that adds two numbers together:
const add = ( a, b ) => {
return a + b
}
// Add 5 and 6
const c = add( 5, 6 )
Now, if instead it is written like this, calling with one argument returns a function which adds that argument to anything:
// add is a function which returns a function
const add = (a) => (b) => {
return a + b
}
// Using the function to add two numbers together now looks like this
const c = add(5)(6)
// This is a Curried function, it adds 5 to any number when called
const addFive = add(5)
// Add five to six
const d = addFive(6)
This can now be used anywhere that a function with a single argument can be used:
const arrayWithFiveAdded = myArray.map( addFive )