**Definition: **Currying is transforming a function by a fixed arity(number of given arguments) to a function that is a sequence of nested returned functions each takes one of those arguments at a time.
func([1…n]) -> func1…n
**In Code: **If we have this function:
const soakIn = (a, b) => b + " " + a;
soakIn("soaked in BBQ sauce", "beef"); // beef soaked in BBQ sauce
We can form it to a curried function:
const soakIn = a => b => b + " " + a;
soakIn("soaked in BBQ sauce")("beef"); // beef soaked in BBQ sauce
So this is what currying does in short but more details are in the 👉 Walk-through section.
A friend of mine named Soheyl believes that BBQ sauce makes everything perfect! (true story) 😀
_He can make a specilized function by
_const soakedInBBQ = soakIn("soaked in BBQ sauce");_
and call the new function by every parameter he want to have everything with BBQ sauce like __soakedInBBQ("chicken"); // chicken soaked in BBQ sauce_
In the motivation section, I’ve mentioned some reasons to answer the why on using curried functions,
Next with an example in walk-through section, I explain how to transform a normal function to a curried one (It’s so simple, no need to know about JavaScript in particular),
And at last in other demonstrations section, I’ve demonstrated this transformation and usage in some other languages like Java and JavaScript libraries like React.js and React Native along with some Good-to-know Facts.
#functional-programming #react #react-native #java #javascript