www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 9882] New: Add UFCS-friendly printing functions

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9882

           Summary: Add UFCS-friendly printing functions
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc



This program prints the transposed of some strings. You can see that if you
want to print the result of a long UFCS chain with a formatting string you are
forced to break the chain, and it's not immediate to print intermediate steps:


import std.stdio: writeln, writefln;
import std.range: iota, transversal;
import std.algorithm: map, reduce, max;

void main() {
    immutable txt = ["Line one", "Line 2"];

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



Output:

8
6
LL
ii
nn
ee

o2
n
e

- - - - - - - - - - - - - - - - - - - - - - -

A simple way to solve both problems is to add to std.stdio four printing
functions that are UFCS-friendly, where the format string is leading:


import std.stdio: write, writeln, writef, writefln;
import std.traits: isSomeString;

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

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

auto uWritef(T, TS)(T data, TS format) if (isSomeString!TS) {
    writef(format, data);
    return data;
}

auto uWritefln(T, TS)(T data, TS format) if (isSomeString!TS) {
    writefln(format, data);
    return data;
}

// Usage demo -------------

import std.range: iota, transversal;
import std.algorithm: map, reduce, max;

void main() {
    immutable txt = ["Line one", "Line 2"];

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



Output (also note that 8 and 6 are printed with the array syntax):

[8, 6]
LL
ii
nn
ee

o2
n
e

- - - - - - - - - - - - - - - - - - - - - - -

Disadvantages:

1) A .tap() function that allows to perform some impure operation on the
stream, but keep it flowing, is more general than those printing functions.
Something like this:



import std.range: iota, transversal;
import std.algorithm: map, reduce, max;
import std.range: tap;

void main() {
    immutable txt = ["Line one", "Line 2"];

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


In practice I've seen that in most cases I such tap only to print.



2) functions like that uWritefln() accept only the format string beside the
stream to print. So you can't print the stream plus some other data. In theory
this can be solved with printing functions like this:


auto uWrite(TArgs...)(TArgs items) {
    write(items);
    return items[0];
}

auto uWriteln(TArgs...)(TArgs items) {
    writeln(items);
    return items[0];
}

auto uWritef(TArgs...)(TArgs items) if (isSomeString!(TArgs[$ - 1])) {
    writef(items[$ - 1], items[0 .. $ - 1]);
    return items[0];
}

auto uWritefln(TArgs...)(TArgs items) if (isSomeString!(TArgs[$ - 1])) {
    writefln(items[$ - 1], items[0 .. $ - 1]);
    return items[0];
}


But their order of printing is not intuitive, so I suggest to not use them.


See also the discussion thread:
http://forum.dlang.org/thread/vkuzklqsqqoawkbpoxpg forum.dlang.org

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 05 2013
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9882


Jacob Carlborg <doob me.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |doob me.com



I like that "tap" function in Ruby and use it for non-range code as well.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 05 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9882





 I like that "tap" function in Ruby and use it for non-range code as well.
To use tap I have used a syntax like: .tap!q{ writeln(s) } .tap!q{ writefln("%(%s\n%)", s) }; But I don't know if such syntax is good and possible in D. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Apr 05 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9882




 To use tap I have used a syntax like:
 
     .tap!q{ writeln(s) }
     .tap!q{ writefln("%(%s\n%)", s) };
 
 But I don't know if such syntax is good and possible in D.
Sure, but I prefer the lambda syntax: .tap!(s => writeln(s)) -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Apr 05 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9882





 Sure, but I prefer the lambda syntax:
 
 .tap!(s => writeln(s))
A lambda like s => writeln(s) returns void, it's kind of the opposite of the lambda calculus :-) -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Apr 05 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9882





 A lambda like s => writeln(s) returns void, it's kind of the opposite of the
 lambda calculus :-)
Right, forgot that. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Apr 05 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9882




A suggestion in D.learn shows me that sometimes
std.functional.binaryReverseArgs is an acceptable solution for the terminal
writefln (but it can't be used for intermediate printing):


import std.stdio: writeln, writefln;
import std.range: iota, transversal;
import std.algorithm: map, reduce, max;
import std.functional: binaryReverseArgs;

void main() {
    immutable txt = ["Line one", "Line 2"];

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

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 26 2013