Here is the interview question prompt, presented for reference.
In mathematics and computer science, currying is the technique of converting a function that takes multiple arguments into a sequence of functions that each takes a single argument. Currying provides a way for working with functions that take multiple arguments, and using them in frameworks where functions might take only one argument.
In programming, currying is an advanced technique of working with functions. It’s used not only in JavaScript, but in other languages as well.
It is a transformation of functions that translates a function from callable as f(a, b, c)
into callable as f(a)(b)(c)
. Currying doesn’t call a function. It just transforms it.
Currying is a very useful technique because it helps to avoid passing the same variable again and again, but it also helps to create a higher order function.
Currying transforms a function with multiple arguments into a sequence/series of functions each taking a single argument.
Your task would be to implement a curry()
function, which accepts a function and return a curried one.
For example:
Here is an example
const joinArgs = (a, b, c) => {
return `${a}_${b}_${c}`
}
const curriedFunc = curry(joinArgs)
curriedJoin(1, 2, 3) // '1_2_3'
curriedJoin(1)(2, 3) // '1_2_3'
curriedJoin(1, 2)(3) // '1_2_3'
You can see the full challenge with visuals at this link.
Challenges • Asked over 3 years ago by Jake from AlgoDaily
This is the main discussion thread generated for Create Curry Function (Main Thread).