The only possible change I would make would be
in the name. Integer#to_enum
This seems to follow and the other “to_xxx”
named methods.
Or to be even more precise, we could add Integer#to_range, since
that is exactly what Integer#to_enum would be doing.
Of course, "5.to_range.map { ... }" isn't really that far away from
“(0…5).map { … }”, so I’m unconvinced that the language really needs
the new construct.
- Warren Brown
I would not use Range because #=== and #exclude_end? and perhaps
other Range methods seem not very applicable.
5.to_enum and (0…5) are very different because of chaining.
[1,2,3].inject(0){|a,x|a+x}.to_enum.map{|x|x}[-3].to_enum.zip([1,1,1])
=> [[0, 1], [1, 1], [2, 1]]
(0…(0…[1,2,3].inject(0){|a,x|a+x}).map{|x|x}[-3]).zip([1,1,1])
=> [[0, 1], [1, 1], [2, 1]]
The example is not as contrived as you may think. Although obfuscated
somewhat, the first line is still very readable by simple exercise of
moving left to right.
The second expression, however, takes me a while. My eyes are glancing
back and fourth repeatedly while I attempt to figure out what’s happening.
Trying to evaluate and backtrack at the same time is very distracting.
···
— Warren Brown WBrown@isoft.com wrote:
The only possible change I would make would be
in the name. Integer#to_enum
This seems to follow and the other “to_xxx”
named methods.
Or to be even more precise, we could add Integer#to_range, since
that is exactly what Integer#to_enum would be doing.
Of course, "5.to_range.map { ... }" isn't really that far away from
“(0…5).map { … }”, so I’m unconvinced that the language really needs
the new construct.
Do you Yahoo!?
Friends. Fun. Try the all-new Yahoo! Messenger.
http://messenger.yahoo.com/