Skip to Content
Mastering JavaScript Functional Programming
book

Mastering JavaScript Functional Programming

by Federico Kereki
November 2017
Intermediate to advanced
386 pages
9h 22m
English
Packt Publishing
Content preview from Mastering JavaScript Functional Programming

Inner state

The notion is also extended to internal variables, in which a local state is stored, and then used for future calls. In this case, the external state is unchanged, but there are side effects that imply future differences as to the returned values from the function. Let's imagine a roundFix() rounding function, that takes into account if it has been rounding too much upwards or downwards, so next time it will round the other way, to make the accumulated difference closer to zero: 

const roundFix = (function() {    let accum = 0;    return n => {        // reals get rounded up or down        // depending on the sign of accum        let nRounded = accum > 0 ? Math.ceil(n) : Math.floor(n);        console.log("accum", accum.toFixed(5), " result", nRounded); accum += ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Mastering JavaScript Functional Programming - Second Edition

Mastering JavaScript Functional Programming - Second Edition

Federico Kereki

Publisher Resources

ISBN: 9781787287440Supplemental Content