Partial Functions in Scala Last Updated : 29 Mar, 2019 Comments Improve Suggest changes Like Article Like Report 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 // 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)) } } Output: 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 // Scala program using // case statements // Creating object object Case { // Main method def main(args: Array[String]) { // Creating Partial function val d: PartialFunction[Int, Int] = { // using case statement case x if (x % 3) == 0 => x * 3 } // Displays output if // the condition is // satisfied println(d(3)) } } Output: 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 // Scala program using // orElse // Creating object object orElse { // Main method def main(args: Array[String]) { // Creating Partial function1 val M: PartialFunction[Int, Int] = { // using case statement case x if (x % 5) == 0 => x * 5 } // Creating Partial function2 val m: PartialFunction[Int, Int] = { // using case statement case y if (y % 2) == 0 => y * 2 } // chaining two partial // functions using orElse val r = M orElse m // Displays output for // which the given condition // is satisfied println(r(5)) println(r(4)) } } Output: 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 // Scala program using // collect method // Creating object object Collect { // Main method def main(args: Array[String]) { // Creating Partial function val M: PartialFunction[Int, Int] = { // using case statement case x if (x % 5) != 0 => x * 5 } // Applying collect method val y = List(7, 15, 9) collect { M } // Displays output for which // the given condition // is satisfied println(y) } } Output: 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 // Scala program using // andThen method // Creating object object andThen { // Main method def main(args: Array[String]) { // Creating Partial function val M: PartialFunction[Int, Int] = { // using case statement case x if (x % 4) != 0 => x * 4 } // Creating another function val append = (x: Int) => x * 10 // Applying andThen method val y = M andThen append // Displays output after // appending the another // function given println(y(7)) } } Output: 280 Here, andThen will append the output of Partial function with the another function given and then will return that value. Comment More info N nidhi1352singh Follow Improve Article Tags : Scala Scala Scala-Method Explore OverviewScala Programming Language3 min readIntroduction to Scala7 min readSetting up the environment in Scala3 min readHello World in Scala2 min readBasicsScala Keywords2 min readScala Identifiers3 min readData Types in Scala3 min readVariables in Scala3 min readControl StatementsScala | Decision Making (if, if-else, Nested if-else, if-else if)5 min readScala | Loops(while, do..while, for, nested loops)5 min readBreak statement in Scala3 min readScala | Literals4 min readOOP ConceptsClass and Object in Scala5 min readInheritance in Scala5 min readOperators in Scala11 min readScala Singleton and Companion Objects3 min readScala Constructors4 min readScala | Polymorphism5 min readScala | Multithreading3 min readScala this keyword2 min readMethodsScala | Functions - Basics3 min readAnonymous Functions in Scala2 min readScala | Closures3 min readRecursion in Scala4 min readMethod Overloading in Scala5 min readMethod Overriding in Scala8 min readLambda Expression in Scala4 min readScala Varargs2 min readStringsScala String4 min readScala | String Interpolation3 min readScala | StringContext2 min readRegular Expressions in Scala5 min readStringBuilder in Scala4 min readScala PackagesPackages In Scala4 min readScala | Package Objects3 min readChained Package Clauses in Scala3 min readFile Handling in Scala3 min readScala TraitScala | Traits7 min readScala | Sealed Trait4 min readScala | Trait Mixins3 min readTrait Linearization in Scala5 min readCollectionsScala Lists5 min readScala ListBuffer6 min readListSet in Scala6 min readScala Map5 min readScala | Arrays6 min readScala | ArrayBuffer4 min readScala | Tuple5 min readSet in Scala | Set-13 min readSet in Scala | Set-27 min readBitSet in Scala5 min readHashSet In Scala4 min readStack in Scala3 min readHashMap in Scala3 min readTreeSet in Scala4 min readIterators in Scala5 min readScala | Option3 min read Like