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
Here is the relevant part of the source code for this function:
return function(...args) {
let index = 0
let result = length ? funcs[index].apply(this, args) : args[0]
while (++index < length) {
result = funcs[index].call(this, result)
}
return result
}
So it first applies the first function to the input arguments; then calls the rest of the functions with each taking up the result of the previous stage.
I can see the following benefits from doing it this way:
- All the applied functions will use the this
argument of the caller in case that is desired.
- On definition, it just returns a function. This allows for lazy evaluation. The flow function can be passed around. The actual calculation will take place only when it is applied to the arguments.