Scala | Function Composition Last Updated : 26 May, 2022 Comments Improve Suggest changes Like Article Like Report Function composition is a way in which a function is mixed with other functions. During the composition the one function holds the reference to another function in order to fulfill it's mission. There are some different ways in which a function composition can take place, as below :- compose : Composing method works with val functions. Syntax : (function1 compose function2)(parameter)In the above syntax function2 works first with the parameter passed & then passes then returns a value to be passed to function1. Example 1: Scala // A Scala program to illustrate // compose method with val function // Creating object object GFG { // Main method def main(args: Array[String]) { println((add compose mul)(2)) // adding more methods println((add compose mul compose sub)(2)) } val add=(a: Int)=> { a + 1 } val mul=(a: Int)=> { a * 2 } val sub=(a: Int) =>{ a - 1 } } Output :5 3In above example, firstly mul function called we got 4(2 * 2) than add function called and we got 5(4 + 1). similarly (add compose mul compose sub)(2) will print 3 (step1 : 2 - 1 = 1, step2 : 1 * 2 = 2, step3 : 2 + 1 = 3).andThen : andThen method also works with val functions. Syntax :(function1 andThen function2)(parameter)In the above syntax function1 works first with the parameter passed & then passes then returns a value to be passed to function2. or as same as below:function2(function1(parameter))Example 2: Scala // A Scala program to illustrate //andThen method with val function // Creating object object GFG { // Main method def main(args: Array[String]) { println((add andThen mul)(2)) // Adding more methods println((add andThen mul andThen sub)(2)) } val add=(a: Int)=> { a + 1 } val mul=(a: Int)=> { a * 2 } val sub=(a: Int) =>{ a - 1 } } Output :6 5In above example, firstly add function called we got 3(2 + 1) than mul function called and we got 6(3 * 2). similarly add (andThen mul andThen sub)(2)) will print 5 (step1 : 2 + 1 = 3, step2 : 3 * 2 = 6, step3 : 6 - 1 = 5).Passing methods to methods : Methods are passed to other methods. Syntax :function1(function2(parameter))It works as same as compose function, but it works with def and val methods. Example 3: Scala // A Scala program to illustrate // passing methods to methods // Creating object object GFG { // Main method def main(args: Array[String]) { println(add(mul(2))) // Adding more methods println(add(mul(sub(2)))) } val add=(a: Int)=> { a + 1 } val mul=(a: Int)=> { a * 2 } val sub=(a: Int) =>{ a - 1 } } Output :5 3In above example, firstly mul function called we got 4(2 * 2) than add function called and we got 5(4 + 1). similarly add(mul(sub(2)) will print 3 (step1 : 2 - 1 = 1, step2 : 1 * 2 = 2, step3 : 2 + 1 = 3). Comment More info P Patabhu 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