I am reading some code that uses _.flow() from lodash and the explanation in the docs just isn't making sense to me.
The doc says
Creates a function that returns the result of invoking the given functions with the this binding of the created function, where each successive invocation is supplied the return value of the previous.
With the example:
function square(n) {
return n * n;
}
var addSquare = _.flow([_.add, square]);
addSquare(1, 2);
// => 9
I have read this a few times and I can not work out what it means or how that function returns 9. The closest think I can think of is folding in functional programming but this doesn't look like that. Is there any alternative way of explaining what flow does?
Rewording the definition from the docs in simpler terms: It calls, in order, the methods in the array. It uses the result from each function as the parameters for the next function. In the case of the example given, the steps are as follows:
_.add(1, 2)
, which returns 3
.square(3)
. This returns 9
.