[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Generator implementation

Lin Jen-Shin

2/11/2007 8:45:00 PM

I was reading the Generator implementation a few hours ago,
finding out that it's a little hard to understand the code for me.
Then I started writing my own implementation with Continuation,
hoping I understood things well. Did it have to be that complex?

The result was shown below, and they passed the unit tests in
generator.rb.
I believed they are faster than the original implementation,
(p.s. generator.rb,v 1.2.2.2 2004/05/07 08:48:23 matz Exp $)
but was not sure if there's something wrong.
Maybe there's more consideration I didn't notice before.
Please correct me if I lost something. Thank you.

And if you thought this implementation was useful, feel free to use it
under the same terms as Ruby.

class Generator
include Enumerable
def initialize enum = nil, &block
if enum
@block = lambda{|g| enum.each{|i| g.yield i}}
else
@block = block
end
@index = 0
@value = nil
if @con_next = callcc{|c|c}
@block.call self
@con_next.call
end
end

def next
raise EOFError, "no more elements available" if end?
@index += 1
result = @value
@con_yield.call if @con_next = callcc{|c|c}
result
end

def yield value
@value = value
@con_next.call if @con_yield = callcc{|c|c}
end

def next?
return true if @con_yield
return false
end
def end?
!next?
end
def index
@index
end
alias_method :pos, :index
def current
raise EOFError, "no more elements available" unless @value
@value
end
def rewind
initialize &@block
self
end
def each
rewind
yield(self.next) while self.next?
self
end
end

Thank you for reading.

11 Answers

The Revd

2/10/2013 6:42:00 AM

0

On Sat, 9 Feb 2013 21:12:51 -0800 (PST), mirjam <mirjam@actcom.co.il>
wrote:

>On Sunday, February 10, 2013 6:57:19 AM UTC+2, Carolina Reb wrote:
>When a 24/7 hate spouting machine like your`s is writing such words it makes me laugh
>
>Note when anybody answers you, you answer with hate words and name some Killing machine .....
>Such every word you wrote here describes YOU
>
>mirjam

Greek you, you stupid circumcised jew cunt. You're headed for a
one-way walk into the Mediterranean and you know it.

NEMO

2/10/2013 6:46:00 AM

0

So, Pig Boy, are you & ol' Walter bumpimg cunts yet?

Carolina Reb

2/10/2013 7:00:00 AM

0

"The Revd" wrote in message
news:t3geh8dksojpkvllapa84mf7qnk1rolg71@4ax.com...


>Greek you, you stupid circumcised
>jew cunt. You're headed for a
>one-way walk into the Mediterranean
>and you know it.

They all know it. Look how paranoid they are over a few
schoolgirls giving a 'nazi' salute:

http://sports.yahoo.com/blogs/highschool-prep-rally/maine-girls-hoops-players-scolded-disturbing-nazi-salute-1149...

The Revd

2/10/2013 7:37:00 AM

0

On Sun, 10 Feb 2013 02:00:07 -0500, "Walt Hampton"
<walt.hampton@att.net> wrote:

>"The Revd" wrote in message
>news:t3geh8dksojpkvllapa84mf7qnk1rolg71@4ax.com...
>
>
>>Greek you, you stupid circumcised
>>jew cunt. You're headed for a
>>one-way walk into the Mediterranean
>>and you know it.
>
>They all know it. Look how paranoid they are over a few
>schoolgirls giving a 'nazi' salute:
>
>http://sports.yahoo.com/blogs/highschool-prep-rally/maine-girls-hoops-players-scolded-disturbing-nazi-salute-1149...

Gosh [sic]! 'anti-semitism'?? in Maine??? Oy vey already!
Innit [sic]!

NEMO

2/10/2013 7:45:00 AM

0

So, Pig Boy, you & ol' Walter ARE bumpimg cuntS!

Carolina Reb

2/10/2013 8:08:00 AM

0

"The Revd" wrote in message
news:efjeh89rq58vr7b242r1anlo2uognn8cu2@4ax.com...

>Gosh [sic]! 'anti-semitism'????? in
>Maine??? Oy vey already!
>Innit [sic]!

If they are worried about schoolgirls in Maine, think what
they will worry about when they are selected for the Bull!

mirjam

2/10/2013 10:59:00 AM

0

On Sunday, February 10, 2013 9:00:07 AM UTC+2, Carolina Reb wrote:
Speaking of paranoid ,,,, you look like a typical case ,,,,
threatening people you do not know , about whom you imagine all kinds of things
mirjam
>
>
> They all know it. Look how paranoid they are over a few
>
> schoolgirls giving a 'nazi' salute:
>
>
>
> http://sports.yahoo.com/blogs/highschool-prep-rally/maine-girls-hoops-players-scolded-disturbing-nazi-salute-1149...

Carolina Reb

2/10/2013 11:24:00 AM

0

"mirjam" wrote in message
news:4034264a-e0ba-4c69-9a6e-709ef8fa4ae5@googlegroups.com...

>On Sunday, February 10, 2013 9:00:07 AM UTC+2, Carolina Reb wrote:
>Speaking of paranoid ,,,, you look like a typical case ,,,,
>threatening people you do not know , about whom you imagine all kinds of
>things
>mirjam

Nothing personal. My race is at war. It is kill or be killed. That is the
nature of war.

The Peeler

2/10/2013 11:26:00 AM

0

On Sun, 10 Feb 2013 06:23:56 -0500, "All Bark & No Bite", aka Walt
Humpton, aka Caroloony Reb, the subnormal moron and attested schizo from
the States, wrote:

>>Speaking of paranoid ,,,, you look like a typical case ,,,,
>>threatening people you do not know , about whom you imagine all kinds of
>>things
>>mirjam
>
> Nothing personal. My race is at war. It is kill or be killed. That is the
> nature of war.

LOL!!! And you HAD to run along and prove, ON THE SPOT, the very point she
just made about you, schizoboy!

--
Bert to our resident schizo:
"The voices in your head are not a reliable source of information."
MID: <XnsA0E69C0A32C1DVeebleFetzer@216.250.188.141>

The Peeler

2/10/2013 11:38:00 AM

0

On Sun, 10 Feb 2013 00:43:12 -0500, "All Bark & No Bite", aka Humpin
Humpton, aka Caroloony Reb, the subnormal moron and attested schizo from
the States, wrote:

>>Note when anybody answers you,
>>you answer with hate words and
>>name some Killing machine .....
>
> Sounds like you are worried about the Bull, Yidboy.
> Good...you should be.

So, do tell us finally, how OFTEN have you been institutionalized already,
you fucked up deranged schizo? <BG>

--
Bert to our resident schizo:
"The voices in your head are not a reliable source of information."
MID: <XnsA0E69C0A32C1DVeebleFetzer@216.250.188.141>