random k-sample of a file

bearophile bearophileHUGS at lycos.com
Thu Oct 9 14:02:34 PDT 2008


Andrei Alexandrescu:
> But where's the D code you guys?

My third converted to D, using my libs:

import std.conv, d.all;

void main(string[] args) {
    assert(args.length == 3);
    string filename = args[1];
    int k = toInt(args[2]);
    assert (k > 0);

    string[] chosen_lines;
    foreach (i, line; xfile(filename))
        if (i < k)
            chosen_lines ~= line;
        else
            if (fastRandom() < (1.0 / (i+1)))
                chosen_lines[randInt(k-1)] = line;

    putr(chosen_lines);
}


> D is better than Python at scripting. Ahem.

I know many languages, and so far I have never found something better than Python to create working prototypes.

Bye,
bearophile



More information about the Digitalmars-d mailing list