www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 9611] New: std.algorithm.nWayUnion(Tuple) too?

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

           Summary: std.algorithm.nWayUnion(Tuple) too?
           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



In some cases I have had to merge ranges of different type. So maybe for such
situations it's worth supporting nWayUnion of a Tuple of ranges:


import std.algorithm: nWayUnion, map;
import std.range: iota;
import std.typecons: tuple;
void main() {
    auto a = iota(10);
    auto b = [3, 6, 9];
    auto c = iota(11).map!q{a * a};
    auto r = nWayUnion(tuple(a, b, c));
}


Note: in all such of my usage cases the number of the ranges was limited, 2 or
3. So when the input of nWayUnion is a tuple I think there is no need for
nWayUnion to keep the ranges inside with a BinaryHeap.

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

Current workaround, suggested by Ali Çehreli:

import std.algorithm: nWayUnion, map;
import std.range: iota, InputRange, inputRangeObject;
void main() {
    InputRange!int a = inputRangeObject(iota(10));
    InputRange!int b = inputRangeObject([3, 6, 9]);
    InputRange!int c = inputRangeObject(iota(11).map!q{a * a});
    auto r = nWayUnion([a, b, c]);
}

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


Chris Cain <zshazz gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |zshazz gmail.com




 Current workaround, suggested by Ali Çehreli:
 
 import std.algorithm: nWayUnion, map;
 import std.range: iota, InputRange, inputRangeObject;
 void main() {
     InputRange!int a = inputRangeObject(iota(10));
     InputRange!int b = inputRangeObject([3, 6, 9]);
     InputRange!int c = inputRangeObject(iota(11).map!q{a * a});
     auto r = nWayUnion([a, b, c]);
 }
A function that does the conversion to InputRange!E could easily be created and added to Phobos to handle this type of situation. Ranges such as nWayUnion could call this (currently poorly named) tupWrapper when isTuple!T returns true. --- import std.stdio, std.range, std.typecons, std.algorithm; void main() { auto tup = tuple(iota(5), repeat(1).take(3), [5,9,30]); foreach(e; tupWrapper(tup).nWayUnion()) writeln(e); } auto tupWrapper(Tup)(Tup tup) { alias E = ElementType!(Tup.Types[0]); InputRange!E[] arr; foreach(T; Tup.Types) static assert(is(ElementType!T == E)); foreach(ref e; tup) arr ~= inputRangeObject(e); return arr; } --- Currently, that solution isn't too robust. Ideally it would figure out the most powerful range type (either InputRange, ForwardRange, ..., RandomAccessRange) that all the types in the Tuple.Types support and it would return that. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Feb 27 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9611






 A function that does the conversion to InputRange!E could easily be created and
 added to Phobos to handle this type of situation.
I think InputRange and inputRangeObject are not needed to solve this. A "static foreach" on the Tuple fields (that contain ranges) looking for the smallest value seems enough to me. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Feb 27 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9611


Andrei Alexandrescu <andrei erdani.com> changed:

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



PST ---
Yah, this should be added. Suffice to add a one-argument member function to
SortedRange.

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