January 2020
Intermediate to advanced
470 pages
11h 13m
English
Back in Chapter 2, Thinking Functionally – A First Example, we went through an example of developing an FP-style solution for a simple problem: fixing things so that a given function works only once. The following code is what we wrote back then:
const once = func => { let done = false; return (...args) => { if (!done) { done = true; func(...args); } };};
This is a perfectly fine solution; it works well and we have nothing to object to. We can, however, think of a variation. We could observe that the given function gets called once, but its return value gets lost. This is easy to fix: all we need to do is add a return statement. However, that wouldn't be enough; what would the function return if it's called more? ...
Read now
Unlock full access