On Saturday, 23 February 2013 at 16:29:49 UTC, Andrei
Alexandrescu wrote:
On 2/23/13 5:56 PM, monarch_dodra wrote:
On Saturday, 23 February 2013 at 14:28:28 UTC, Jens Mueller
wrote:
Can we have a generic function/code that is as efficient as
yours?
Well, for now, we have "map" (and "iota"). This usually covers
enough
ground, but there remains cases where it is sub-optimal.
What we would need is a "generate". Basically, a range that
calls a
function to generate the value of front on the fly. I've seen
enough
threads like this one that "abuse" iota/repeat/take/map (or
sequence and
recurrence for that matter), just to obtain a generic
generator.
Let's just write Generator/generator.
Make sure you copy Sequence/sequence :o).
Andrei
Have you actually tried it? Do you know what the signature of a
function passed to sequence is?
I think this bug entry sums it up pretty well, and why "Sequence"
is not really adapted:
http://forum.dlang.org/thread/[email protected]%2Fissues%2F
//----
import std.stdio, std.range, std.conv, std.random, std.typecons;
void main()
{
dstring letters = "abcd";
dchar fun(Tuple!(), uint) {return letters[uniform(0, $)];}
writeln(sequence!fun().take(9));
}
//----
I failed to use a lambda or mixin in this case.
I really don't think we should push to try to warp the existing
stuff to work in any scenario, but rather, give the tools
required to work intuitively for any scenario.