
Pattern Matching in Scala can extend to create Partially-Applied functions, a unary function that does not support every possible value that meets the input type. Partial functions are only for certain domains of input values.
Partially Applied functions
The Partially-Applied functions are the functions that 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 fewer arguments in it and when we pass fewer arguments it does not throw an exception.
- 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 the above syntax we have a normal function multiply which has three arguments we pass fewer arguments (two). we can see it does not throw an exception that is a partially applied function.
// 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))
}
}
Partially Defined Functions
This is a unary function that works only for certain values. (Unary means it accepts only one argument.) This idea maps perfectly to the mathematical concept of a domain. For example, the natural logarithm is not defined for any x < =0. We can express that in Scala. Define a function l and use the case statement to limit the parameters it will accept
Scala Currying Function
Through the Scala curry function, we can split a list with multiple parameters into a chain of functions each with one parameter. This means we define them with more than one parameter list. It is a function that takes multiple arguments which can translate into a series of function calls that each take a single argument.
A Syntax of Scala Currying Function:
We use the following syntax to carry out currying:def multiply(a:Int)(b:Int)=a*b
def multiply(a:Int)(b:Int)=a*b
Another way we can do this is:def multiply(a:Int)=(b:Int)=>a*b
def multiply(a:Int)=(b:Int)=>a*b
Example – 1
scala> class Add{
| def sum(a:Int)(b:Int)={
| a+b}
| }
defined class Add
scala> var a=new Add()
a: Add = Add@53cba89f
scala> a.sum(3)(4)
res4: Int = 7
Advantages of Currying Function in Scala
- One benefit is that Scala currying makes creating anonymous functions easier.
- Scala Currying also makes it easier to pass around a function as a first-class object. You can keep applying parameters when you find them.
Conclusion
Hence, using the concept of partial functions, we use curry functions in Scala. This lets us split a list of multiple parameters into a chain of functions. The benefit of the Currying and Partially Applied function is the ability to create specialized functions based on general functions without introducing new code keeping the code free of duplication.
Refrences
For More Information Click HereRefer