Updated April 14, 2023
Definition of Scala Partial Function
We can define it as if the function is not able to return value for every provided parameter then we can call them partial function. This means this function can only be able to provide value for a given set of input but not for all. We can say that the implementation of partial function partial they cannot provide value for all input passed to it. It provides values for only a set of data for which it is defined in Scala. But to check whether the partial function can handle the passing value or not we have the idsDefinedAt method which can check value is available for a given type or not.
Syntax and Parameters
Syntax and parameters of scala partial function
valName_of_partical_function = new PartialFunction[Data_type1, Data_type1] {
//your logic
}
We can create a partial function using the above syntax. We need to give the variable name followed by a Partial function object using the new keyword. Also inside the bracket, we need to assign the data type for the function. Inside the body of this, we can define our partial function.
Let’s see one practical syntax for beginners;
val par1 = new PartialFunction[Int, Int]
{
defisDefinedAt(q1: Int) = q1 != 0
}
In the above example, we are using the isDefinedAt method to define our function.
How Partial Functions Works in Scala?
Partial function is that function whose implementation is partial and they cannot produce an output value for every input we pass. We can use a case statement to define these functions. Let’s take one example to understand it working;
object Main extends App{
// Your code here!
valparResult: PartialFunction[Int, Int] = {
case a: Int if a > 0 =>Math.abs(a)
}
varlist : List[Int] = List(10, 40, 50)
valfinalres = list.collect(parResult)
println(finalres)
}
In this example, we are creating one partial function named parResult. This function will check the value if it is greater than 0 then apply math. Abs on it. We have prepared a collection list with some values in it and calling the partial function by passing its name as parameter. If we do not have a partial function and if we try to call Map here, then we may receive NAN value for some of the inputs of the list. For example, we are trying to calculate the square root of the given input so, in that case, we may receive NAN for inputs. So we can say that partial function helps us to get rid of any side effects as well.
We have some extended, liner super types and known sub classes for partial function in scala let’s have a look into them;
1. Extended classes available;
- (A) => B
2. Super type classes available;
- (A) => B
- AnyRef
- Any
3. Its Hierarchy;
- MapOps
- collection
- UnliftOps
Examples of Scala Partial Function
Following are the examples are given below:
Example #1
In this example, we are creating simple partial function.
Code:
object Main extends App{
// Your code here!
// creating partial function
valparResult: PartialFunction[Int, Int] = {
case a: Int if a > 0 =>Math.abs(a)
}
varlist : List[Int] = List(10, 40, 50)
// calling partial function
valfinalres = list.collect(parResult)
//prinitng result
println("Value with partial function is :::")
println(finalres)
}
Output:
Example #2
In this example, we are using isDefinedAt and apply methods to work with partial function.
Code:
object Main extends App{
// Your code here!
valparFun = new PartialFunction[Int, Int]
{
// here applying defined method
defisDefinedAt(x: Int) = x != 0
// here apply method
defapply(x: Int) = 10 * x
}
// printing outpt from function
println("calling function")
println(parFun(34))
}
Output:
Example #3
In this example, we are using a case statement to define our partial function and writing our logic.
Code:
object Main extends App{
// Your code here!
valparFun: PartialFunction[Int, Int] =
{
// using case statement
case obj if (obj % 3) == 0 =>obj * 2
}
// printing output from function
println("calling function")
println(parFun(39))
}
Output:
Example #4
In this example, we are using the collect method to work with partial function.
Code:
object Main extends App{
// Your code here!
valparFun: PartialFunction[Int, Int] =
{
// declaring function using case statement
case obj if (obj % 5) != 0 =>obj * 5
}
// method
// using collect method
val result = List(13, 57, 38, 15, 0 , 12, 200) collect { parFun }
/// printing output
println("Output of function is :::::")
println(result)
}
Output:
Example #5
In this example, we are using andThen method here to work with partial function.
Code:
object Main extends App{
// Your code here!
//here we are declaring partial function using case statement
valparFun: PartialFunction[Int, Int] =
{
case obj if (obj % 4) != 0 =>obj * 2
}
val a = (y: Int) => y * 100
//applingandThenfunction
val result = parFunandThen a
println("result is :: ")
println(result(3))
}
Output:
Example #6
In this example, we are using a binary operator inside the case statement while defining the partial function in scala.
Code:
object Main extends App{
// Your code here!
valparFun: PartialFunction[Int, Int] =
{
// using case statement
case obj if (obj % 10) == 0 =>obj * 20
}
// printing output from function
println("calling function")
println(parFun(300))
}
Output:
Important point while working with Scala partial function:
- A partial function is a trait which means we need to give an implementation of all the methods of it. In this, we have two method defined and apply. If you do not give an implementation of any of them while working with it. It will throw an exception.
- We can define the function by using the case statement.
- We can use different methods such as collect, andThen, If, else, the case with partial function in scala.
Conclusion
Scala partial function is partially implemented. Also, we have two methods available to define them defined and apply. Also, we can define a partial function using the case statement but the syntax would be a little different in this case.
Recommended Articles
We hope that this EDUCBA information on “Scala Partial Function” was beneficial to you. You can view EDUCBA’s recommended articles for more information.