Welcome to Web-News
A Web-based News Reader
Subject lazy thoughts
From Andrei Alexandrescu <SeeWebsiteForEmail@erdani.org>
Date Mon, 12 Jan 2009 09:05:18 -0800
Newsgroups digitalmars.D

(I originally emailed this to Walter and a couple others, but I thought
it might be better to gather insights from the community.)

I'm gearing up for changing std.algorithm, and I am thinking of making
the major change of favoring lazy evaluation throughout. For example,
consider the map function. Right now map allocates and returns a new
vector, for example:

int[] arr = [ 1, 2, 3, 4 ];
auto squares = map!("a * a")(arr);
assert(squares == [ 1, 4, 9, 16 ]);

What happens is unfortunate because (1) all evaluation is done upfront
even if it is only partially needed, (2) allocation is done every call,
(3) the whole scheme won't work with unbounded inputs, e.g. generators
or even large files.

So now that we have nice range support in the language, I'm thinking
very seriously of switching full-bore to lazy semantics. In that case
map returns a range - a little struct that saves the input and trickles
out results one at a time.

One nice thing about lazy is that lazy includes eager. If you actually
want to "eagerize" map, you just call eager against the returned range:

int[] arr = [ 1, 2, 3, 4 ];
auto squares = eager(map!("a * a")(arr));
assert(squares == [ 1, 4, 9, 16 ]);

The real advantage comes when you actually exploit the laziness, e.g.:

int[] arr = [ 1, 2, 3, 4 ];
auto squares = map!("a * a")(arr);
foreach (x; squares) {
     writeln(x);
}

Look ma, no allocation!

I just wanted to gauge your opinion on this. It is a major departure
from the STL, and I think in the right direction. It is a departure
nonetheless and some STL users might feel uncomfortable.

Also, lazy evaluation has the risk of getting confusing as there's a lot
of escaping. Consider:

int[] arr = [ 1, 2, 3, 4 ];
auto squares = map!("a * a")(arr);
arr[] = [ 5, 6, 7, 8 ];

Now iterating squares will see different numbers than the original ones.

Please let me know what you think!


Andrei

Recent messages in this thread
 
-# lazy thoughts (Current message) Andrei Alexandrescu 12-Jan-2009 12:05 pm
.-# Re: lazy thoughts Daniel Keep 12-Jan-2009 12:12 pm
.|\# Re: lazy thoughts Andrei Alexandrescu 12-Jan-2009 12:14 pm
.-# Re: lazy thoughts dsimcha 12-Jan-2009 12:19 pm
.|-# Re: lazy thoughts Andrei Alexandrescu 12-Jan-2009 12:32 pm
.|.\# Re: lazy thoughts Denis Koroskin 12-Jan-2009 05:56 pm
.-# Re: lazy thoughts Sergey Gromov 12-Jan-2009 12:35 pm
.|-# Re: lazy thoughts dsimcha 12-Jan-2009 12:38 pm
.||-# Re: lazy thoughts Sergey Gromov 12-Jan-2009 12:53 pm
.||.\# Re: lazy thoughts Andrei Alexandrescu 12-Jan-2009 01:42 pm
.|\# Re: lazy thoughts Robert Fraser 12-Jan-2009 12:41 pm
.-# Re: lazy thoughts bearophile 12-Jan-2009 12:44 pm
.||# Re: lazy thoughts bearophile 12-Jan-2009 12:50 pm
.|\# Re: lazy thoughts Robert Fraser 12-Jan-2009 01:07 pm
.|# Re: lazy thoughts BCS 12-Jan-2009 01:48 pm
.-# Re: lazy thoughts Brian Palmer 12-Jan-2009 02:26 pm
.|\# Re: lazy thoughts Andrei Alexandrescu 12-Jan-2009 02:36 pm
.|# Re: lazy thoughts Max Samukha 12-Jan-2009 02:37 pm
.-# Re: lazy thoughts noobie 12-Jan-2009 02:40 pm
.|\# Re: lazy thoughts Andrei Alexandrescu 12-Jan-2009 04:29 pm
.-# Re: lazy thoughts Bill Baxter 12-Jan-2009 03:44 pm
.|-# Re: lazy thoughts Andrei Alexandrescu 12-Jan-2009 04:42 pm
.|.-# Re: lazy thoughts Bill Baxter 12-Jan-2009 05:05 pm
.|.|-# Re: lazy thoughts Andrei Alexandrescu 12-Jan-2009 05:10 pm
.|.||-# Re: lazy thoughts Daniel Keep 12-Jan-2009 09:39 pm
.|.||.-# Re: lazy thoughts Andrei Alexandrescu 12-Jan-2009 11:31 pm
.|.||..\# Re: lazy thoughts Jason House 12-Jan-2009 11:41 pm
.|.|-# Re: lazy thoughts bearophile 12-Jan-2009 08:47 pm
.|.|.|# Re: lazy thoughts Jason House 12-Jan-2009 08:56 pm
.|.|.\# Re: lazy thoughts Bill Baxter 12-Jan-2009 09:17 pm
.|.-# Re: lazy thoughts Brian Palmer 12-Jan-2009 06:48 pm
.|.|\# Re: lazy thoughts Andrei Alexandrescu 12-Jan-2009 07:53 pm
.|.-# Re: lazy thoughts Nick Sabalausky 12-Jan-2009 07:00 pm
.|..-# Re: lazy thoughts Andrei Alexandrescu 12-Jan-2009 07:54 pm
.|...-# Re: lazy thoughts Robert Fraser 12-Jan-2009 11:07 pm
.|....-# Re: lazy thoughts Andrei Alexandrescu 12-Jan-2009 11:42 pm
.|.....-# Re: lazy thoughts Robert Fraser 13-Jan-2009 04:27 am
.|......-# Re: lazy thoughts Fawzi Mohamed 13-Jan-2009 10:11 am
.|.......-# Re: lazy thoughts Robert Fraser 13-Jan-2009 04:39 pm
.|........\# Re: lazy thoughts Bill Baxter 13-Jan-2009 05:09 pm
.\# Re: lazy thoughts Jason House 12-Jan-2009 08:01 pm