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 infoAdvertise with us Next Article Scala | Function Composition P Patabhu Follow Improve Article Tags : Scala Scala Scala-Method Similar Reads Composition of Functions The composition of functions is a process where you combine two functions into a new function. Specifically, it involves applying one function to the result of another function. In simpler terms, the output of one function becomes the input for the other function.Mathematically, the composition of t 11 min read Function Composition in Python Function composition is a powerful concept where two or more functions are combined in such a way that the output of one function becomes the input for the next. It allows for the creation of complex functionality from simple, reusable building blocks. This concept is widely used in functional progr 6 min read Scala | Functions - Basics A function is a collection of statements that perform a certain task. One can divide up the code into separate functions, keeping in mind that each function must perform a specific task. Functions are used to put some common and repeated task into a single function, so instead of writing the same co 3 min read Anonymous Functions in Scala In Scala, An anonymous function is also known as a function literal. A function which does not contain a name is known as an anonymous function. An anonymous function provides a lightweight function definition. It is useful when we want to create an inline function. Syntax: (z:Int, y:Int)=> z*yOr 2 min read Scala Constructs Some of the basic Scala constructs are expressions, blocks, classes, objects, functions, methods, traits, Main methods, fields, and closures. 1. Scala Expression: A computable statement in Scala is known as expression. For example, the following is an expression: 3 + 4. To print the output of an exp 5 min read Like