Referential Transparency by Substitution

An expression is referentially transparent in a specified context when replacing it with its value preserves observable behavior.

Source
Anbloq
Language
English
Created
Updated
Chapters
2
Reading time
1 min
In this article2 chapters
  1. Try substitution on a calculation
  2. Test what can be observed

Try substitution on a calculation

Replacing a pure sum of 3 and 4 with seven preserves a result computed from that sum. Replacing a call obtaining the next item from a queue with its previous output changes consumption. Compare pure functions.

Test what can be observed

A logging expression might return a stable value and still emit a line each time it runs. Observable effects identify why reusing the value can change the full program.[1]

References

  1. Python Software Foundation, Functional Programming HOWTO ↩