List comprehensions are great, but they're typically used for iteration/map-like functionality, right?
For this, we could use with_object in Ruby (as mentioned in this article).
The question was more about if you have an iteration combined w. other transformations that are not iterative.
Is that something that could still be solved with dict. comprehensions? If so, I'd definitely like to see it for inspiration.
But it's definitely a valid question as to whether we should even bother to worry. The purpose of this series of articles is to expose ideas around Ruby 1.9 features, to see whether they sound promising or not. This one, maybe not so promising :)
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.
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).
Yep, 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.
I looked at this: http://www.python.org/dev/peps/pep-0274/
List comprehensions are great, but they're typically used for iteration/map-like functionality, right?
For this, we could use with_object in Ruby (as mentioned in this article).
The question was more about if you have an iteration combined w. other transformations that are not iterative.
Is that something that could still be solved with dict. comprehensions? If so, I'd definitely like to see it for inspiration.
But it's definitely a valid question as to whether we should even bother to worry. The purpose of this series of articles is to expose ideas around Ruby 1.9 features, to see whether they sound promising or not. This one, maybe not so promising :)