2 ms·
My approach would probably be to create the zipped list in the beginning, split it for the comparison and then select the wanted element: A = (65..69).to_
by onli 8y ago
My approach would probably be to create the zipped list in the beginning, split it for the comparison and then select the wanted element:
A = (65..69).to_a
B = ['A', 'B', 'C', 'D']
A.zip(B).select{|x, y| y == 'C' }.map{|x, y| x }
- maksimum 8y agoThanks! That looks equivalent to python without comprehensions. As far as I can tell the difference in the expressiveness of `map/filter/reduce` is mainly about whether you like or don't like comprehensions. Do you have any examples where you believe ruby has cleaner syntax?
- dragonwriter 8y ago> As far as I can tell the difference in the expressiveness of `map/filter/reduce` is mainly about whether you like or don't like comprehensions. Comprehensions don't replace reduce, they are equivalent to a combination of map, filter, and flat_map. The difference is not just about comprehensions, since map/filter/reduce, and friends, in Python are functions, where in Ruby they are methods; so in Python when you can't do it all with comprehensions (reduce, zip, etc.), it's either imperative loops or functional notation (inside-out), while Ruby uses method call notation, which results in operations reading in the order of application.
- maksimum 8y agoGreat clarification. You're right, reduce was a typo. I definitely like how method call notations can be read left-to-right rather than inside out. Now I'm curious why python developers decided not to add these 'transformation methods' to all Iterables.