September 2018
Intermediate to advanced
302 pages
7h 17m
English
Pure functions are referentially transparent, meaning that their function invocation can be replaced with its corresponding outcome for a given argument.
Now, look at the examples of referentially-transparent and referentially-opaque functions:
let globalValue = 0; const inc1 = (num) => { // Referentially opaque (has side effects) globalValue += 1; return num + globalValue; } const inc2 = (num) => { // Referentially transparent return num + 1; }
Let's imagine a mathematical expression:
inc(4) + inc(4) * 5// With referentially transparent function you can simplify to:inc(4) * ( 1 + 1*5 )// and even toinc(4) * 6
Be aware that you need to avoid such simplifications if your function is not referentially transparent. ...
Read now
Unlock full access