Reference ยท Functional Programming in Scala

Pure Functions

A compact reference for Lesson 01.

Standalone

The example uses only synthetic readings defined in the source file. No VPS, account, private repository, production data, network service, or CLI is required.

Optional Scala source for readers who want to compare syntax.

Definition

A pure function returns the same result for the same explicit inputs and has no observable side effect.

Test

f(input) == f(input)

If the result can change because of a clock, random source, file, network, mutable global, or hidden state, that dependency belongs in the function's inputs or at an explicit effect boundary.

Flow Insight example

final case class Reading(meterId: String, liters: Double)

def usageFor(meterId: String, readings: List[Reading]): Double =
  readings
    .filter(_.meterId == meterId)
    .map(_.liters)
    .sum

Boundary rule

Rust translation

Think of a pure Scala function like a Rust function that receives its data explicitly and returns a value without touching global state or performing I/O.