Scala Lesson 23 – Lambdas and Closures | Dataplexa

Lambdas & Closures

In this lesson, you will learn about Lambdas and Closures in Scala. These concepts are central to functional programming and are used extensively in modern Scala applications.

Lambdas allow you to write short, anonymous functions, while closures allow those functions to capture and use variables from their surrounding scope.


What Is a Lambda Function?

A lambda function is an anonymous function that does not have a name. It is often used for short operations where defining a full function would be unnecessary.

Scala supports lambda functions natively, making code concise and expressive.


Basic Lambda Syntax

The general syntax of a lambda function in Scala is:

(parameters) => expression

Here is a simple example that adds two numbers using a lambda:

val add = (a: Int, b: Int) => a + b

println(add(3, 5))

The lambda takes two integers and returns their sum.


Lambdas with Collections

Lambda functions are most commonly used with Scala collections such as lists, sets, and maps.

For example, using map to transform each element:

val numbers = List(1, 2, 3, 4)

val doubled = numbers.map(n => n * 2)

println(doubled)

The lambda n => n * 2 is applied to each element in the list.


Short Lambda Syntax

Scala provides a shorter syntax for lambdas when each parameter is used only once. This is done using the underscore character (_).

val squared = numbers.map(_ * _)

println(squared)

The underscore represents each element in the collection.


Multiple Parameters with Lambdas

Lambdas can also accept multiple parameters.

val multiply = (x: Int, y: Int) => x * y

println(multiply(4, 6))

This lambda multiplies two numbers and returns the result.


What Is a Closure?

A closure is a function that captures variables from its surrounding environment. These captured variables remain available even after the surrounding scope has finished execution.

Closures allow functions to remember values outside of their own definition.


Closure Example

In the example below, the lambda captures the variable factor.

var factor = 3

val multiplyByFactor = (x: Int) => x * factor

println(multiplyByFactor(5))

factor = 4

println(multiplyByFactor(5))

The lambda reflects the updated value of factor, showing that it has captured the variable by reference.


Closures in Real Applications

Closures are commonly used for:

  • Configuration-based logic
  • Callbacks and event handlers
  • Custom validation rules
  • Reusable business logic

Lambdas vs Named Functions

Use lambdas when:

  • The logic is short and simple
  • The function is used only once

Use named functions when:

  • The logic is complex
  • The function is reused in multiple places

📝 Practice Exercises


Exercise 1

Create a lambda function that subtracts two integers.

Exercise 2

Use a lambda with map to triple all values in a list.

Exercise 3

Create a closure that adds a fixed bonus value to a salary.


✅ Practice Answers


Answer 1

val subtract = (a: Int, b: Int) => a - b

Answer 2

val nums = List(2, 4, 6)
val tripled = nums.map(_ * 3)
tripled

Answer 3

val bonus = 1000
val addBonus = (salary: Int) => salary + bonus

addBonus(5000)

What’s Next?

In the next lesson, you will learn about Currying, which builds on lambdas and higher-order functions to create even more flexible Scala code.