Scala Lesson 24 – Currying | Dataplexa

Currying

In this lesson, you will learn about Currying in Scala. Currying is a functional programming technique that transforms a function with multiple parameters into a sequence of functions, each taking a single parameter.

Currying helps you write reusable, modular, and expressive code and is widely used in advanced Scala applications.


What Is Currying?

Currying is the process of breaking a function that takes multiple arguments into a series of functions that take one argument at a time.

Instead of this:

def add(a: Int, b: Int): Int = a + b

Currying rewrites it like this:

def add(a: Int)(b: Int): Int = a + b

Why Use Currying?

Currying allows:

  • Partial application of functions
  • Cleaner functional composition
  • Better reuse of logic
  • More readable and expressive code

Basic Curried Function Example

Here is a simple curried function that multiplies two numbers.

def multiply(a: Int)(b: Int): Int = a * b

val result = multiply(3)(4)
println(result)

The function is called using two sets of parentheses.


Partial Application with Currying

One of the biggest advantages of currying is partial application. You can supply some arguments now and the rest later.

def add(a: Int)(b: Int): Int = a + b

val addFive = add(5) _
println(addFive(10))

Here, addFive becomes a reusable function that always adds 5.


Currying with Lambdas

Currying also works naturally with lambda expressions.

val multiplyCurried = (a: Int) => (b: Int) => a * b

println(multiplyCurried(2)(6))

This example shows how currying can be expressed using anonymous functions.


Currying in Real-World Scala Code

Currying is commonly used in:

  • Configuration-based logic
  • Reusable validation functions
  • Functional libraries and frameworks
  • Collection operations

Currying with Collections

Currying becomes powerful when combined with higher-order functions.

def filterGreaterThan(limit: Int)(value: Int): Boolean = {
  value > limit
}

val numbers = List(5, 10, 15, 20)

val greaterThan10 = numbers.filter(filterGreaterThan(10))
println(greaterThan10)

This allows reusable filtering logic without duplicating code.


Currying vs Normal Functions

Normal functions:

  • Take all parameters at once
  • Are simpler for basic use cases

Curried functions:

  • Allow partial application
  • Support functional composition
  • Work better in advanced functional designs

📝 Practice Exercises


Exercise 1

Create a curried function that subtracts two integers.

Exercise 2

Use partial application to create a function that always multiplies by 10.

Exercise 3

Write a curried function to check if a number is divisible by a given divisor.


✅ Practice Answers


Answer 1

def subtract(a: Int)(b: Int): Int = a - b

Answer 2

def multiply(a: Int)(b: Int): Int = a * b

val timesTen = multiply(10) _
timesTen(7)

Answer 3

def divisibleBy(divisor: Int)(number: Int): Boolean = {
  number % divisor == 0
}

divisibleBy(3)(9)

What’s Next?

In the next lesson, you will learn about Traits, which are used to share behavior across Scala classes in a flexible way.