Altering functions
In the previous section, we considered some ways of wrapping functions, so they would maintain their original functionality, though enhanced in some ways. Now we'll turn to actually modifying what the functions do, so the new results will actually differ from the original function's ones.
Doing things once, revisited
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 a given function would work only once:
const once = func => { let done = false; return (...args) => { if (!done) { done = true; func(...args); } }; };
This is a perfectly fine solution, 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. That's easy to fix, however; all we require is adding a return
statement. However, that wouldn't be...