9.6. Using Partially Applied Functions

Problem

You want to eliminate repetitively passing variables into a function by (a) passing common variables into the function to (b) create a new function that is preloaded with those values, and then (c) use the new function, passing it only the unique variables it needs.

Solution

The classic example of a partially applied function begins with a simple sum function:

val sum = (a: Int, b: Int, c: Int) => a + b + c

There’s nothing special about this sum function, it’s just a normal function. But things get interesting when you supply two of the parameters when calling the function, but don’t provide the third parameter:

val f = sum(1, 2, _: Int)

Because you haven’t provided a value for the third parameter, the resulting variable f is a partially applied function. You can see this in the REPL:

scala> val sum = (a: Int, b: Int, c: Int) => a + b + c
sum: (Int, Int, Int) => Int = <function3>

scala> val f = sum(1, 2, _: Int)
f: Int => Int = <function1>

The result in the REPL shows that f is a function that implements the function1 trait, meaning that it takes one argument. Looking at the rest of the signature, you see that it takes an Int argument, and returns an Int value.

When you give f an Int, such as the number 3, you magically get the sum of the three numbers that have been passed into the two functions:

scala> f(3)
res0: Int = 6

The first two numbers (1 and 2) were passed into the original sum function; that process created the new function named f

Get Scala Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.