[Issue 9611] std.algorithm.nWayUnion(Tuple) too?

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Feb 27 17:13:40 PST 2013


http://d.puremagic.com/issues/show_bug.cgi?id=9611


Chris Cain <zshazz at gmail.com> changed:

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


--- Comment #1 from Chris Cain <zshazz at gmail.com> 2013-02-27 17:13:37 PST ---
(In reply to comment #0)
> 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: -------


More information about the Digitalmars-d-bugs mailing list