[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: LFM + LFSP = LFE?



> >>>True of Ruby, too. Instead of:
> >>>(map (lambda (n) (* n 2)) '(0 1 2 3 4))
> >>>You write:
> >>>[0,1,2,3,4].map{|n| n*2}
> >>>
> >>What's the Ruby equivalent of this?
> >>
> >>(map * '(1 2 3) '(4 5 6))
> Python:
> 
> map(mul, [1,2,3], [1,2,3])
> 
> Mind you, I think this example shows more about the flexibility of 
> Scheme's map function (which Python's happens to share) than about syntax.

Actually, I think the example does show something about syntax/semantics -- I
don't believe there's an obvious, natural version of this for Ruby.  Ruby's
approach to iteration is fundamental to the language, and to the idiom of its
programs.  It uses a simple OO style (one receiver, no language-supported
multimethods), rather than a functional style.  Note that the Ruby
implementation of "map" is a method on Enumerable objects.  

That being said, it is certainly possible to define a non-OO map function
(which would actually be a method in Kernel):
    map([1, 2, 3], [1, 2, 3]) {|x,y| x*y}
But it seems somewhat unnatural in Ruby.

The implementation of Kernel::map is left as an exercise to the reader.  It
also is somewhat awkward, since Ruby doesn't cleanly support parallel
iteration over multiple Enumerables.  A variation on map that supported
indefinitely large objects (e.g. I/O streams) would be very awkward --
the implementation would require threads or continuations.
--
	Steven
"I'm a fifth-level Vegan.  I don't eat anything that casts a shadow."