Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the lodash flow function do?

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?

like image 498
Qwertie Avatar asked Nov 28 '18 00:11

Qwertie


People also ask

What is the purpose of lodash?

Lodash is a popular javascript based library which provides 200+ functions to facilitate web development. It provides helper functions like map, filter, invoke as well as function binding, javascript templating, deep equality checks, creating indexes and so on.

What is lodash throttle?

throttle() method in lodash is used to create a throttled function that can only call the func parameter maximally once per every wait milliseconds.

What is lodash in react?

Lodash is a JavaScript utility library. It provides a variety of functions for manipulating arrays, objects, strings, and numbers. ( source: Lodash) Since Lodash is written in Javascript, we can use it in any React application. Using Lodash in your React application is a simple and straightforward process.

What is lodash merge?

Lodash's `merge()` Function Given two objects destination and source , Lodash's merge() function copies the 2nd object's own properties and inherited properties into the first object.


2 Answers

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:

  1. Invokes _.add(1, 2), which returns 3.
  2. Passes that return value as the parameter to the next function in the array, which becomes square(3). This returns 9.
like image 185
Matt Avatar answered Oct 09 '22 21:10

Matt


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.

like image 25
jrook Avatar answered Oct 09 '22 19:10

jrook