[Issue 9871] std.typecons.asRange

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Apr 3 21:15:05 PDT 2013


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


bearophile_hugs at eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|std.typecons.AsRange/asRang |std.typecons.asRange
                   |e                           |


--- Comment #1 from bearophile_hugs at eml.cc 2013-04-03 21:15:04 PDT ---
A simpler implementation:


import std.typecons, std.typetuple, std.stdio, std.algorithm,
       std.range;

bool validAsRange(T)() {
    if (!isTuple!T || NoDuplicates!(T.Types).length != 1)
        return false;
    foreach (i, _; T.Types) // Useless?
        if (T.tupleof[i].offsetof != T.Types[0].sizeof * i)
            return false;
    return true;
}

T.Types[0][] asRange(T)(ref T tup) if (validAsRange!T()) {
    return (cast(T.Types[0]*)&tup[0])[0 .. T.length];
}

void main() { // demo
    auto a = [0];
    auto b = [5];
    auto c = [11];
    auto d = [22];
    Tuple!(int,int,int,int) tup = zip(a, b, c, d).front;
    auto result = tup.asRange.map!(x => x * 2).array;
    assert(result == [0, 10, 22, 44]);
}

-- 
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