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 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:
// Scala program of // Partial function // Creating object object Case { // Main method def main(args: Array[String]) { // Creating Partial function // using two methods val r = new PartialFunction[Int, Int] { // Applying isDefinedAt method def isDefinedAt(q: Int) = q != 0 // Applying apply method def apply(q: Int) = 12 * q } // Displays output if the // condition is satisfied println(r(10)) } } |
120
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:// Scala program using// case statements// Creating objectobjectCase{// Main methoddefmain(args:Array[String]){// Creating Partial functionvald:PartialFunction[Int, Int]={// using case statementcasexif(x%3)==0=>x *3}// Displays output if// the condition is// satisfiedprintln(d(3))}}chevron_rightfilter_noneOutput:9
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:// Scala program using// orElse// Creating objectobjectorElse{// Main methoddefmain(args:Array[String]){// Creating Partial function1valM:PartialFunction[Int, Int]={// using case statementcasexif(x%5)==0=>x *5}// Creating Partial function2valm:PartialFunction[Int, Int]={// using case statementcaseyif(y%2)==0=>y *2}// chaining two partial// functions using orElsevalr=M orElse m// Displays output for// which the given condition// is satisfiedprintln(r(5))println(r(4))}}chevron_rightfilter_noneOutput:25 8
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:// Scala program using// collect method// Creating objectobjectCollect{// Main methoddefmain(args:Array[String]){// Creating Partial functionvalM:PartialFunction[Int, Int]={// using case statementcasexif(x%5) !=0=>x *5}// Applying collect methodvaly=List(7,15,9) collect { M }// Displays output for which// the given condition// is satisfiedprintln(y)}}chevron_rightfilter_noneOutput:List(35, 45)
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:// Scala program using// andThen method// Creating objectobjectandThen{// Main methoddefmain(args:Array[String]){// Creating Partial functionvalM:PartialFunction[Int, Int]={// using case statementcasexif(x%4) !=0=>x *4}// Creating another functionvalappend=(x:Int)=>x *10// Applying andThen methodvaly=M andThen append// Displays output after// appending the another// function givenprintln(y(7))}}chevron_rightfilter_noneOutput:280
Here, andThen will append the output of Partial function with the another function given and then will return that value.
Recommended Posts:
- Scala | Functions - Basics
- Scala | Nested Functions
- Anonymous Functions in Scala
- Scala | Functions Call-by-Name
- Currying Functions in Scala with Examples
- Scala | Partially Applied functions
- Higher Order Functions in Scala
- Set in Scala | Set-2
- Scala | Either
- Scala Map
- Set in Scala | Set-1
- Scala | Equals
- Scala | ArrayBuffer
- Scala | Product3
- Scala | Arrays
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



