Currying vs Partially Applied Function

In this blog, I’m going to discuss about currying and partially applied functions.

CURRYING

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.

CURRYING EXAMPLE

partially applying a function on isDivisibleCurried will create a chain of functions, one per parameter list [ Int => (Int => Boolean) ].

Thanks for reading!

knoldus-advt-sticker

Written by 

Jyoti Sachdeva is a software consultant with more than 6 months of experience. She likes to keep up with the trending technologies. She is familiar with languages such as C,C++,Java,Scala and is currentky working on akka,akka http and scala. Her hobbies include watching tv series and movies, reading novels and dancing.

Leave a Reply

%d bloggers like this: