Partially Applied Functions in Scala
Partial applied functions allow us to set any parameter of a function. After partial functions, a function is created with the remaining parameters of the function. In this post we are going to discuss the details about this feature.
Let’s create a function which accepts three parameters. It uses the arguments for an algorithm and returns the result of the calculations.
Scala allows us to fix any argument, no matter its placements in the parameters list. Here we have a partially applied function add1 with its second argument fixed to 2. A partially applied function can further go through the same process creating more refined partially applied functions.
Just look at the definition of add2, which is created by fixing the first argument of add1. Now add2 is a Function1, which just uses the argument as the third parameter value. It just applies the previously fixed values for the calculation and the returns the result.
We can also use add1 as a Function2 e.g. sum2. We can just provide the values for the remaining arguments to get the result of the calculation.
You must log in to post a comment.