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

Simple memoization

We will work with the Fibonacci function we mentioned, which is a simple case: it receives a single numeric parameter. The function, as we saw it, was the following:

function fib(n) {    if (n == 0) {        return 0;    } else if (n == 1) {        return 1;    } else {        return fib(n - 2) + fib(n - 1);    }}

The solution we did there was general in concept, but particularly in its implementation: we had to directly modify the code of the function in order to take advantage of said memoization. Now we should look into a way of doing it automatically, in the same fashion as with other wrapped functions. The solution would be a memoize() function that wraps any other function, to apply memoization:

const memoize = fn => {    let cache = {};    return x => ...
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