Functions & Relations

Difficulty
Importance

The problem

Once we can describe collections, we need to describe how elements of one collection correspond to elements of another — deterministically (functions) or loosely (relations). Without this, 'mapping', 'lookup', and 'transformation' have no precise meaning.

Why now

Sets alone are static — they say what exists, not how things connect. Logic alone judges truth, not correspondence. Describing 'every user has exactly one email' or 'orders reference products' requires a formal notion of mapping between sets.

Mental model

A function is a machine with a guarantee: same input, same output, always exactly one. A relation drops the guarantee — it is just a set of pairs, any connection you can write down. Hash maps, database foreign keys, and pure functions in code are all this one idea wearing different clothes.

Requires

Unlocks

Parallel

Used in

Projects

  • Model a small e-commerce schema purely as relations between sets, then identify which relations are functions
  • Take three impure functions from your own code and refactor each into a pure function plus an effect

Examples

  • Map<UserId, Email> is a function; the 'follows' table on a social network is a relation that is not a function
  • Injective = no collisions (perfect hashing); surjective = every output reachable (full test coverage of states)

Resources

Mastery checklist