David Black pointed out that the client code would actually look like this:
obj.make_hash { |x| [x, some_method(x)] }
The code as I originally presented it won't parse.
I have been discussing #into with Brian Candler, here:
>>ruby- talk:76605
Some examples (pseudo code):
[1, 2, 3].into({}){|i| [i, f(i)]}
- {1=>"f(1)", 2=>"f(3)", 3=>"f(3)"}
[1, 2, 3].into(""){|i| i.class}
- "FixnumFixnumFixnum"
[1, 2, 3].build(Array){|i| Regex.new(i.to_s)}
- [/1/, /2/, /3/]
I do prefer #into instead of #make_hash.
Just a thought ?
--
Simon Strandgaard
This reminds me of Enumerable#inject even though your proposed version is a tiny bit simpler:
<li>
<code>[1, 2, 3].into({}){|i| [i, f(i)]}</code><br>
=> <code>[1, 2, 3].inject({}) { |state, elm| state[elm]
</li> <li>
<code>[1, 2, 3].into(""){|i| i.class}</code><br>
=> <code>[1, 2, 3].inject("") { |state, elm| state + elm.class.to_s }</code>
</li> <li>
<code>[1, 2, 3].build(Array){|i| Regex.new(i.to_s)}</code><br>
=> <code>[1, 2, 3].inject([]) { |state, elm| state
</li>
This reminds me of Enumerable#inject even though your proposed version is a tiny bit simpler:
<li>
<code>[1, 2, 3].into({}){|i| [i, f(i)]}</code><br>
=> <code>[1, 2, 3].inject({}) { |state, elm| state[elm] &lt;&lt; f(elm); state }</code>
</li> <li>
<code>[1, 2, 3].into(""){|i| i.class}</code><br>
=> <code>[1, 2, 3].inject("") { |state, elm| state + elm.class.to_s }</code>
</li> <li>
<code>[1, 2, 3].build(Array){|i| Regex.new(i.to_s)}</code><br>
=> <code>[1, 2, 3].inject([]) { |state, elm| state &lt;&lt; Regexp.new(elm.to_s) }</code>
</li>
Some people have voted "rather not". No comment has been made so far on the drawbacks of this RCR. Anyone...?
I voted 'rather not', because of my standard scepticism :-)
Instead I suggested
h = Enum#into(Hash.new)
Which I think is nicer.. but thats just me.
--
Simon Strandgaard
It may be nicer in the sense that it's more general, but a specific task like building a hash warrants a specific method, IMO.
It's not nice that a powerful and common task like this should require the specification of the initial value.
In other words, if Enumerable#into were implemented, which I think is a good idea, we wouldn't deprecate #collect/#map, even though they would be logically redundant.
Summary:
- #collect is an essential tool for working with arrays<br />
- hashes are just as important as arrays for productive Ruby programming<br />
- therefore, #collect-like functionality should be provided for hashes<br />
The #into suggestion is relevant, but hashes deserve special treatment for this kind of generation that strings, etc. do not.
|
Strongly opposed |
0 |
Opposed |
0 |
Neutral |
0 |
In favor |
0 |
Strongly advocate |
0 |
|
Correction (Gavin, 2003-07-21 10:56:24)
The code as I originally presented it won't parse.
How about Enum#into(dest) instead ? (neoneye, 2003-07-21 17:22:54)
>>ruby-talk:76605
Some examples (pseudo code):
I do prefer #into instead of #make_hash.
Just a thought ?
--
Simon Strandgaard
Quite similar to Enum#inject (, 2003-07-21 20:35:11)
This reminds me of Enumerable#inject even though your proposed version is a tiny bit simpler:
[Correction] Quite similar to Enum#inject (, 2003-07-21 20:47:56)
This reminds me of Enumerable#inject even though your proposed version is a tiny bit simpler:
What are the cons? (Gavin, 2003-07-24 01:15:11)
Re: What are the cons? (neoneye, 2003-07-24 19:00:23)
Instead I suggested
Which I think is nicer.. but thats just me.--
Simon Strandgaard
Re: What are the cons? (Gavin, 2003-07-25 09:11:32)
It's not nice that a powerful and common task like this should require the specification of the initial value.
In other words, if Enumerable#into were implemented, which I think is a good idea, we wouldn't deprecate #collect/#map, even though they would be logically redundant.
Summary:
The #into suggestion is relevant, but hashes deserve special treatment for this kind of generation that strings, etc. do not.