Another rambling musing by a Dynamic Programmer - map!

Jacob Carlborg via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jun 24 12:13:32 PDT 2014


On 2014-06-24 04:34, John Carter wrote:
> So in Ruby and R and Scheme and... I have happily used map / collect for
> years and years.
>
> Lovely thing.
>
> So I did the dumb obvious of
>
>     string[] stringList = map!...;
>
> And D barfed, wrong type, some evil voldemort thing again.
>
> So..
>
>     auto stringList = map!....;
>
> and we're good..
>
> and happily use it as
>     foreach( s; stringList)....
>
> Suddenly the words in the map! documentation made sense to me... unlike
> Ruby, map doesn't allocate and populate an array. It just returns a lazy
> range.
>
> No array is allocated (unless I ask for one), it just does the lambda
> when I want it in the foreach!
>
> Cool! Very very cunning. Very light on resources.

I wished Ruby behaved that way quite often, especially when chaning 
multiple functions. In Ruby 2.0 there are lazy "map" and similar 
functions but I've heard they are slower than the eager ones.

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list