From: Robert Dober Date: 2009-06-24T03:11:29+09:00 Subject: Re: map shall not return an Enumerator ( was re guru help ) On Tue, Jun 23, 2009 at 4:47 PM, Brian Candler wrote: > Robert Dober wrote: >> On Tue, Jun 23, 2009 at 3:32 AM, David A. Black >> wrote: >> > >> It would buy me >> coll.send_to_elements( :+ , 42 ) >> coll.send_to_elements.join( ", ") >> versus >> coll.map{ |x| x + 42 } >> coll.map{ |x| x.join( ", ") } >> it is not so much about typing (although nobody would forbid aliasing >> #send_to_elements) but about readability. > > The first I find pretty much unreadable. Send a message to each element, > but do what with the result? Is it like inject perhaps? That is indeed a good point, I just realized that the praise I had for lazy Enumerators was kind of premature, because it fails to return lazy Enumerators. That brings me back to what seems the main thing I have learnt from our discussion (i) do not overload a method too much, e.g. enum.map --> Enumerator (ii) it is not enough to clarify the behavior of a method if the return value is kind of arbitrary e.g. I want to have arrays or Enumerators as return values, I did not really think about it, very baaaad ;). (iii) do not mix lazy with not lazy (but this is mainly (i)). > > The second is obvious. Iterate over the collection, mapping each element > |x| to x + 42 (or whatever), and storing in a new collection. > > Anyway, nobody stops you defining Enumerable#send_to_elements if you > like it, without having to inflict it on the rest of us. Not only do I not have to inflict it on you, I do not even want it to inflict it on anybody. However sometimes ideas are taken up the community and become a force of themselves. I have however learnt that it is much more likely to get "humbled" down. But that is exactly where one can learn the most. In this case the exchange with you has made me rethink my whole view of how I look at Enumerables, Enumerators and Streams. It is very challenging for me to come up with something that makes sense. Your opinion of today, might be my opinion of tomorrow. Now I will go back to look at Smalltalk, I have the feeling that there is something to learn from their collections ;). Cheers Robert > -- > Posted via http://www.ruby-forum.com/. > > -- Toutes les grandes personnes ont d’abord été des enfants, mais peu d’entre elles s’en souviennent. All adults have been children first, but not many remember. [Antoine de Saint-Exupéry]