![]() |
VOOZH | about |
The Partially applied functions are the functions which are not applied on all the arguments defined by the stated function i.e, while invoking a function, we can supply some of the arguments and the left arguments are supplied when required. we call a function we can pass less arguments in it and when we pass less arguments it does not throw an exception. these arguments which are not passed to function we use underscore( _ ) as placeholder. Some important points:
Syntax:
val multiply = (a: Int, b: Int, c: Int) => a * b * c // less arguments passed val f = multiply(1, 2, _: Int)
As we can see in above syntax we defined a normal function multiply which have three arguments we pass less arguments (two). we can see it does not throw an exception that is partially applied function. Example:
300.0
Here, the discount is passed in the argument and costprice part is left empty which can be passed later when required so, the discounted price can be calculated any number of times. Some more examples of Partially applied functions:
72.0
560.0
8.0