0

We have discovered some files in our app that are using the EOL React library recompose.

After looking into function composition, It sounds like something Javascript is able to do natively

Assuming foo and bar are Higher Order Functions(Components) that both return a function that takes another function.

Would someone be able to explain the difference between using a function to compose other functions like this:

compose(foo(arg), bar(arg2));

And just combining the functions without a library like this:

foo(arg)(bar(arg2))

The docs for compose read:

Use to compose multiple higher-order components into a single higher-order component. This works exactly like the function of the same name in Redux, or lodash's flowRight().

And the docs for lodash/flowRight read:

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.

So it sounds like the only difference is what this binding is supplied to each function invocation - is there any other difference between the two?

Daniel Cooke
  • 1,426
  • 12
  • 22
  • Possible duplicate of [this](https://stackoverflow.com/questions/41357897/understanding-compose-functions-in-redux) – tarzen chugh Apr 29 '19 at 10:10
  • 2
    `compose(foo(arg), bar(arg2))` evaluates to `arg3 => foo(bar(arg3)`, i.e. it would compose the partially applied `foo`/`bar`. This only works if both funs expect at least 2 arguments and are curried. With `compose` your 2nd example `foo(arg)(bar(arg2))` would look like `compose(foo(arg), bar) (arg2)`. –  Apr 29 '19 at 11:00
  • I wouldn't be in a huge rush to rewrite that, sounds from reading the README and Dan Abramov's comments on the thread that they're going to continue adding bugfixes and updating for newer versions of React. – Jared Smith Apr 29 '19 at 13:37

0 Answers0