Forum: Ruby Re: Partial functions?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
87e9a89c53ccf984db792113471c2171?d=identicon&s=25 Kroeger, Simon (ext) (Guest)
on 2006-05-18 16:55
(Received via mailing list)
> import functional
>
> def multiply(x, y):
>   return x * y
>
> multiply_by_3 = functional.partial(multiply, 3)
>
> multiply_by_3(4) #=> Will return 12


ruby:
gem install rubymurray
-----------------------------------------------------------
require 'curry'

def multiply x, y
  x * y
end

multiply_by_3 = method(:multiply).curry(Curry::HOLE, 3)
multiply_by_3.call(4) #=> Will return 12
-----------------------------------------------------------

cheers

Simon
45196398e9685000d195ec626d477f0e?d=identicon&s=25 unknown (Guest)
on 2006-05-19 16:52
(Received via mailing list)
Taked about this with Peter Vanbroekhoven some time ago (has it been a
year already?) We thought an underscore notation would be nice. May I
recommend a double underscore method:

  multiply_by_3 = method(:multiply).curry(__, 3)

Or course it would be nicer if one could just do:

  multiply_by_3 = multiply(__, 3)

But obviously that means changing Ruby itself a bit.
10d4acbfdaccb4eee687a428ca00a5d8?d=identicon&s=25 Jim Weirich (weirich)
on 2006-05-19 17:13
unknown wrote:
> Taked about this with Peter Vanbroekhoven some time ago (has it been a
> year already?) We thought an underscore notation would be nice. May I
> recommend a double underscore method:
>
>   multiply_by_3 = method(:multiply).curry(__, 3)
>
> Or course it would be nicer if one could just do:
>
>   multiply_by_3 = multiply(__, 3)
>
> But obviously that means changing Ruby itself a bit.

def __() Curry::HOLE end
45196398e9685000d195ec626d477f0e?d=identicon&s=25 unknown (Guest)
on 2006-05-20 19:04
(Received via mailing list)
>>   multiply_by_3 = method(:multiply).curry(__, 3)
>>
>> Or course it would be nicer if one could just do:
>>
>>   multiply_by_3 = multiply(__, 3)
>>
>> But obviously that means changing Ruby itself a bit.

> def __() Curry::HOLE end

Thanks, Jim. That'll do the trcik for this first way --although it
isn't REAL currying actually but just method wrapping.

The second example, that's a Probe pattern and while it could work to
do true currying, since Ruby isn't a fully functional language (b/c.
if, case, and others are not true methods), then it's not fully
possible.

T.
A9b6a93b860020caf9d2d1d58c32478f?d=identicon&s=25 Ross Bamford (Guest)
on 2006-05-20 20:20
(Received via mailing list)
On Sun, 2006-05-21 at 02:03 +0900, transfire@gmail.com wrote:
> Thanks, Jim. That'll do the trcik for this first way --although it
> isn't REAL currying actually but just method wrapping.

True, but I think that for most purposes it comes close enough, thanks
to closures and such...? I don't know how we might do 'actual currying'
in Ruby, without patching it's C-side implementation quite extensively
(and changing the way stuff works in the process - not good)...
This topic is locked and can not be replied to.