Currying vs Partially applied functions
In this blog, I’m going to discuss 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:
scala> def multiply(a: Int)(b: Int)(c: Int) = a * b * c
is the same as:
def multiply(a: Int) = (b: Int) => (c: Int) => a * b * c
multiply is a curried function that takes three parameters a, b and c.
Invoke the multiply function as:
multiply(1)(_)res9: Int => (Int => Int) = $$Lambda$1118/1682999176@30b1c5d5
will result in another function that takes an integer as input and yields a lambda expression (Int => Int).
scala> res9(2)(_)res10: Int => Int = $$Lambda$1119/2012237082@27682fa9
will result in another function that takes an integer as an input and returns an integer as a result.
scala> res10(3)res11: Int = 6
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.
def isInRange(leftBound: Int, num: Int, rightBound: Int): Boolean = {if (leftBound < num && num < rightBound) trueelse false}isInRange(_: Int, 5, _: Int)/* will return another function that will take two integers as an argument and result type will be of type Boolean (Int, Int) => Boolean */scala> isInRange(0, 8)res0: Boolean = truescala>(isInRange _).curriedres28: Int => (Int => (Int => Boolean)) = scala.Function3$$Lambda$1322/926382023@3e9cceff
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
scala> def isDivisible(numberOne: Int, numberTwo: Int) = ((numberOne % numberTwo) == 0)isDivisible: (numberOne: Int, numberTwo: Int)Booleanscala> isDivisible _res23: (Int, Int) => Boolean = $$Lambda$1285/1740173358@6fdc624
partially applying a normal function(isDivisible) results in a function(res21) that takes all parameters [ (Int, Int) => Boolean ].
scala> res23(4)
res26: Int => Boolean = scala.Function2$$Lambda$1320/451962020@40c78bd1scala> res26(2)
res27:Boolean = true
will give true result.
CURRYING EXAMPLE
scala> def isDivisibleCurried(numberOne: Int)(numberTwo: Int) = ((numberOne % numberTwo) == 0)
isDivisibleCurried: (numberOne: Int)(numberTwo: Int)Booleanscala> isDivisibleCurried _
res22: Int => (Int => Boolean) = $$Lambda$1298/27138712@4ec8083
partially applying a function on isDivisibleCurried will create a chain of functions, one per parameter list [ Int => (Int => Boolean) ].
Thanks for reading!