www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 4468] New: std.string.join() for lazy iterable of strings

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

           Summary: std.string.join() for lazy iterable of strings
           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


--- Comment #0 from bearophile_hugs eml.cc 2010-07-15 17:24:25 PDT ---
This Python code generates a lazy sequence of the first 20 integers (starting
from 0), then maps them lazily to strings, and then joins them in a single
string:


from itertools import imap
r = imap(str, xrange(20))
result = "".join(r)
print result



The same code can be written in D2:

import std.stdio, std.algorithm, std.conv, std.range, std.string;
void main() {
    auto r = map!("to!string(a)")(iota(20));
    //string result = join(array(r), ""); // OK
    string result = join(r, ""); // error
    writeln(result);
}


But I'd like a std.string.join() able to accept a Range too, so I can use
join(r, "") instead of join(array(r), ""), because it's shorter, simpler,
natural enough and reduces memory used (and probably increases code performance
too).

When the total size of the resulting string is not known because the input is a
lazy sequence of strings, then probably join() has to use something like
appender() to improve its performance.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 15 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4468


Andrei Alexandrescu <andrei metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
                 CC|                            |andrei metalanguage.com
         AssignedTo|nobody puremagic.com        |andrei metalanguage.com


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 09 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4468



--- Comment #1 from bearophile_hugs eml.cc 2011-02-07 14:33:28 PST ---
See also bug 5542

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 07 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4468



--- Comment #2 from bearophile_hugs eml.cc 2011-07-30 06:54:17 PDT ---
This is a very common need. Another example:


import std.range;
void main() {
    auto aa = [1:["hello", "red"], 2:["blue", "yellow"]];
    auto r1 = join(aa.values); // OK
    auto r2 = join(aa.byValue()); // error
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 30 2011
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4468


bearophile_hugs eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|                            |FIXED


--- Comment #3 from bearophile_hugs eml.cc 2011-12-21 10:33:37 PST ---
Now in DMD 2.057 this works:

import std.stdio, std.algorithm, std.conv, std.range, std.string;
void main() {
    auto r = map!("to!string(a)")(iota(20));
    //string result = join(array(r), ""); // OK
    string result = join(r, ""); // error
    writeln(result);
}


This line:
auto r = map!("to!string(a)")(iota(20));
is also better written:
auto r = map!text(iota(20));


But this doesn't work still:

import std.range;
void main() {
    auto aa = [1:["hello", "red"], 2:["blue", "yellow"]];
    auto r2 = join(aa.byValue()); // error
}


To make this work there are two solutions:
1) Change byValue() to return a Range.
2) Change join() to accept an opApply too.

The first solution allows to use byValue in many other cases, so I think join()
is OK, and I close this bug report.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 21 2011