The _.compose()
method from the Underscore library allows you to chain multiple functions together and create a new function that applies each of them one after the other. Here's an example usage:
index.tsx318 chars19 lines
In the above example, we first define three functions addTwo()
, square()
, and negate()
that we want to compose together. Then we create a new function composedFunc
using _.compose()
, passing in the three functions in order (right-to-left) that we want to apply. Finally, we call composedFunc
with a value of 5
, which applies each of the three composed functions to the value in turn, resulting in -49
.
The _.compose()
function is a higher-order function, which means it takes one or more functions as arguments and returns a new function. This makes it suitable for use in functional programming, where functions are treated as first-class objects and can be passed around and composed together in various ways.
gistlibby LogSnag