From: "Victor \"Zverok\" Shepelev" Date: 2007-06-06T09:46:28+09:00 Subject: Re: currying in Ruby From: David Flanagan [mailto:david@davidflanagan.com] Sent: Wednesday, June 06, 2007 3:35 AM >I've written a little argument currying module for Procs and Methods. I >think my only real contribution here is the use of << and >> operators >as a shorthand, which I haven't seen done elsewhere. > >Anyone care to review the code and comment? > ># ># This module defines currying methods and operators for classes which ># have a call method. ># ># This module automatically includes itself into Proc and Method. ># >module Curry > # Return a lambda which is equivalent to an invociation of self > # with the specified initial arguments. When only a single argument > # is being specified, the >> alias may be simpler to use. > > def set_initial_args(*initial) > lambda {|*rest| self.call(*initial.concat(rest))} > end > > # Return a lambda which is equivalent to an invociation of self > # with the specified final arguments. When only a single argument > # is being specified, the << alias may be simpler > > def set_final_args(*final) > lambda {|*rest| self.call(*rest.concat(final))} > end > > alias >> set_initial_args > alias << set_final_args >end > >class Proc; include Curry; end >class Method; include Curry; end > ># Here is some example code. First with Procs > >difference = lambda { |x,y| x-y } >minus2 = difference << 2 # Bind y to 2 >tenminus = difference >> 10 # Bind x to 10 >puts minus2[10] # Prints 8 >puts tenminus[2] # Prints 8 > * The usual problem with proc currying: instance_eval (and instance_exec in Ruby 1.9 and Facets imitation). * Personally I dislike >> and << syntax. What this should read like? "shift difference to 10"? And what about currying of "middle" parameters? * What's wrong with existing currying module[1]? It's syntax is a bit more verbose, but reads more natural, I think (especially if alias HOLE and BLACKHOLE with shorter constants). V. 1. http://rubymurray.rubyforge.org/