Let’s learn about function composition in Scala

Reading Time: 3 minutes

In this blog, we are going to learn about how a list of functions can be composed to create a single function, in the context of mapping a set of values using those functions.

Introduction

When we compose two functions, we create a new function which applies one function to the result of the other. If I have one function a -> b which takes an a and gives a b, and another b -> c which takes a b and gives a c, I should be able to combine them into one function a -> c which takes an a and gives a c

In Scala, there are multiple ways to define the composition of a function. 

 They are,

  1. Using compose Keyword
  2. Using andthen Keyword
  3. Passing method to method

Scala composition function using compose keyword

Composing method works with val functions. So what you can do is define 2 or more val functions and can use compose on those to mix them and extract the result. Let’s see that through an example:-

def main(args: Array[String]) {

    val composeTwoFunctions = (increment compose multiply)(5)
    val composeThreeFunctions = (subtract compose multiply compose increment)(8)

    println("composeTwoFunctions: " + composeTwoFunctions)
    println("composeThreeFunctions: " + composeThreeFunctions)
  }

  val increment = (num: Int) => {
    num + 1
  }

  val multiply = (num: Int) => {
    num * 2
  }

  val subtract = (num: Int) => {
    num - 1
  }

Output

composeTwoFunctions: 11
composeThreeFunctions: 17

Compose keyword is right associative which means that it is applied from right to left.

So, in the above example, when composeTwoFunctions is computed first the multiply function is called with the value 5 which returns 10. And then the increment function is called, so the final result is 11. Similarly, when composeThreeFunctions is computed, first the increment function is called with the value 8 which returns 9. Then the multiply function is called which returns 18. And finally, the subtract function is called, so the final result is 17.

Scala composition function using andThen keyword

andThen method also works with val functions. So the way to define the val function is exactly the same as in case of compose keyword. The difference is only in the way andThen computes the applied functions. Let’s see that through an example:-

def main(args: Array[String]) {

val composeTwoFunctions = (increment andThen multiply)(4)
val composeThreeFunctions = (subtract andThen multiply andThen increment)(6)

println("composeTwoFunctions: " + composeTwoFunctions)
println("composeThreeFunctions: " + composeThreeFunctions)
}

val increment = (num: Int) => {
num + 1
}

val multiply = (num: Int) => {
num * 2
}

val subtract = (num: Int) => {
num - 1
}

Output

composeTwoFunctions: 10
composeThreeFunctions: 11

andThen keyword is left associative which means that it is applied from left to right.

So, in the above example, when composeTwoFunctions is computed, first the increment function is called with the value 4 which returns 5. And then the multiply function is called, so the final result is 10. Similarly, when composeThreeFunctions is computed, first the subtract function is called with the value 6 which returns 5. Then the multiply function is called which returns 10. And finally, the increment function is called, so the final result is 11.

Scala composition function using method to method

Another way to compose methods is to pass a method as a parameter to another method. So what happens in this case is that the result of the inner most method is computed first and the result of that method is passed to the outer method. Let’s understand that through an example:-

def main(args: Array[String]) {

    val composeTwoFunctions = multiply(increment(7))
    val composeThreeFunctions = increment(multiply(subtract(10)))

    println("composeTwoFunctions: " + composeTwoFunctions)
    println("composeThreeFunctions: " + composeThreeFunctions)
  }

  val increment = (num: Int) => {
    num + 1
  }

  val multiply = (num: Int) => {
    num * 2
  }

  val subtract = (num: Int) => {
    num - 1
  }

Output

composeTwoFunctions: 16
composeThreeFunctions: 19

In the above example, when composeTwoFunctions is computed, increment function is called first with the value 7 which returns 8. And then the multiply function is called, so the final result is 16. Similarly, when composeThreeFunctions is computed, first the subtract function is called with the value 10 which returns 9. Then the multiply function is called which returns 18. And finally, the increment function is called, so the final result is 19.

So, this was all about function composition in scala. I hope you unserstood what is function composition, what are the ways to define them and how they work.

Written by 

Bhavya Verma is a Software Consultant at Knoldus. An avid Scala programmer, he is recognised as a good team player, dedicated and responsible professional, and a technology enthusiast. He has good time management skills. His hobbies include playing badminton, watching movies and travelling.

Leave a Reply