Skip to content
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

Curry #72

Closed
broadsw0rd opened this issue Dec 13, 2017 · 9 comments
Closed

Curry #72

broadsw0rd opened this issue Dec 13, 2017 · 9 comments

Comments

@broadsw0rd
Copy link

broadsw0rd commented Dec 13, 2017

What about this kind of curry?

const curry = (fn, ...args) =>
  fn.length <= args.length
    ? fn(...args)
    : curry.bind(null, fn, ...args)
@elderhsouza
Copy link
Contributor

Can you show an example of how that would work?
I believe this don't account for currying of variadic functions like Math.min for example.

@broadsw0rd
Copy link
Author

@elderhsouza yes, you right, it was just an example, we have to define arity

const curry = (fn, arity = fn.length, ...args) =>
  arity <= args.length
    ? fn(...args)
    : curry.bind(null, fn, arity, ...args)

// curry(Math.pow)(2)(10) -> 1024
// curry(Math.min, 3)(10)(50)(2) -> 2

@steinslin
Copy link

@elderhsouza the curry may not work

const curry = (f, arity = f.length, next) =>
  (next = prevArgs =>
    nextArg => {
      const args = [ ...prevArgs, nextArg ];
      return args.length >= arity ? f(...args) : next(args);
    }
  )([]);
// curry(Math.min, 3)(10, 200)(2)  -> not work

I think should like this

const curry = (f, arity = f.length, next) =>
  (next = prevArgs =>
    (...nextArg) => {
      const args = [ ...prevArgs, ...nextArg ];
      return args.length >= arity ? f(...args) : next(args);
    }
  )([]);

@elderhsouza
Copy link
Contributor

elderhsouza commented Dec 13, 2017

@broadsw0rd I like this one, haven't used currying like this one yet, but I can see how it can be useful, solid implementation.

@linrui1994 That's really elegant and terse, I tested all versions with the same use cases and it seems to work for all of it.

@Chalarangelo Great discussion here, wanna jump in?

@Chalarangelo
Copy link
Owner

If it works better than the previous one or covers more use-cases, PR it. I like what I see. 😉

@Chalarangelo
Copy link
Owner

Anybody wanna open a PR for this? I'd be glad to merge.

@elderhsouza
Copy link
Contributor

@Chalarangelo Done! 👍

@Chalarangelo
Copy link
Owner

Resolved in #124. Closing...

@lock
Copy link

lock bot commented Dec 18, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for any follow-up tasks.

@lock lock bot locked as resolved and limited conversation to collaborators Dec 18, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants