Altering a function’s behavior
In the previous section, we considered some ways of wrapping functions so that they maintain their original functionality, even though they’ve been enhanced in some way. Now, we’ll turn to modifying what the functions do so that the new results will differ from their original ones.
We’ll be covering the following topics:
- Revisiting the problem of having a function work, but just once
- Negating or inverting a function’s result
- Changing the arity of a function
- Throttling and debouncing functions for performance
Let’s get started!
Doing things once, revisited
In Chapter 2, Thinking Functionally, 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. We defined once()
with an arrow function then; let’s go with a standard function for variety:
// once.ts function once<T extends (...args...