liveBook · Manning

Pure function

As explained in Deep Learning with R, Third Edition

A pure function produces results solely from its explicit inputs and has no externally observable side effects.

A pure function produces the same result from the same explicit inputs without relying on hidden global state or externally observable side effects. Random-number generation makes the idea concrete: a global random seed can change results between calls, while explicit seed arguments or JAX PRNG keys make the random state part of the function's inputs. section 3.5.3

Explicit seeds make noise generation repeatable

apply_noise <- function(x, seed) { set.seed(seed) x + array(runif(length(x), -1, 1)) } identical(apply_noise(x, 1337), apply_noise(x, 1337)) # [1] TRUE Changing the seed produces a different deterministic result. In R, restoring the original global seed is also necessary for the function to be truly pure; withr::local_seed() handles that detail. section 3.5.3

JAX makes purity and statelessness operational through explicit PRNG keys:

  • Stateless operations support determinism and automatic vectorization. section 3.5.3

How Deep Learning with R, Third Edition covers it

How deep it goes
Explains repeatability through random keys and seed management, while stopping short of a general side-effect taxonomy.
Examples
Demonstrates R noise generation with explicit seeds and JAX normal sampling with repeated and split PRNG keys.
What it assumes
Assumes basic function calls and random-number generation, while teaching why hidden global random state undermines purity.
What you can do after
Apply it via explicit seeds or JAX PRNG keys to reproduce random computations and manage new random states deterministically.
How it connects
Connects pure functions with statelessness, pseudorandom number generation, determinism, vectorization, and global state.

Also explained in

Compare how 19 books explain pure function →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with R, Third Edition. The section links point to the parts of the book it draws on.