In this blog, I’m going to discuss about currying and partially applied functions.
Currying splits method with multiple parameters into a chain of functions – each with one parameter.
Let’s understand currying using an example:
is the same as:
multiply is a curried function that takes three parameters a, b and c.
Invoke multiply function as:
will result in another function that takes interger as an input and yields a lambda expression (Int => Int).
will result in another function that takes interger as an input and returns integer as result.
res11 is the final result.
PARTIALLY APPLIED FUNCTIONS
Pass to function less arguments than it has in its declaration. Scala returns a new function with rest of arguments that need to be passed.
will split the isInRange method into a chain of functions each with one parameter.
DIFFERENCE BETWEEN CURRYING AND PARTIALLY APPLIED FUNCTION
PARTIALLY APPLIED FUNCTION EXAMPLE
partially applying a normal function(isDivisible) results in a function(res21) that takes all parameters [ (Int, Int) => Boolean ].
will give true result.
partially applying a function on isDivisibleCurried will create a chain of functions, one per parameter list [ Int => (Int => Boolean) ].
Thanks for reading!