← Lessons

quiz vs the machine

Silver1080

Concurrency

Pure Functions and Parallelism

No side effects means any order is a safe order.

4 min read · intro · beat Silver to climb

What makes a function pure

A pure function depends only on its arguments and changes nothing outside itself. Same inputs, same output, every time, with no hidden writes to global state, files, or the network.

Why purity helps parallelism

  • No shared mutable state means two pure calls cannot interfere.
  • The runtime is free to run them on different cores in any order.
  • Results can be cached because the answer never depends on timing.

When a computation is a tree of pure functions, the only constraint on scheduling is data dependency: a node must wait for its inputs, but independent branches can run at the same time.

Finding the parallelism

A pure expression like a sum over a large list can be split into chunks. Each chunk is summed independently, then the partial results are combined. Because addition is associative and the work is pure, the split is always correct.

The catch

Purity guarantees correctness, not speed. Tiny tasks lose to scheduling overhead, so good runtimes batch work into reasonably sized chunks.

Key idea

Pure functions have no side effects, so independent calls can run on any core in any order with the same result.

Check yourself

Answer to earn rating on the learn ladder.

1. What is the only ordering constraint among pure computations?

2. Why can results of pure functions be cached?