Hacker News new | past | comments | ask | show | jobs | submit login

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



    def transform(x)
      x * 2
    end
    
    list = [1, 2, 3]
    
    p list.map &method(:transform)
<3 <3 <3 <3


You 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)


Exactly, but there should be an easier syntax that declaring a lambda for each algorithm I want to map over.


That'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.


    alias :m :method
<3 <3 <3




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: