04: Currying, partial application & functional composition in Scala

Let’s see how we can wire up functions with & without currying, and then touch on functional composition.

Without currying

Here are simple functions example that add, add10, multiply and multiplyBy10 without currying.


With currying & partial application with functional composition

Note: You can also do


What is Currying?

Currying is the process of converting a function with multiple arguments into a sequence of functions that take one argument.

Why is it useful?

Currying allows us to create new functions with some pre-defined data in their closure scope. This removes repetition & promotes code reuse.

It lets you specialise or partially apply functions using a lightweight syntax and then pass these partially applied functions around to higher order function such as map or filter. Higher order functions are the essence of functional programming where currying and partially applied functions enable higher order functions to be concise & effective.

What is a Partial Applied Function (i.e. PAF)

This is the process of applying a function to some of its arguments. A partially-applied function gets returned for later use. In other words, a PAF is a function that takes a function with multiple parameters and returns a function with fewer parameters.

What is Functional composition?

The term “composition of functions” (or “composite function”) refers to the combining together of two or more functions in a manner where the output from one function becomes the input for the next function.

In mathematics, function composition is an operation that takes two functions f and g and produces a function h such that h(x) = g(f(x)). In this operation, the function g is applied to the result of applying the function f to x.

f(x) is add10: Int => Int = add(10) _

g(x) is def multiplyBy10 = multiply(10) _

h(x) = g(f(x)) which is multiplyBy10.compose(add10)


This is same as Int => Int

Another Example – currying, PAF & composing


Java Interview FAQs

800+ Java Interview Q&As