Lazy lists

bearophile bearophileHUGS at lycos.com
Wed Feb 23 05:29:41 PST 2011


Andrei:

> popFrontN(c, 3);
> ... use c.front() ...

Right. I have used the c.popFrontN(3); syntax and then I have not read the error message well.

A drop(range, n) is an expression, it allows to write it as this:
drop(c, 3).front


> You shouldn't need array most at all. Use chain() instead of ~.

I don't understand how, this is a simple try:

import std.stdio, std.string, std.algorithm, std.array, std.range;

auto nextCarpet(R)(R c) {
     auto b = map!q{a ~ a ~ a}(c);
     return chain(b, map!q{a ~ a.replace("#"," ") ~ a}(c), b);
}

void main() {
     auto c = recurrence!((a, n){ return nextCarpet(a[n-1]); })(["#"]);
     popFrontN(c, 3);
     writeln(c.front().join("\n"));
}


It gives:
...\dmd\src\phobos\std\range.d(3659): Error: cannot cast from ChainImpl!(Map!(result,string[]),Map!(result,string[]),Map!(result,string[])) to string[]

Later I will think more on this. Maybe I am missing something.
Thank you for your answers,
bye,
bearophile


More information about the Digitalmars-d mailing list