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 6900725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:34:58+00:00 2026-05-27T07:34:58+00:00

I was playing with CoffeeScript when I found myself writing the following lines and

  • 0

I was playing with CoffeeScript when I found myself writing the following lines and then looking at them in awe:

compose = (f, g) -> (x) -> f g x
curry = (f) -> (x) -> (y) -> f(x, y)
uncurry = (f) -> (x, y) -> (f x) y

How nice, did I think! Now, as an exercise, I thought I would generalize the curry and uncurry functions to n args, to obtain something similar to this:

curry2 = (f) -> (x) -> (y) -> f(x, y)
curry3 = (f) -> (x) -> (y) -> (z) -> f(x, y, z)
curry4 = (f) -> (x) -> (y) -> (z) -> (t) -> f(x, y, z, t)

And the same thing for uncurry:

uncurry2 =  (f) -> (x, y) -> (f x) y
uncurry3 = (f) -> (x, y, z) -> ((f x) y) z
uncurry4 = (f) -> (x, y, z, t) -> (((f x) y) z) t

Writing the n-ary uncurry was not very hard:

uncurry = (n) -> (f) -> (args...) ->
    if n == 1
        f args[0]
    else
        ((uncurry n - 1) f args.shift()) args...

On the other hand, I can’t figure out how to get the n-ary curry to work. I thought of implementing first a curry_list function that is the generalization of this suite:

curry_list2 = (f) -> (x) -> [x, y]
curry_list3 = (f) -> (x) -> (z) -> [x, y, z]
curry_list4 = (f) -> (x) -> (z) -> (t) -> [x, y, z, t]

Here’s the implementation:

curry_list = (n) ->
    curry_list_accum = (n, accum) ->
        if n
            (x) ->
                accum.push x
                curry_list_accum n - 1, accum
        else
            accum
    curry_list_accum n, []

And then I would just compose curry_list with function application to obtain currying. That’s what I tried to do:

curry = (n) ->
    apply_helper = (f) -> (args) -> f args...
    (f) -> compose (apply_helper f), (curry_list n)

But for some reason, it does not work. For exemple, trying to evaluate

curry(3)((a,b,c) -> a + b + c)(1)(2)(3)

yields the following error:

Function.prototype.apply: Arguments list has wrong type

Now after jotting some more notes I understand that trying to compose f with curry_list is incorrect. I do have the intuition that what I’m looking for is something that looks like this composition, but is not exactly that. Am I correct in thinking that?

Finally, what would be a correct implementation?

  • 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-27T07:34:59+00:00Added an answer on May 27, 2026 at 7:34 am

    You are returning the composed function after curry(3)((a,b,c) -> a + b + c), not the accumulator.

    That means ((args) -> f args...) is receiving a function as argument, your code doesn’t wait until the argument list is complete to call f.

    Maybe implement this without composition?

    accumulator = (n, accum, f) ->
        return f accum... if n is 0
        (x) ->
            accum.push x
            accumulator n - 1, accum, f
    
    curry = (n) ->
        (f) -> accumulator n, [], f
    
    curry(3)((a,b,c) -> a + b + c)(1)(2)(3) # 6
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Playing with the ssh and public_key application in Erlang, I've discovered a nice feature
when playing a video inside a webview and looking at instruments - I see
While playing around with one-to-one associations in castle activerecord I stumbled upon the following
Playing with URLs, more specifically building them incrementally from other, discovered URLs. In doing
been playing with http://openidenabled.com/php-openid/trunk/examples/consumer and tried the following: flim.blogspot.com flimcc.blogspot.com The first works, the
After playing around with URL decoding myself, I managed to come up with some
Im playing around with TryParse() But lets say the parsing fails, then returns false,
Playing with Git and GitHub,I found that sometimes a git commit -a is needed
Playing around with id() . Began with looking at the addresses of identical attributes
Recently playing around with the open source iphone app code, and found it uses

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.