Is std.array.replace supposed to work with char[]?

bearophile bearophileHUGS at lycos.com
Sun Feb 20 11:51:10 PST 2011


Jacob Carlborg:

> Every time I try to use D2 it's just a PITA to use. I've used D1 and Tango for 
> several years and had no problem with that.

I use this thread to ask regarding one specific little problem I have with strings. I want to generate a random string of AB using the array, map, etc, this looks like a possible implementation (in std.random there is no choice() function yet):


import std.stdio, std.random, std.string, std.algorithm, std.range;
void main() {
    auto m = map!((i){ return "AB"[uniform(0,2)]; })(iota(10));
    string s = join(array(m));
    writeln(s);
}


It gives this error:
...\dmd\src\phobos\std\array.d(62): Error: result[i] isn't mutable
test.d(5): Error: template instance std.array.array!(Map!(__dgliteral1,Iota!(int,uint))) error instantiating

What's the right way to write it in D?

The same code in Python2.x:

from random import choice
s = "".join(choice("AB") for _ in xrange(10))
print s

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list