M9 — Functional Programming

SENG 365 — Software Engineering

Neil Ernst

University of Victoria

2026-07-24

Learning Objectives

Learning Objectives

Reading: Robillard, 3rd ed. — Ch. 9, Functional Design (pp. 241–272).

  • The nature of functional programming
  • Java 8+ functional approaches (compared to Scala)
  • Several key functional concepts
  • Leveraging functional idioms for design

Beyond OO

Other Approaches to OO

  • OO models entities as objects (nouns); computation is message passing between objects.
  • Smalltalk: aPen go: 100 + 20 — late binding, dynamic typing.
  • JavaScript uses prototypes instead of classes.
  • Actor model (Erlang/Elixir), CSP (Go) — helpful in distributed/concurrent systems.

Functions as the Unit

  • Functions (the mathematical construct) as the core unit of computation.
  • Functions almost never (Scala) or strictly never (Haskell) have side effects.
  • Side effects are non-deterministic and complicate distributed programming — but essential to getting things done.

Why a Functional Approach

  • FP language idioms (lambdas, purity, typing, monads) have appealing mathematical formality.
  • FP is good for parallelism and concurrency — increasingly necessary at the design level.
  • FP is good at reductive reasoning: side-effect-free code is easier to reason about.

Functional Concepts

Function Composition

  • Build software by composing small functions (cf. “pipe and filter”).
  • Functions as arguments: f(g(x)).
  • A stream is a series of events — think “every student who has or will take this course”: effectively infinite.

Pure Functions

  • No side effects → same arguments yield the same result.
curl http://localhost/numberAfter/5   → [always 6]
curl http://localhost/customer/5/v1   → [always v1]
curl http://localhost/customer/5      → [may differ]

Statelessness and Immutability

  • No state: easy to reason about; all instances equivalent.
  • Immutable: easy to reason about; concurrent access is safe.

Idempotence

Get the same answer regardless of how many times you do it.

resizeTo100px(image) vs. shrinkByHalf(image)

  • Hard to guarantee exactly-once — is the task stalled or failed?
  • Idempotent operations can be safely retried.

Declarative vs. Imperative

  • FP defines what something is, not a procedure to reach it.
  • How much paint do I need?
    • Imperative: while(!done) { fence.paint(); }
    • Declarative: paint(l=20, w=3)

Reactive Programming (not React)

  • Optimize for data-centric programs.
  • Large apps are effectively infinite streams of events.
  • Operate on stream events (e.g. group a user’s events into “buy a new computer”).
  • Related: serverless, CQRS, event sourcing.

FP in Java

First-Class Functions

Functions are first-class program entities (before Java 8, only objects were).

  • Formerly, a Comparator was just a function wrapped in an object.
  • Java added higher-order functions — passing functions to functions.

Three Key Language Supports

  • Functional interfaces — one abstract method (e.g. java.util.function.Predicate).

  • Lambdas — anonymous functions without class ceremony:

    Predicate<Card> blackCardFilter =
      (Card card) -> card.getSuit().getColor() == Suit.Color.BLACK;
  • Method references — a pointer to the method itself:

    cards.removeIf(Card::hasBlackSuit);

FP and the Gang of Four

  • Many GoF patterns are idiomatic in FP — no separate pattern needed.
  • Observer as a lambda (Mario Fusco): a ConcurrentHashMap + Consumer radically reduces code.
  • Easier to read and maintain (given basic Java 8 / FP knowledge).

FP and Design

In-Class Activities

Day 1

  • Group discussion of EX8

Day 2 — Cube Composer

  • Cube Composer: find a laptop and work through at least levels 0 and 1.
  • Function composition; pure functions; statelessness; idempotence; declarative style.

Day 3

  • Q&A and office hours