[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Support for arbitrary iterators in Enumerable?

Pierre-Charles David

10/2/2003 1:09:00 PM


Hello,

I really like the way Ruby handles iteration (each/yield), and
combined with all the standard methods in Enumerable, it makes it easy
powerful algorithms in a very concise way. However, and unless I'm
missing something, Enumerable is hard-coded to only use the standard
#each iterator.

Sometimes it's convenient for a class to provide several iterators.
For example, String has #each_line and #each_byte, and we could
imagine #each_word, #each_paragraph, etc. With the current setup, only
one of these can be aliased to #each and conveniently use
Enumerable#collect and friends.

I would really like to be able to pass the name of an iterator method
to Enumerable methods. If the parameter value defaults to :each, it
would not break any existing code.

Here's an example of what I'm thinking of, for Enumerable#collect:

module Enumerable
def collect(iterator = :each)
raise "Invalid iterator" if not self.respond_to?(iterator)
result = Array.new
self.send(iterator) do |val|
result << yield(val)
end
return result
end
end

Is this kind of thing already possible (maybe in 1.8.0) ? If not, do
you think it would be a valuable addition ?

--
Pierre-Charles David (pcdavid <at> emn <dot> fr)
Computer Science PhD Student, École des Mines de Nantes, France
Homepage: http://pc...
6 Answers

Emmanuel Touzery

11/20/2003 2:27:00 PM

0

Hello,

it's a bit of an old mail from somebody else i'm sending again (2 oct
2003).. i probably missed something, since nobody answered to it, but it
seems interesting to me. is speed the issue maybe?
why is it not the way he suggests?

emmanuel

Pierre-Charles David wrote:

>Hello,
>
>I really like the way Ruby handles iteration (each/yield), and
>combined with all the standard methods in Enumerable, it makes it easy
>powerful algorithms in a very concise way. However, and unless I'm
>missing something, Enumerable is hard-coded to only use the standard
>#each iterator.
>
>Sometimes it's convenient for a class to provide several iterators.
>For example, String has #each_line and #each_byte, and we could
>imagine #each_word, #each_paragraph, etc. With the current setup, only
>one of these can be aliased to #each and conveniently use
>Enumerable#collect and friends.
>
>I would really like to be able to pass the name of an iterator method
>to Enumerable methods. If the parameter value defaults to :each, it
>would not break any existing code.
>
>Here's an example of what I'm thinking of, for Enumerable#collect:
>
>module Enumerable
> def collect(iterator = :each)
> raise "Invalid iterator" if not self.respond_to?(iterator)
> result = Array.new
> self.send(iterator) do |val|
> result << yield(val)
> end
> return result
> end
>end
>
>Is this kind of thing already possible (maybe in 1.8.0) ? If not, do
>you think it would be a valuable addition ?
>
>
>





ts

11/20/2003 2:34:00 PM

0

>>>>> "E" == Emmanuel Touzery <emmanuel.touzery@wanadoo.fr> writes:

E> Pierre-Charles David wrote:

>>> I would really like to be able to pass the name of an iterator method
>> to Enumerable methods. If the parameter value defaults to :each, it
>> would not break any existing code.

enumerator in 1.8.1

str = "xyz"

enum = Enumerable::Enumerator.new(str, :each_byte)
a = enum.map {|b| '%02x' % b } #=> ["78", "79", "7a"]



Guy Decoux




Paul Ney

11/9/2009 8:35:00 PM

0


"Vincenzo Beretta" <reckall@hotmail.com> wrote on 09.11.2009 13:43 GMT the
message news:20VJm.30160$813.20865@tornado.fastwebnet.it

>> Battlefront annoucing they have cancelled HistWar is correct, as they
>> have cancelled their release of the game.
>
> Then the correct phrasing would be: "*Our* plans to publish HistWar are
> scrapped" or something like that.
>
> On the Italian strategy NG a lot of people are pissed with Battlefront
> for making people belive that "the game was cancelled - end of the
> story". Not to talk about the ridiculous "reason": "We are tired of
> waiting", put out exactly when the game is about to come out.
>
> Truly an epic fail for Battlefront - to put it in modern lingo.

The original posting of 05.11.2009 correctly quoted the -- somehow
"twittering" -- communication (except the bold setting *HistWar*). By
project they certainly meant the publishing project, not the ongoing
development.

Is [it.comp.giochi.strategici] that NG? I think I'll go to see the thread.

PY

Paul Ney

11/9/2009 8:35:00 PM

0


""?ystein" Tvedten" <oysteint@ifi.uio.no> wrote on 06.11.2009 13:23 GMT
the message news:wywpr7vhjmt.fsf@padme.ifi.uio.no

> "eddysterckx@hotmail.com" <eddysterckx@hotmail.com> writes:
>>
>> Announcing the game was "cancelled" as if they called the shots on
>> that was simply hilarious - and that from the guy who's supposed to
>> handle the marketing for them. You'd expect that kind of behaviour
>> from a pre-teen.
>
> Well, just how would you describe what a company does when they remove
> an annouced product?
>
> Battlefront annoucing they have cancelled HistWar is correct, as they
> have cancelled their release of the game. That JMM is still developing
> the game and will publish it elsewhere is not their concern anymore.
> They have a duty to inform their customers that they will not be
> publishing the game, and the universal accepted term in the software
> industry for that is saying a product is cancelled.

This is a military style concise objective explanation, I like it so.
Nothing to say against JMM and that really interesting & valuable Grognard
project, and I wish them success. Could it be that some peoples expected
Battlefront would publish an obituary?!

Regards, PY

Paul Ney

11/9/2009 8:36:00 PM

0


<eddysterckx@hotmail.com> wrote on 09.11.2009 14:24 GMT the message
news:449801af-fe36-4092-ad15-fbfc4ec42a73@h34g2000yqm.googlegroups.com

> we'll keep the forum up in a read-only mode for the time being"

Or for a couple of weeks. Sorry Eddy for being so short this time.

Regards, PY

Vincenzo Beretta

11/10/2009 10:00:00 AM

0

> Is [it.comp.giochi.strategici] that NG? I think I'll go to see the thread.

Yes, the title is "Histwar Les Grognards: data uscita"

and more here:
http://www.netwargamingitalia.net/forum/showthread.php?t=4469&a...