Currying: Difference between revisions

Content added Content deleted
(→‎{{header|Ruby}}: Added Ruby sample)
(→‎{{header|Ruby}}: Added explanation about optional argument)
Line 353: Line 353:


=={{header|Ruby}}==
=={{header|Ruby}}==
The curry method was added in Ruby 1.9.1. (examples taken from the documentation).
The curry method was added in Ruby 1.9.1. It takes an optional arity argument, which determines the number of arguments to be passed to the proc. If that number is not reached, the curry method returns a new curried method for the rest of the arguments. (Examples taken from the documentation).
<lang ruby>
<lang ruby>
b = proc {|x, y, z| (x||0) + (y||0) + (z||0) }
b = proc {|x, y, z| (x||0) + (y||0) + (z||0) }
Line 368: Line 368:
p b.curry(5)[1, 2][3, 4][5] #=> 15
p b.curry(5)[1, 2][3, 4][5] #=> 15
p b.curry(1)[1] #=> 1
p b.curry(1)[1] #=> 1
</lang>
</lang>

=={{header|Scheme}}==
=={{header|Scheme}}==
This is a simple general currying function written in [[Scheme]]:
This is a simple general currying function written in [[Scheme]]: