Two easy pieces

bearophile bearophileHUGS at lycos.com
Mon Apr 1 20:20:55 PDT 2013


ixid:

> UFCS also needs its write function to be able to print whatever 
> data it is passed and then pass exactly the data it was passed 
> on so you can drop write statements into UFCS chains and take 
> them out without needing to chop around the chain.

Right, second version:


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

auto ufcsWrite(T)(T data) {
     write(data);
     return data;
}

auto ufcsWriteln(T)(T data) {
     writeln(data);
     return data;
}

auto ufcsWritef(T)(T data, string format) {
     writef(format, data);
     return data;
}

auto ufcsWritefln(T)(T data, string format) {
     writefln(format, data);
     return data;
}

void main() {
     // Problem from:
     // reddit.com/r/dailyprogrammer_ideas/comments/15in89
     immutable txt = ["Line one", "Line 2"];

     txt
     .map!q{ a.length }
     .ufcsWriteln
     .reduce!max
     .iota
     .map!(i => txt.transversal(i))
     .ufcsWritefln("%(%s\n%)");
}


Bye,
bearophile


More information about the Digitalmars-d-learn mailing list