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

By Nadim Bahadoor | Last updated: July 25, 2017 at 13:48 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 Partial 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 partial 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 partial function named totalCostForGlazedDonuts which will call the curried totalCost() function from Step 1.


println("\nHow to create a partial 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 partial function because it only fills in the first parameter Glazed Donut. It uses the wildcard _ as a placeholder for the other parameters.

4. How to call a partial function

Calling the partial 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 partial 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 partial 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 partial function from a function with curried parameter groups
  • How to call a partial 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
Senior Software Developer | Nephila Capital
Founder of allaboutscala.com. I have over 10 years of experience in building large scale real-time trading systems in the financial industry. Passionate about Distributed Systems, Scala, Big Data and Functional Programming. Stay in touch for upcoming tutorials!
Other allaboutscala.com tutorials you may like:

Share this article on