-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
composition can be seeded with multiple arguments #1050
Conversation
Hey @Bjvanminnen, If you need a multiple-arg compose in your app could you not use lodash's compose? |
I could, and am fine with doing so. However, it also seems reasonable to expect the compose provided by redux to support this functionality. |
Watch out for an empty function list. The way compose is written in this PR, compose()(x) will throw an error. Currently redux's |
I would argue that neither redux nor lodash's current behavior really makes sense. I can't think of any good reason one would call compose with no functions, but if you did, I would think it would make more sense for the result to be That said, I understand the value in not changing existing behavior where there's not a clearly better behavior. I will update my PR and add a test for this, hopefully later this evening. |
Latest commit ensures that we don't change existing behavior for case where no funcs are passed. |
@@ -6,5 +6,9 @@ | |||
* left. For example, compose(f, g, h) is identical to arg => f(g(h(arg))). | |||
*/ | |||
export default function compose(...funcs) { | |||
return arg => funcs.reduceRight((composed, f) => f(composed), arg) | |||
return (...args) => { | |||
return funcs.length ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is a little hard to grasp what this is doing at first.
Maybe re-writing this as an if
statement would make this function a bit easier to follow.
Stopped trying to do this as a one-liner, and I think it makes it a lot easier to follow. |
Would this be a breaking change? |
It shouldn't be. If given no funcs, we return the first argument, same as before. |
composition can be seeded with multiple arguments
Out in 3.0.6, thanks! |
Related to #632 (comment)
This allows us to provide our composition with multiple arguments.
It does so by pulling off the last function from our list, calling it with the provided args, and using the result as the initialValue for reduceRight (which we then call on the remaining functions). Unfortunately, this does make our simple one liner a little more complex
Let me know if you see a cleaner approach.