Scala | Partially Applied functions
Last Updated :
11 Oct, 2022
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:
- Partially applied functions are helpful in minimizing a function which accepts many arguments to a function that accepts only some arguments.
- It can replace any number of arguments defined by a function.
- It is easier for users to utilize this method.
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:
Scala
// Scala program of Partially
// applied functions
// Creating object
object Applied
{
// Main method
def main(args: Array[String])
{
// Creating a Partially applied
// function
def Book(discount: Double, costprice: Double)
: Double =
{
(1 - discount/100) * costprice
}
// Applying only one argument
val discountedPrice = Book(25, _: Double)
// Displays discounted price
// of the book
println(discountedPrice(400))
}
}
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:
- A partially applied function can be obtained even when not applied on any of the arguments defined. Example:
Scala
// Scala program of Partially
// applied functions
// Creating object
object Applied
{
// Main method
def main(args: Array[String])
{
// Creating a Partially applied
// function
def Mul(x: Double, y: Double)
: Double =
{
x * y
}
// Not applying any argument
val r = Mul _
// Displays Multiplication
println(r(9, 8))
}
}
- Partially applied functions can be utilized to replace any number of parameters. Example:
Scala
// Scala program of Partially
// applied functions
// Creating object
object Applied
{
// Main method
def main(args: Array[String])
{
// Creating a Partially applied
// function
def Mul(x: Double, y: Double, z: Double)
: Double =
{
x * y * z
}
// applying some arguments
val r = Mul(7, 8, _ : Double)
// Displays Multiplication
println(r(10))
}
}
- Currying approach can be utilized in Partially applied functions to transmit a function with multiple arguments into multiple functions, where each function takes only one argument. Example:
Scala
// Scala program of Partially
// applied functions using
// Currying approach
// Creating object
object curr
{
// Main method
def main(args: Array[String])
{
// Creating a Partially applied
// function
def div(x: Double, y: Double)
: Double =
{
x / y
}
// applying currying approach
val m = (div _).curried
// Displays division
println(m(72)(9))
}
}
- Here, currying approach breaks the given function into two functions, where each function takes one parameter so, you need to pass one value for each of the functions and get the desired output.
Similar Reads
Partial Functions in Scala Introduction: When a function is not able to produce a return for every single variable input data given to it then that function is termed as Partial function. It can determine an output for a subset of some practicable inputs only. It can only be applied partially to the stated inputs. Some import
4 min read
Scala | Functions - Basics A function is a collection of statements that perform a certain task. One can divide up the code into separate functions, keeping in mind that each function must perform a specific task. Functions are used to put some common and repeated task into a single function, so instead of writing the same co
3 min read
Pure Function In Scala In any programming language, there are two types of functions: 1. PURE FUNCTIONS 2. IMPURE FUNCTIONSThere is a basic difference between the two, that is pure function doesnât change the variable itâs passed and an impure function does. For example, there exists a function which increases the input b
3 min read
Scala | Nested Functions A function definition inside an another function is known as Nested Function. It is not supported by C++, Java, etc. In other languages, we can call a function inside a function, but itâs not a nested function. In Scala, we can define functions inside a function and functions defined inside other fu
2 min read
Currying Functions in Scala with Examples Currying in Scala is simply a technique or a process of transforming a function. This function takes multiple arguments into a function that takes single argument. It is applied widely in multiple functional languages. Syntax def function name(argument1, argument2) = operation Let's understand with
3 min read