Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4062750
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:38:17+00:00 2026-05-20T15:38:17+00:00

I’m trying to create curry function that can be applied to any function and

  • 0

I’m trying to create curry function that can be applied to any function and return another, with 1 of the arguments applied.
Properties that I want to have:

  1. If function has only one argument curry function should return value:
    f(a); curry(f,x) = f(x);
  2. If function has many arguments currey should retrun curried function:
    g(a1,a2,..,aN); curry(g,x) = g2(a2,..,aN) : g2(a2,..aN)=g(x,a2,…,aN)
  3. Length propery of curried function should work “as needed”
    g.length = N => curry(g,x).length = N-1

There is some implementations of curry in Prototype Framework and discussion in one blog. But this implementation is not good because it doesn’t work well on functions with only one argument (1), and also returning function ‘length’ attribute is 0 (3).

For first property there is an easy implementation:

 function curry(f,x) {
    if (f.length == 1) return f(x);
    ...
 }

But I don’t know how to work with 3rd rule, i.e. function can be constucted as inner function since there will be a nested Lexical Environment and will be able to use f:

function curry(f,x) {
   return function() { ... }
}

but in this case I’ll no longer will able to explicitly set parameters.
On the other hand function can be constructed with ‘new Function’ statement, smth like that:

 function curry(f,x) {
    var args = [];
    for (var i=1; i<f.length; i++) {
       args.push('a'+i);
    }
    var sa = args.join();
    return new Function(sa,"return f(x,"+sa+")");
 }

But in this situation f and x will unbound because anonymous function will be created in
Global Lexical Environment.

So the questions:

  1. is there a way to explicitly set parameters count when creating function with function keyword?
  2. is there a way to set Environment of function created with ‘new Function’ statement?
  3. us there a way to solve my problem in any other way?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T15:38:18+00:00Added an answer on May 20, 2026 at 3:38 pm

    The way that the Functional library implements it is to take the parameters passed in to “curry()” as the first parameters to be passed. The function result of the “curry” operation will then take any additional parameters passed in when it is invoked and add them at the end of the argument list. It doesn’t worry at all about argument list length, because that’s not a fixed thing in JavaScript generally so there’s really no point.

    Thus:

    var curry = myFunction.curry("Tuesday", x + y);
    

    So calling:

    curry(100, true);
    

    will be just like calling:

    myFunction("Tuesday", x + y, 100, true);
    

    Functional has another function called “partial()” that allows a more controlled substitution of parameters. When you call “partial()”, you pass in a dummy argument (“_”) to indicate where “holes” are in the argument list:

    var partialFunc = myFunction.partial("Tuesday", _, 100, true, _, "banana");
    

    Those two “_” parameters mean that the resulting “partialFunc” should drop first two arguments passed to it into those slots in the argument list:

    partialFunc(x + y, "Texas");
    

    is thus just like calling:

    myFunction("Tuesday", x + y, 100, true, "Texas", "banana");
    

    I heartily recommend getting that library and looking at the code involved. It’s surprisingly succinct and clear.

    One more thing: it’s important to note that because JavaScript is not a lazy-evaluation language, this isn’t really the same as the “curry” operation in a lazy functional language like Haskell. The distinction is that the arguments at “curry time” are evaluated and therefore sort-of “cooked” into the result. In a lazy language, things are different.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.