The BackendDeeper

Kayavak / The Backend / BE·37 Functional Programming / Deeper

Deeper · The Backend

Functional Programming

The questions the chapter doesn't stop for: why this exists, the model to keep in your head, what changes when an assumption does, how sure we are of each claim, and what's still open. Read it after the chapter; back to the chapter.

Why does this exist?

Functional programming is older than computers. Alonzo Church's lambda calculus (1930s) described computation purely as applying functions, and John McCarthy's Lisp (1958) brought it to machines. ML, Haskell (1990) and others explored pure functions, immutability and types, mostly in research. Mainstream languages kept loops and mutation.

Two pressures changed that. Multicore processors (mid-2000s) made shared mutable state a source of race conditions, and code without mutation is safe to run in parallel. And data processing at scale (Google's MapReduce, 2004, named after two functional operations) showed that describing what to compute lets a system decide how. Java added lambdas and streams in Java 8 (2014); C#, JavaScript, Python and C++ added similar features in the same decade. Gatherers (final in JDK 24) let streams carry custom intermediate steps.

The mental model

Try it yourself first: write your own model in the Mental Model Explorer, then compare.

Describe computation as values flowing through functions that don't change anything else. Keep the parts that do change things (I/O, databases, clocks) at the edges, so the core is easy to test and safe to reorder.

Question Answer
What is it? A style using functions as values, pure functions, immutable data and composition
Problem it solves Code that's easier to reason about, test and parallelise, because results depend only on inputs
Parts Lambdas, method references, functional interfaces, streams (source, intermediate, terminal operations), Optional, collectors
How it works Streams build a pipeline lazily; the terminal operation pulls elements through it, one at a time
Assumptions The functions you pass are pure and don't depend on order
When they change Side effects inside map or filter run in surprising order, or not at all, or concurrently in parallel streams
Where it fails Debugging long pipelines; checked exceptions inside lambdas; performance of boxed streams
Related ideas Pure functions (Foundations, ch02); laziness; concurrency (ch38)

What if?

What if a stream had no terminal operation?

Reason it through, then open

Nothing would happen. list.stream().map(x -> { log(x); return x; }) without toList() or forEach never calls the lambda, because streams are lazy: intermediate operations only describe the pipeline. That's what lets filter and limit stop early on an infinite stream, and why side effects in intermediate steps are unreliable.

What if you made a sequential stream .parallel()?

Reason it through, then open

The work would be split across the common fork-join pool. Pure, independent operations on large in-memory data might run faster. Lambdas that touch shared state would race; order-dependent operations (findFirst, forEachOrdered) would cost coordination; small streams would be slower from the overhead; and blocking I/O would starve the shared pool for the whole application. Parallelism is only free for pure functions on large, splittable data.

Where the boundary is

Aspect Pure function Function with side effects
Result depends on Only its arguments Also on, or changes, outside state
Call it twice Same result, nothing else changes Maybe different results, maybe two database rows
Test Inputs and outputs Set up and inspect the outside world
Reorder or parallelise Safe Unsafe without thought

How sure are we?

Claim How it's known Where from
Streams are lazy until a terminal operation Convention java.util.stream specification
Pure functions are referentially transparent Theorem By definition: a call can be replaced by its result
Functional style reduces defects Active research Plausible and widely argued; controlled evidence is limited

Still open

How far mainstream languages should go towards effect tracking (making side effects part of a function's type, as Haskell and newer research languages do) is unsettled.

What would change your mind? If pipelines of pure functions were consistently harder for teams to maintain than loops, the readability argument would fall away; the correctness argument would remain.

Recall

Why does a stream without a terminal operation do nothing?

Intermediate operations are lazy; only a terminal operation pulls elements through.

When is `.parallel()` safe and worthwhile?

For pure, independent operations on large in-memory data that splits well.

What makes a function pure?

Its result depends only on its arguments and it changes nothing outside itself.

What is "functional core, imperative shell"?

Keep logic in pure functions and push I/O and other effects to a thin outer layer.

Reflect

Be honest with yourself; this stays in your browser. Anything you can't tick yet is what to revisit.