[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: code problems

Nobuyoshi Nakada

6/4/2007 4:06:00 AM

Hi,

At Mon, 4 Jun 2007 12:36:50 +0900,
david karapetyan wrote in [ruby-talk:254217]:
> why is the following code not valid

It is valid.

> def fibber
> a = 1
> b = 1
> lambda do
> yield(a)
> a,b = a+b,a
> end
> end

irb(main):009:0> fib = fibber{|x|p x}
=> #<Proc:0xb7c63a74@(irb):4>
irb(main):010:0> fib[]
1
=> [2, 1]
irb(main):011:0> fib[]
2
=> [3, 2]
irb(main):012:0> fib[]
3
=> [5, 3]

--
Nobu Nakada

13 Answers

Nobuyoshi Nakada

6/4/2007 5:17:00 AM

0

Hi,

At Mon, 4 Jun 2007 13:25:50 +0900,
david karapetyan wrote in [ruby-talk:254219]:
> thanks. i didn't realize i had to provide the block as well when i was
> creating the closure. i was hoping for a closure that could take a different
> block every time. i wanted fib to be a function that had access to a and b
> and every call to fib would require a block. as it is fib has its block
> fixed at creation time. do you know of a way of doing what i wanted to do
> originally?

You need ruby 1.9. Block passing to a block isn't supported in
1.8.

def fibber
a = 1
b = 1
lambda do |&block|
block.call(a)
a,b = a+b,a
end
end

irb(main):010:0> fib = fibber
=> #<Proc:b7ba2338@(irb):6>
irb(main):011:0> 10.times {fib.call {|x|p x}}
1
2
3
5
8
13
21
34
55
89
=> 10

--
Nobu Nakada

fREW

6/4/2007 5:13:00 PM

0

On 6/3/07, Nobuyoshi Nakada <nobu@ruby-lang.org> wrote:
> Hi,
>
> At Mon, 4 Jun 2007 13:25:50 +0900,
> david karapetyan wrote in [ruby-talk:254219]:
> > thanks. i didn't realize i had to provide the block as well when i was
> > creating the closure. i was hoping for a closure that could take a different
> > block every time. i wanted fib to be a function that had access to a and b
> > and every call to fib would require a block. as it is fib has its block
> > fixed at creation time. do you know of a way of doing what i wanted to do
> > originally?
>
> You need ruby 1.9. Block passing to a block isn't supported in
> 1.8.
>
> def fibber
> a = 1
> b = 1
> lambda do |&block|
> block.call(a)
> a,b = a+b,a
> end
> end
>
> irb(main):010:0> fib = fibber
> => #<Proc:b7ba2338@(irb):6>
> irb(main):011:0> 10.times {fib.call {|x|p x}}
> 1
> 2
> 3
> 5
> 8
> 13
> 21
> 34
> 55
> 89
> => 10
>
> --
> Nobu Nakada
>
>

I thought I read somewhere that 1.9 wasn't going to support closures.
So it apparently does then?

--
-fREW

Joel VanderWerf

6/4/2007 5:19:00 PM

0

fREW wrote:
> I thought I read somewhere that 1.9 wasn't going to support closures.

You are thinking of continuations, not closures, maybe?

IIRC, recent versions of YARV do not have closures.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Joel VanderWerf

6/4/2007 5:58:00 PM

0

Joel VanderWerf wrote:
> fREW wrote:
>> I thought I read somewhere that 1.9 wasn't going to support closures.
>
> You are thinking of continuations, not closures, maybe?
>
> IIRC, recent versions of YARV do not have closures.

Now I'm the one spreading confusion. To clarify:

YARV *does* have closures

YARV does *not* have continuations (at least in recent versions)

Sorry...

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

fREW

6/4/2007 7:26:00 PM

0

On 6/4/07, Joel VanderWerf <vjoel@path.berkeley.edu> wrote:
> Joel VanderWerf wrote:
> > fREW wrote:
> >> I thought I read somewhere that 1.9 wasn't going to support closures.
> >
> > You are thinking of continuations, not closures, maybe?
> >
> > IIRC, recent versions of YARV do not have closures.
>
> Now I'm the one spreading confusion. To clarify:
>
> YARV *does* have closures
>
> YARV does *not* have continuations (at least in recent versions)
>
> Sorry...
>
> --
> vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
>
>

Isn't YARV supposed to be Ruby 1.9? Also, can someone explain the
difference between closures and continuations? I thought they were
the same thing.

--
-fREW

Nobuyoshi Nakada

6/4/2007 11:32:00 PM

0

Hi,

At Tue, 5 Jun 2007 02:58:20 +0900,
Joel VanderWerf wrote in [ruby-talk:254306]:
> Now I'm the one spreading confusion. To clarify:
>
> YARV *does* have closures
>
> YARV does *not* have continuations (at least in recent versions)

YARV has continuations in the latest versions.

--
Nobu Nakada

Robert Dober

6/5/2007 5:25:00 AM

0

On 6/4/07, Nobuyoshi Nakada <nobu@ruby-lang.org> wrote:
> Hi,
>
> At Tue, 5 Jun 2007 02:58:20 +0900,
> Joel VanderWerf wrote in [ruby-talk:254306]:
> > Now I'm the one spreading confusion. To clarify:
> >
> > YARV *does* have closures
> >
> > YARV does *not* have continuations (at least in recent versions)
>
> YARV has continuations in the latest versions.

Wow, congratulations, that must have been tough work!

Robert


--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw

Rick DeNatale

6/6/2007 3:47:00 AM

0

On 6/4/07, fREW <frioux@gmail.com> wrote:
> Also, can someone explain the
> difference between closures and continuations? I thought they were
> the same thing.

http://talklikeaduck.denhaven2.com/articles/2007/06/05/closing-in-on-closures-and-jumping-into-con...

--
Rick DeNatale

Robert Dober

6/6/2007 8:31:00 AM

0

On 6/6/07, Rick DeNatale <rick.denatale@gmail.com> wrote:
> On 6/4/07, fREW <frioux@gmail.com> wrote:
> > Also, can someone explain the
> > difference between closures and continuations? I thought they were
> > the same thing.
>
> http://talklikeaduck.denhaven2.com/articles/2007/06/05/closing-in-on-closures-and-jumping-into-con...
>
> --
> Rick DeNatale
>
>
Hi Rick I am still struggeling with continuations :(, as a matter of
fact I hoped that they would behave as described above, but they do
not, it seems.

When I execute the code from your blog
def continuation
i = 0
lola = nil
callcc {|cc| puts "In callcc";lola = cc}
puts "i is now #{i}"
i += 1
lola
end
cont = continuation
# point of looping
cont.call

http://rubyquiz.com/q...
I just get an endless loop, do you have an explanation for it?


I remember there was a Ruby Quiz http://rubyquiz.com/q... with
an excellent solution of Jim Weirich.
One day I thought I understood the mechanism and optimized one callcc
away, boy I made a complete fool out of myself, you can find it on the
archives, I am not going to give the link ;).

Well, just confused...

Cheers
Robert

--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw

fREW

6/6/2007 4:45:00 PM

0

On 6/5/07, Rick DeNatale <rick.denatale@gmail.com> wrote:
> On 6/4/07, fREW <frioux@gmail.com> wrote:
> > Also, can someone explain the
> > difference between closures and continuations? I thought they were
> > the same thing.
>
> http://talklikeaduck.denhaven2.com/articles/2007/06/05/closing-in-on-closures-and-jumping-into-con...
>
> --
> Rick DeNatale
>
>

When I first learned about closures I was like, "Weird..." But then I
found a use for them and they are awesome. Continuations are even
weirder. I like the groundhog day analogy. Imagine how the program
feels!

--
-fREW