Scala Tutorial - Learn Function Composition Using Compose
Overview
In this tutorial, we will learn how to create value functions which are defined using the val keyword as opposed to using the def keyword.
Val functions inherit the compose function and we will show how to use the compose function to compose two functions together.
Mathematically speaking, (f compose g)(x) = f(g(x)). The second function g(x) is ran first and its result is passed along to the function f(x).
Note however that the ordering when composing function using the compose method is different to using andThen as shown in the Function Composition Using AndThen Tutorial.
Mathematically speaking, (f andThen g)(x) = g(f(x)). The results of the first function f(x) is ran first and will be passed as input to the second function g(x)
Steps
1. Assume a pre-calculated total cost amount
Let's start with a simple totalCost value which represents the total cost in dollar figure for a particular customer buying donuts from your store.
println("Step 1: Assume a pre-calculated total cost amount")
val totalCost: Double = 10
2. How to define a val function to apply discount to total cost
Similar to the example from the previous tutorial on defining function with val keyword, let's define a val function which will apply some discount dollar value from the total cost figure.
println("\nStep 2: How to define a val function to apply discount to total cost")
val applyDiscountValFunction = (amount: Double) => {
println("Apply discount function")
val discount = 2 // fetch discount from database
amount - discount
}
3. How to call a val function
Calling the val function applyDiscountValFunction from Step 2 is very straight-forward. You simply need to pass it the totalCost value from Step 1.
println("\nStep 3: How to call a val function")
println(s"Total cost of 5 donuts with discount = ${applyDiscountValFunction(totalCost)}")
You should see the following output when you run your Scala application in IntelliJ:
Step 3: How to call a val function
Apply discount function
Total cost of 5 donuts with discount = 8.0
4. How to define a val function to apply tax to total cost
Let's go ahead and define another val function which should apply some tax amount to the totalCost value.
println("\nStep 4: How to define a val function to apply tax to total cost")
val applyTaxValFunction = (amount: Double) => {
println("Apply tax function")
val tax = 1 // fetch tax from database
amount + tax
}
5. How to call compose on a val function
As we've seen from the previous tutorial on defining function with val keyword, val function inherits a compose function.
Calling compose will take the result from the second function and pass it as input parameter to the first function. Let's use the compose semantics to apply tax first and afterwards apply discount to the totalCost figure as shown below.
println("\nStep 5: How to call compose on a val function")
println(s"Total cost of 5 donuts = ${ (applyDiscountValFunction compose applyTaxValFunction)(totalCost) }")
You should see the following output when you run your Scala application in IntelliJ:
Step 5: How to call compose on a val function
Apply tax function
Apply discount function
Total cost of 5 donuts = 9.0
NOTE:
- The apply tax function was called first following which the apply discount function was called.
- The output from the apply tax function was also passed through as input parameter to the apply discount function
- Although in this example, the result is similar to using andThen as shown in the previous tutorial, bear in mind the difference in ordering between andThen and compose.
- Ordering using andThen: f(x) andThen g(x) = g(f(x))
- Ordering using compose: f(x) compose g(x) = f(g(x))
Summary
In this tutorial, we went over the following:
- How to define a value of type Double to represent total cost
- How to define a val function to apply discount to total cost
- How to call a val function
- How to define a value function to apply tax to total cost
- How to call compose on a val function
Tip
- val functions are instances of FunctionN classes and if you look at the Scala documentation for say Function1 class, you will notice that val function will inherit other methods such as andThen or compose which allow for function composition.
Source Code
The source code is available on the allaboutscala GitHub repository.
What's Next
In the next tutorial, I will show you how to define function which is tail recursive.