3 ms·
I don't think so, select is just a filter. List comprehensions map and select at the same time. Regarding TLDMs, how do you propose mapping a TLDM over an arra
by djacobs 16y ago
I don't think so, select is just a filter. List comprehensions map and select at the same time.
Regarding TLDMs, how do you propose mapping a TLDM over an array? For example:
def transform(x)
# implementation ...
end
list = [1, 2, 3]
# doesn't work ...
list.map &transform
Edit: &method(:transform) is way too verbose compared with Python's solution
- tenderlove 16y agodef transform(x) x * 2 end list = [1, 2, 3] p list.map &method(:transform) <3 <3 <3 <3
- tenderlove 16y agoalias :m :method <3 <3 <3
- philwelch 16y agoThat's probably why explicitly calling transform inside the block, or making transform a method of the objects you're trying to transform, is more idiomatic Ruby. You're right in that list comprehensions are semantically equivalent to combining map and select, not just select.
- carbon8 16y agoYou wouldn't use a named Ruby method for something like this. You'd typically use a Proc or block. [1,2,3].map{ |x| x * 2 } or if you wanted to reuse the block for other things transform = lambda { |x| x * 2 } [1,2,3].map(&transform)
- djacobs 16y agoExactly, but there should be an easier syntax that declaring a lambda for each algorithm I want to map over.
- deleted 16y ago[deleted]