Scala Tutorial - Learn How To Create Function Currying With Parameter Groups

By Nadim Bahadoor | Last updated: May 31, 2018 at 12:12 pm

Overview

In this tutorial, we will learn how to create functions whose parameters are organized into parameter groups - also known as Function Currying.

 

In addition, you will be introduced to creating Partially Applied Functions from curried functions.

Steps

1. How to define function with curried parameter groups

By now you should be familiar with defining functions with parameters if you have followed Chapter 3 tutorials on functions.

 

However, Scala also allows you to create functions where each parameter is enclosed within its own group using the () as shown below:


println("Step 1: How to define function with curried parameter groups")
def totalCost(donutType: String)(quantity: Int)(discount: Double): Double = {
  println(s"Calculating total cost for $quantity $donutType with ${discount * 100}% discount")
  val totalCost = 2.50 * quantity
  totalCost - (totalCost * discount)
}

NOTE:

  • Functions defined with parameter groups are also commonly referred to as curried functions.
  • Each parameter is enclosed within () and there is no need to separate each parameter with comma as you would when defining a function with parameters.

2. How to call a function with curried parameter groups

When calling a curried function, you will need to fill in its parameters by enclosing each parameter within () as shown below:


println("\nStep 2: How to call a function with curried parameter groups")
println(s"Total cost = ${totalCost("Glazed Donut")(10)(0.1)}")

You should see the following output when you run your Scala application in IntelliJ:


Step 2: How to call a function with curried parameter groups
Calculating total cost for 10 Glazed Donut with 10.0% discount
Total cost = 22.5

 

3. How to create a partially applied function from a function with curried parameter groups

If you recall from the Scala Features tutorial, functions are designed from the ground up to be first class citizens in the language.

 

To this end, one common application of curried function is to be a building block where you can reuse functions by creating partial functions.

 

As a simple example, let's create a partially applied function named totalCostForGlazedDonuts which will call the curried totalCost() function from Step 1.


println("\nStep 3: How to create a partially applied function from a function with curried parameter groups")
val totalCostForGlazedDonuts = totalCost("Glazed Donut") _

NOTE:

  • totalCostForGlazedDonuts function was defined as a value function using the val keyword as opposed to the def function. In upcoming tutorials, we will show additional examples of value functions.
  • totalCostForGlazedDonuts is a partially applied function because it only fills in the first parameter Glazed Donut. It uses the wildcard _ as a placeholder for the other parameters.
  • Note that the return type of the partially applied function totalCostForGlazedDonuts is Int => Double => Double. The first Int is for our quantity parameter, the Double is for discount parameter and the last Double the return type of the function. In short, the partially applied function creates a chain of functions.

4. How to call a partially applied function

Calling the partially applied function totalCostForGlazedDonuts is no different than how you've called the curried function totalCost in Step 2 by enclosing each parameter within ().

 

However, you do not need to fill in the first parameter for the donutType String parameter as you have already pre-filled it with the Glazed Donut String value in Step 3.


println("\nStep 4: How to call a partially applied function")
println(s"\nTotal cost for Glazed Donuts ${totalCostForGlazedDonuts(10)(0.1)}")

You should see the following output when you run your Scala application in IntelliJ:


Step 4: How to call a partially applied function
Calculating total cost for 10 Glazed Donut with 10.0% discount
Total cost for Glazed Donuts 22.5

This concludes our tutorial on Learn How To Create Function Currying With Parameter Groups and I hope you've found it useful!

 

Stay in touch via Facebook and Twitter for upcoming tutorials!

 

Don't forget to like and share this page :)

Summary

In this tutorial, we went over the following:

  • How to define function with curried parameter groups
  • How to call a function with curried parameter groups
  • How to create a partially applied function from a function with curried parameter groups
  • How to call a partially applied function

Tip

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 takes another function as its parameter.

Nadim Bahadoor on FacebookNadim Bahadoor on GithubNadim Bahadoor on LinkedinNadim Bahadoor on Twitter
Nadim Bahadoor
Technology and Finance Consultant with over 14 years of hands-on experience building large scale systems in the Financial (Electronic Trading Platforms), Risk, Insurance and Life Science sectors. I am self-driven and passionate about Finance, Distributed Systems, Functional Programming, Big Data, Semantic Data (Graph) and Machine Learning.
Other allaboutscala.com tutorials you may like: