higher-order funcs for ranges (with usual interface)

spir denis.spir at gmail.com
Wed Feb 2 04:26:39 PST 2011


Hello,

This bit of code for arrays:

Out[] map (In,Out) (In[] input, Out delegate (In) f) {
     Out[] output = new Out[](input.length);
     foreach (i,item ; input)
         output [i] = f(item);
     return output;
}
unittest {
     char character (uint code) {return cast(char)code;}
     uint[] codes = [0x61,0x62,0x63];
     // functional style
     writeln(map(codes, &character));    // "abc"
     // OO style
     writeln(codes.map(&character));     // "abc"
}

How to write this for ranges? I mean, with the same kind of interface to client 
code (not the interface of std.algo.map).
And is there a way to write it so that it works both for ranges and other kinds 
of sequences (arrays, strings); or even for anything "iterable" (AA, set, list, 
tree...).

For ranges, I'm looking for something similar to:
     Range!Out map (In,Out) (Range!In input, Out delegate (In) f) {...}
Indeed, the compiler should understand that Range!T is a type id just like T[].

Denis
-- 
_________________
vita es estrany
spir.wikidot.com



More information about the Digitalmars-d-learn mailing list