Two easy pieces

Steven Schveighoffer schveiguy at yahoo.com
Mon Apr 1 21:57:02 PDT 2013


On Mon, 01 Apr 2013 20:10:44 -0400, bearophile <bearophileHUGS at lycos.com>  
wrote:

> Sometimes you want to print something coming out of a UFCS chain with a  
> formatting string. In this case you can't append the writef/writefln at  
> the end of the chain. The problem is easy to solve with two simple  
> functions like this. Are they worth having in std.stdio?
>
>
> import std.stdio, std.range, std.algorithm;
>
> void ufcsWritef(T)(T data, string format) {
>      writef(format, data);
> }
>
> void ufcsWritefln(T)(T data, string format) {
>      writefln(format, data);
> }
>
> void main() {
>      // Problem from:
>      // reddit.com/r/dailyprogrammer_ideas/comments/15in89
>      immutable txt = ["Line one", "Line 2"];
>      foreach (i; 0 .. txt.map!q{ a.length }.reduce!max)
>          txt.transversal(i).writeln;
>
>      // Or equivalently:
>      txt
>      .map!q{ a.length }
>      .reduce!max
>      .iota
>      .map!(i => txt.transversal(i))
>      .ufcsWritefln("%(%s\n%)");
> }

Good idea.

I propose fwrite.  We're reversing the parameters, so I reversed the  
function name.  Well, sort of :)

-Steve


More information about the Digitalmars-d-learn mailing list