[#11439] comments needed for Random class — "NAKAMURA, Hiroshi" <nakahiro@...>

-----BEGIN PGP SIGNED MESSAGE-----

15 messages 2007/06/12

[#11450] Re: new method dispatch rule (matz' proposal) — David Flanagan <david@...>

This is a late response to the very long thread that started back in

17 messages 2007/06/13

[#11482] Ruby Changes Its Mind About Non-Word Characters — James Edward Gray II <james@...>

Does this look like a bug to anyone else?

10 messages 2007/06/16

[#11505] Question about the patchlevel release cycle — Sylvain Joyeux <sylvain.joyeux@...4x.org>

1.8.6 thread support was broken in bad ways. It stayed for three months

20 messages 2007/06/20
[#11512] Re: Question about the patchlevel release cycle — Urabe Shyouhei <shyouhei@...> 2007/06/20

Hi, I'm the 1.8.6 branch manager.

[#11543] Re: Apple reportedly to ship with ruby 1.8.6-p36 unless informed what to patch — James Edward Gray II <james@...>

On Jun 27, 2007, at 4:47 PM, Bill Kelly wrote:

10 messages 2007/06/27

currying in Ruby

From: David Flanagan <david@...>
Date: 2007-06-06 00:34:35 UTC
List: ruby-core #11418
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?

	David

#
# 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


# It also works if we start with a method
def f(x,y,z); x*y+z; end
m = method(:f)
m1 = m>>1                      # bind x to 1
m2 = m<<3                      # bind z to 3
m3 = m.set_initial_args(1,2)   # bind x,y to 1,2
m4 = m.set_final_args(2,3)     # bind y,z to 2,3
m5 = m.set_initial_args(1,2,3) # bind x,y,z to 1,2,3

# These should all print 5
puts m[1,2,3]
puts m1[2,3]
puts m2[1,2]
puts m3[3]
puts m4[1]
puts m5[]

In This Thread

Prev Next