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 important points:
- Partial functions are beneficent in understanding many inconsistent kind of Scala functions.
- It can be interpreted by utilizing case statements.
- It is a Trait, which needs two methods namely isDefinedAt and apply to be implemented.
Example:
object Case
{
def main(args : Array[String])
{
val r = new PartialFunction[Int, Int]
{
def isDefinedAt(q : Int) = q ! = 0
def apply(q : Int) = 12 * q
}
println(r( 10 ))
}
}
|
Here, two methods are defined for applying Partial function, where isDefinedAt states the condition and apply performs the operation if the given condition is satisfied.
Methods to define Partial functions:
There are some methods to define Partial function, which includes case statements, collect method, andThen, and orElse.
- Partial function using Case statement:
we will create a Partial function below using case statement.
Example:
object Case
{
def main(args : Array[String])
{
val d : PartialFunction[Int, Int] =
{
case x if (x % 3 ) == 0 => x * 3
}
println(d( 3 ))
}
}
|
Here, Partial function is created using case statement so, apply and isDefinedAt is not required here.
- Partial function using orElse:
This method is helpful in chaining Partial functions together.
Example:
object orElse
{
def main(args : Array[String])
{
val M : PartialFunction[Int, Int] =
{
case x if (x % 5 ) == 0 => x * 5
}
val m : PartialFunction[Int, Int] =
{
case y if (y % 2 ) == 0 => y * 2
}
val r = M orElse m
println(r( 5 ))
println(r( 4 ))
}
}
|
Here, orElse will return output for which the given condition is satisfied.
- Partial function using Collect method:
Collect method requests Partial function to every single element of the collection and thus, helps in constructing a new collection.
Example:
object Collect
{
def main(args : Array[String])
{
val M : PartialFunction[Int, Int] =
{
case x if (x % 5 ) ! = 0 => x * 5
}
val y = List( 7 , 15 , 9 ) collect { M }
println(y)
}
}
|
Here, Collect will apply Partial function to all the elements of the List and will return a new List on the basis of the conditions stated.
- Partial function using andThen:
This method appends at the end of the chains, which is utilized to continue towards additional chains of Partial functions.
Example:
object andThen
{
def main(args : Array[String])
{
val M : PartialFunction[Int, Int] =
{
case x if (x % 4 ) ! = 0 => x * 4
}
val append = (x : Int) => x * 10
val y = M andThen append
println(y( 7 ))
}
}
|
Here, andThen will append the output of Partial function with the another function given and then will return that value.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!