On 2004-09-30 22:28:07 +0900, Gavin Kistner wrote:
> ...it was a week later than I pulled up the documentation and saw that 
> they're the same thing. Something about 'map' made absolutely no sense 
> to me (it implies a hash-like permanent connection between the old and 
> the new), and collect made perfect sense (go traipsing through the 
> entire field of daisies, picking up only the best ones).

But this isn't what collect does. It picks up all the daisies. I think
it's called collect because it returns a collection of the same type
as the receiver in Smalltalk. The elements in this collection are the
result of evaluating the given block with each element of the original
collection as first argument. The funny thing is that collect in Ruby
always returns an array and not the same type. If Ruby would know
a default way of adding to a collection (like <<) one could define
collect like this:

module Enumerable
  def collect
    inject(self.class.new) { |s, x| s << yield(x); s }
  end
end

Map (or mapcar) was invented in the LISP world. It maps a given
function f on each value of the (list x1 x2 ...) and returns the
resulting new (list (f x1) (f x2) ...). Ruby doesn't have a builtin
list type but uses Array instead. So it makes a lot of sense to have
map return an array. I think the idea behind Ruby's map is: If
i have the enumerable enum let's pretend it was already an array. Now
lets yield the given block to each of its values and build a new array
of the same size that consists of the return values of the block.

The ruby block does act pretty much like a function here:

You can translate from Scheme

(map (lambda (x) (+ x 1)) (list 1 2 3))

into Ruby pretty easy

[1, 2, 3].map &lambda { |x| x + 1 }

Now, you can call Non-Prefix-LISP if you like. ;)

-- 
lambda { |c| lambda { |f| f[f] } [ lambda { |f| c[lambda { |x| f[f][x] } ] }] }