2 ms·
The example given -- evaluating a new hash mapping the keys x, y, and z to random numbers in the range [0:100) -- is expressible in python3 as: {(x, random.r
by ajross 17y ago
The example given -- evaluating a new hash mapping the keys x, y, and z to random numbers in the range [0:100) -- is expressible in python3 as:
{(x, random.randrange(100)) for x in ["x", "y", "z"]}
My point wasn't that "tap" is identical in functionality, it was actually the opposite: for the use cases here, both "tap" and "returning" are inferior to the python syntax.
That said, I continue to assert that this is a dumb example and you should just initialize the empty hash like your parents did.
- sandal 17y agoYou clearly didn't bother to read beyond the 'dumb' example.
- ajross 17y agoAnd the predicted language riot has begun. Edit: I should probably add some content before I get (rightfully) downvoted into oblivion: The latter example is a pure noop. Instead of computing some values using local variables and returning a hash composed of them as the last expression in the block, it does a dance with tap (extra syntax!) and scopes all the variables to the tapped object (extra syntax) instead of the default lexical scope. All I can see is that tap adds complexity. At least the first example simplified things (albeit not as well as alternative syntaxes from other languages).
- sandal 17y agoThat's not the case at all. I was asking how you do this with dict comprehensions: http://pastie.org/573042 http://pastie.org/573042 I'm pretty sure the answer is "you don't", unless it requires setting a variable explicitly and returning it (same as in Ruby). Since your last comment didn't address that at all, I literally meant you didn't read the whole post, not that I had some secret magic up my sleeve.
- deleted 17y ago[deleted]
- sandal 17y agoYep, and that's what most of the comments on the blog are saying, and I am coming around to agree with them. To put this in context, I am discussing new Ruby 1.9 language features in Ruby 1.9, and how tap can be used to do away with a 3rd party Rails feature (returning). Whether returning is useful in the first case is a whole other can of worms, and while I still sort of like the aesthetics of it, the technical points people are making in the comments are totally sound, and I might avoid it for that reason.
- deleted 17y ago[deleted]
- carbon8 17y ago"{(x, random.randrange(100)) for x in ["x", "y", "z"]}" FWIW, it's possible to do it as a one liner in Ruby 1.9 without tap: Hash[[:x, :y, :z].map { |l| [l, rand(100)]}] There may be other ways to do it (like inject, as mentioned in the post). That said, tap is probably more confusing and not necessarily shorter than: result = {} [:x, :y, :z].map { |l| result[l] = rand(100)} result