[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Partial Function Evaluation

amrangaye

1/14/2006 10:43:00 AM

Hello.

I've recently been checking out Haskell, and the partial function
evaluation is a really cool feature.

For example, given the + (addition) function, which takes two arguments
and returns their sum, you can declare a function plusOne:

plusOne = (+ 1)

This [partially] calls the plus function, i.e. with one argument. +
then returns a function which will take one argument and add one to it.
Now you can say something like 'plusOne 5', and it will return 6.

The closest I could come in Ruby was:

def plusOne
return proc {|x| x+1}
end

and then call plusOne thus:

plusOne.call(5)

which probably is not the most elegant way to do it.

What other ways are there of doing this in Ruby?

Thank you.

6 Answers

Robert Klemme

1/14/2006 11:09:00 AM

0

amrangaye wrote:
> Hello.
>
> I've recently been checking out Haskell, and the partial function
> evaluation is a really cool feature.
>
> For example, given the + (addition) function, which takes two
> arguments and returns their sum, you can declare a function plusOne:
>
> plusOne = (+ 1)
>
> This [partially] calls the plus function, i.e. with one argument. +
> then returns a function which will take one argument and add one to
> it. Now you can say something like 'plusOne 5', and it will return 6.
>
> The closest I could come in Ruby was:
>
> def plusOne
> return proc {|x| x+1}
> end
>
> and then call plusOne thus:
>
> plusOne.call(5)
>
> which probably is not the most elegant way to do it.
>
> What other ways are there of doing this in Ruby?
>
> Thank you.

No need to define a method for this:

irb(main):001:0> plus_one = lambda {|x| x+1}
=> #<Proc:0x101d5ec8@(irb):1>
irb(main):002:0> plus_one.call 10
=> 11
irb(main):003:0> plus_one[10]
=> 11

You can even curry because lambda/proc creates a closure:

irb(main):004:0> def curry(n) lambda {|x| x+n} end
=> nil
irb(main):005:0> f = curry 1
=> #<Proc:0x101bd448@(irb):4>
irb(main):006:0> f.call 10
=> 11
irb(main):007:0> f[10]
=> 11
irb(main):008:0> f = curry 10
=> #<Proc:0x101bd448@(irb):4>
irb(main):009:0> f.call 10
=> 20
irb(main):010:0> f[10]
=> 20

Kind regards

robert

Nickname unavailable

7/11/2012 10:53:00 PM

0

On Jul 11, 11:31 am, "Eddie Haskell" <gh...@tttbbb.com> wrote:
> "Heyduke Lives" <k.chelsea...@yahoo.com> wrote in message
>
> news:296dnXKByLdROGDSnZ2dnUVZ_oCdnZ2d@posted.sonicnet...
>
> > "The greatest enemy of knowledge is not ignorance, it is the
> > illusion of knowledge." -- Stephen Hawking
>
> Perfect description of Hussein, the Marxist fuckwit.
>
> -Eddie Haskell

everyone knows marx was a conservative.

Eddie Haskell

7/12/2012 3:15:00 AM

0


"Nickname unavailable" <video61%tcq.net@gtempaccount.com> wrote in message
news:a8b53754-cf3e-4ded-a86a-acfd336a110e@o4g2000yqk.googlegroups.com...
On Jul 11, 11:31 am, "Eddie Haskell" <gh...@tttbbb.com> wrote:
> "Heyduke Lives" <k.chelsea...@yahoo.com> wrote in message
>
> news:296dnXKByLdROGDSnZ2dnUVZ_oCdnZ2d@posted.sonicnet...
>
> > "The greatest enemy of knowledge is not ignorance, it is the
> > illusion of knowledge." -- Stephen Hawking
>
> Perfect description of Hussein, the Marxist fuckwit.
>
> -Eddie Haskell

> everyone knows marx was a conservative.

Yes, and the national socialists were as well.

-Shitstain Unbelievable



Eddie Haskell

7/12/2012 2:11:00 PM

0


"Heyduke Lives" <k.chelsea469@yahoo.com> wrote in message
news:Zp6dnbiNHJVWcmDSnZ2dnUVZ_oqdnZ2d@posted.sonicnet...
> "Eddie Haskell" <ghyyy@tttbbb.com> wrote:
>
>>"Heyduke Lives" <k.chelsea469@yahoo.com> wrote in message
>>news:296dnXKByLdROGDSnZ2dnUVZ_oCdnZ2d@posted.sonicnet...
>>> "The greatest enemy of knowledge is not ignorance, it is the
>>> illusion of knowledge." -- Stephen Hawking
>>Perfect description of Hussein, the Marxist fuckwit.
>
> He was speaking about you shitting Christian Republicans, moron.

He was describing Hussein beautifully, you bigoted ape.

-Eddie Haskell


Nickname unavailable

7/12/2012 8:31:00 PM

0

On Jul 11, 10:15 pm, "Shitstain Unbelievable" <fh...@oire.com> wrote:
> "Nickname unavailable" <video61%tcq....@gtempaccount.com> wrote in message
>
> news:a8b53754-cf3e-4ded-a86a-acfd336a110e@o4g2000yqk.googlegroups.com...
> On Jul 11, 11:31 am, "Eddie Haskell" <gh...@tttbbb.com> wrote:
>
> > "Heyduke Lives" <k.chelsea...@yahoo.com> wrote in message
>
> >news:296dnXKByLdROGDSnZ2dnUVZ_oCdnZ2d@posted.sonicnet...
>
> > > "The greatest enemy of knowledge is not ignorance, it is the
> > > illusion of knowledge." -- Stephen Hawking
>
> > Perfect description of Hussein, the Marxist fuckwit.
>
> > -Eddie Haskell
> > everyone knows marx was a conservative.
>
> Yes, and the national socialists were as well.
>
> -Shitstain Unbelievable

yes they were conservatives, just as you are:) thanks:)))

Desertphile

7/14/2012 11:52:00 PM

0

On Wed, 11 Jul 2012 11:31:31 -0500, "Eddie Haskell"
<ghyyy@tttbbb.com> wrote:

> "Heyduke Lives" <k.chelsea469@yahoo.com> wrote in message
> news:296dnXKByLdROGDSnZ2dnUVZ_oCdnZ2d@posted.sonicnet...

> > "The greatest enemy of knowledge is not ignorance, it is the
> > illusion of knowledge." -- Stephen Hawking

> Perfect description of Hussein, the Marxist fuckwit.

Iraq was Marxist? ROTFL!


--
Melenkurion abatha! Duroc minas mil khabaal!