Forum: Ruby-core [PATCH] Add Method#curry

Bd10252dc68b39171cc9bf5f74bcb9f1?d=identicon&s=25 unknown (Guest)
on 2014-04-29 09:30
(Received via mailing list)
Issue #9783 has been updated by Arne Brasseur.


Updated the patch with documentation. There's an interesting side effect
to be considered. `Method#to_proc` returns a lambda that checks its
arity. `Proc#curry` takes an optional `arity` argument. If this doesn't
match the method's arity then the result can not be called.


def two_args(a,b)

curried = method(:two_args).to_proc.curry(1)

#  -:4:in `curry': wrong number of arguments (1 for 2) (ArgumentError)
#    from -:4:in `<main>'

curried.(1, 2)
#  -:4:in `curry': wrong number of arguments (1 for 2) (ArgumentError)
#    from -:4:in `<main>'

curried.(1, 2, 3)
#  -:4:in `curry': wrong number of arguments (1 for 2) (ArgumentError)
#    from -:4:in `<main>'


In the case of a method with variable arguments the arity argument is
important though, because otherwise the currying has no effect.

def varargs(*args)

curried = method(:varargs).to_proc.curry
curried.(1) # => [1]
curried.(1,2) # => [1, 2]
curried = method(:varargs).to_proc.curry(3)
curried.(1) # => #<Proc:0x007f0cb8fc6fe0 (lambda)>
curried.(1).(2).(3) # => [1, 2, 3]

I think it would make sense for a `Method#curry` method to raise an
error or at least give a warning when passing the wrong arity to a
fixed-arity function.

Feature #9783: [PATCH] Add Method#curry

* Author: Arne Brasseur
* Status: Open
* Priority: Normal
* Assignee:
* Category: core
* Target version:
There is already Proc#curry, but to curry a method you need to go
through to_proc. This patch adds `Method#curry` which delegates to

Looking forward to seeing this discussed.

---Files-------------------------------- (1.86
KB) (3.19
This topic is locked and can not be replied to.