GithubHelp home page GithubHelp logo

Curry about 30-seconds-of-code HOT 9 CLOSED

30-seconds avatar 30-seconds commented on April 27, 2024
Curry

from 30-seconds-of-code.

Comments (9)

Chalarangelo avatar Chalarangelo commented on April 27, 2024 1

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

from 30-seconds-of-code.

elderhsouza avatar elderhsouza commented on April 27, 2024

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.

from 30-seconds-of-code.

broadsw0rd avatar broadsw0rd commented on April 27, 2024

@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

from 30-seconds-of-code.

steinslin avatar steinslin commented on April 27, 2024

@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);
    }
  )([]);

from 30-seconds-of-code.

elderhsouza avatar elderhsouza commented on April 27, 2024

@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?

from 30-seconds-of-code.

Chalarangelo avatar Chalarangelo commented on April 27, 2024

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

from 30-seconds-of-code.

elderhsouza avatar elderhsouza commented on April 27, 2024

@Chalarangelo Done! 👍

from 30-seconds-of-code.

Chalarangelo avatar Chalarangelo commented on April 27, 2024

Resolved in #124. Closing...

from 30-seconds-of-code.

lock avatar lock commented on April 27, 2024

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.

from 30-seconds-of-code.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.