Using functions in FP ways
Several common coding patterns take advantage of the FP style, even if you aren’t aware of it. In this section, we will go through them and look at the functional aspects of the code so that you can get more accustomed to this coding style.
Then, we’ll look in detail at using functions in an FP way by considering several FP techniques, such as the following:
- Injection, which is needed for sorting different strategies, as well as other uses
- Callbacks and promises, introducing the continuation-passing style
- Polyfilling and stubbing
- Immediate invocation schemes
Injection – sorting it out
The Array.prototype.sort()
method provides the first example of passing functions as parameters. If you have an array of strings and you want to sort it, you can just use the sort()
method. For example, to alphabetically sort an array with the colors of the rainbow, we would write something like the following:
// sort...