Over a million developers have joined DZone.

Partially Applied Functions in Scala

DZone's Guide to

Partially Applied Functions in Scala

A great primer for Scala devs or functional programming buffs in general, check out how (and why) to make use of partially applied functions.

· Java Zone ·
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

Scala, like many other functional languages, allows developers to apply functions partially. What this means is that, when applying a function, a developer does not pass in all the arguments defined by the function. But provides only for some of them, leaving remaining parameters to be passed later.

Once you have provided the required initial parameters, what you get back is a new function whose parameter list only contains those parameters from the original function that were left blank.

I will provide an example to explain the concept. Consider the example below, where we define a method to calculate product price after discount. The method takes in two parameters — the first is the discount to be applied, and the second is product price. 

def calculateProductPrice(discount: Double, productPrice: Double): Double =
(1 - discount/100) * productPrice

We cannot ask shopkeepers to provide discounts every time. We will set the discount once for all the products.

val dicountApplied = calculateProductPrice(30, _: Double)

Notice how I have used placeholder syntax to inform Scala that I am going to provide the value of productPrice later on. Now we can use discountApplied again and again without being bothered about the value of the discount.


This technique has many advantages:

  • We have reduced a method that used to accept multiple arguments to a function that accepts only a single argument, thus making it easier for consumers to use the method.

  • We can safeguard our code by exposing only a partially applied function so that no one else can pass in incorrect arguments by mistake.

You can also refer to this video for more explanation and examples:

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

scala ,functional programming ,partially applied functions ,java ,tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}