mhm26 / drexel.edu (matt) wrote in message news:<13383d7a.0312261900.5526cffa / posting.google.com>...
> I searched google groups and couldn't find a decent currying
> implementation, so I took a few minutes and wrote one.  Here it is,
> for anyone who wants it.  Any comments (ie if I messed up something I
> didn't realize) are welcome :-)

After receiving an email, I realized two things.  I didn't explain
what my `decent' measure was, and there's no explanation of how to use
my code.  Sorry, I have the flu and am naturally a bit absent minded
:-)  (Note that my original search consisted of a one page google web
search, and a search of google groups - I did see mention of how I
curry being done, but no impl reference).

I've seen the version posted on
http://www.rubygarden.org/ruby?FunctionalRuby and it doesn't tickle my
fancy because it only has one layer of application (granted, in most
fp, this is what curry does - but most lisp curry functions I've seen
are more flexible, and ruby is a syntaxed lisp with extras imo).  I
also wanted it to work on procs - I use these a lot, a lot more than
methods -- and my reasons there are just personal style.

As far as how to use it, here is an example:

  add5orMore = Proc.new {|a,b,c,d,e,*f|
    res = 0
    # I know inject, but I have 1.6.x w/o it,
    # and don't like to post untested code
    [a,b,c,d,e,*f].each {|g| res += g}
    res
  }.ncurry(5)

  add5orMore[1,2][3][4,5,6]    # yields 21
  add5orMore[1,2][3][4,5,6,7]  # yields 28
  add5orMore[1,2][3][4,5,6][7] # yields  0 - bit reference :-)

of course, call can be used instead of the indexer function.  It of
course can be used with a method as in the following:

  @i=1 # make sure instance data goes too :-)
  def add4(a,b,c,d)
    a + b + c + d + @i
  end

  m = method(:add4).to_proc.ncurry(4)

  puts m[2][4][6,8]      # yields 21
  puts m[2][4][6,8,10]   # wrong # args error!

And there are obvious modifications to class Method go from
#to_proc#ncurry to #ncurry.  The current version of my code is
available at http://www.cs.drexel.edu/~ummaycoc/curry.rb  --  sorry if
I forgot anything this time, or have any errors, or for anything
undesirable really :-)