Jump to page: 1 2
Thread overview
[Issue 7666] New: A function to reverse the items of a tuple
Oct 27, 2012
Andrej Mitrovic
Oct 27, 2012
timon.gehr@gmx.ch
Oct 27, 2012
timon.gehr@gmx.ch
Oct 27, 2012
Andrej Mitrovic
Oct 28, 2012
timon.gehr@gmx.ch
Apr 05, 2013
Andrej Mitrovic
Apr 05, 2013
Andrej Mitrovic
Apr 05, 2013
Andrej Mitrovic
Apr 05, 2013
Andrej Mitrovic
Apr 06, 2013
Andrej Mitrovic
March 07, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7666

           Summary: A function to reverse the items of a tuple
           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 2012-03-07 11:13:30 PST ---
In some situations I'd like a function to reverse a tuple:

reversed(tuple(a,b)) == reversed(tuple(b,a))
reversed(tuple(a,b,c)) == reversed(tuple(c,b,a))
reversed(tuple(a,b,c,d)) == reversed(tuple(d,c,b,a))


Once built-in associative arrays have a byPair() method, that returns a
2-tuple, a reversed(tuple(a,b)) is useful when you need the pairs in reversed
(value-key) order.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 27, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7666


Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich@gmail.com


--- Comment #1 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2012-10-27 15:07:05 PDT ---
First thing I could come up with, but it requires editing the Tuple!() struct. A public alias is added in Tuple struct:

alias Reverse!fieldSpecs revFieldSpecs;

And the internal FieldSpec struct is moved outside and made public. Then the following is possible:


auto reversed(T)(T tup)
    if (isTuple!T)
{
    static string getMixin(T)()
    {
        string[] result;
        foreach (i; 0 .. T.Types.length)
        {
            result ~= xformat("tup.field[%d]", i);
        }

        return result.retro.join(", ");
    }

    static string getSpecs(T)()
    {
        string[] result;
        foreach (spec; T.revFieldSpecs)
        {
            result ~= spec.Type.stringof;
            result ~= xformat(`"%s"`, spec.name);
        }

        return result.join(", ");
    }

    enum str = xformat("return Tuple!(%s)(%s);", getSpecs!(T)(),
getMixin!(T)());
    mixin(str);
}

void main()
{
    alias Tuple!(int, "index", string, "value") Entry;
    Entry e;
    e.index = 4;
    e.value = "Hello";

    auto rev = reversed(e);
    assert(e.index == 4);
    assert(e.value == "Hello");
    writeln(e);
    writeln(rev);
}

Prints:
Tuple!(int,"index",string,"value")(4, "Hello")
Tuple!(string,"value",int,"index")("Hello", 4)

If there is a non-invasive way of doing this it would be welcome.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 27, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7666


timon.gehr@gmx.ch changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |timon.gehr@gmx.ch


--- Comment #2 from timon.gehr@gmx.ch 2012-10-27 15:59:04 PDT ---
(In reply to comment #1)
> ...
> 
> If there is a non-invasive way of doing this it would be welcome.

import std.typecons, std.conv, std.range, std.algorithm;

auto reversed(T)(T t) if(isTuple!T){
    return
mixin(`tuple(`~iota(T.length).retro.map!(a=>text("t[",a,"]")).join(",")~`)`);
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 27, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7666



--- Comment #3 from timon.gehr@gmx.ch 2012-10-27 16:20:46 PDT ---
(In reply to comment #2)
> (In reply to comment #1)
> > ...
> > 
> > If there is a non-invasive way of doing this it would be welcome.
> 

With field names:

import std.typecons, std.conv, std.range, std.algorithm;

auto reversed(T)(T t) if(isTuple!T){
    static if(is(T X:Tuple!A,A...)) alias A Spec;
    static struct Local{
        template Seq(T...){ alias T Seq; }
        template RevSpec(A...){
            enum num=A.length>1&&!is(A[1])?2:1;
            static if(A.length) alias Seq!(RevSpec!(A[num..$]),A[0..num])
RevSpec;
            else alias A RevSpec;
        }
    }
    return mixin(`Tuple!(Local.RevSpec!Spec)(`~
        iota(T.length).retro.map!(a=>text("t[",a,"]")).join(",")~
    `)`);
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 27, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7666



--- Comment #4 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2012-10-27 16:54:02 PDT ---
(In reply to comment #3)
> With field names

Interesting, the last time I saw this syntax:

static if(is(T X : Tuple!A, A...)) alias A Spec;

it was in the templates book (https://github.com/PhilippeSigaud/D-templates-tutorial), where Philippe mentions:

<<beg quote
For me, the main limitation is that template tuple parameters are not ac-
cepted. Too bad. See, imagine you use std.typecons.Tuple a lot. At one point,
you need a template to test if something is a Tuple!(T...) for some T which
can be 0 or more types. Though luck, is is a bit of a letdown there, as you
cannot do:

template isTuple(T)
{
    static if (is(T tup == Tuple!(InnerTypes), InnerTypes...))
    {
        enum isTuple = true;
    }
}
end quote>>

Maybe this was a compiler limitation in earlier versions.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 28, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7666



--- Comment #5 from timon.gehr@gmx.ch 2012-10-27 18:10:59 PDT ---
(In reply to comment #4)
> ...
> Maybe this was a compiler limitation in earlier versions.

Yup, Kenji Hara fixed that recently.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 05, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=7666



--- Comment #6 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-04-05 12:16:18 PDT ---
Got something better:

auto reversed(T)(T t)
    if (isTuple!T)
{
    static if (is(T : Tuple!A, A...))
        alias RevTypes = Reverse!A;

    Tuple!RevTypes result;
    auto tup = t.tupleof;
    result.tupleof = Reverse!tup;
    return result;
}

void main()
{
    auto tup = tuple(1, "2");
    assert(tup.reversed == tuple("2", 1));
}

I'll make a pull shortly.

P.S. this is a compile-error: Reverse!(t.tupleof)

Might be worth filing this as a bug.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 05, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=7666


Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull
         AssignedTo|nobody@puremagic.com        |andrej.mitrovich@gmail.com


--- Comment #7 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-04-05 12:22:29 PDT ---
https://github.com/D-Programming-Language/phobos/pull/1244

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 05, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=7666



--- Comment #8 from github-bugzilla@puremagic.com 2013-04-05 12:37:17 PDT ---
Commits pushed to master at https://github.com/D-Programming-Language/phobos

https://github.com/D-Programming-Language/phobos/commit/cc7c5c5ed2b2e345a29f5e8a663db4556eb8819e Fixes Issue 7666 - Implement function to reverse fields of a tuple.

https://github.com/D-Programming-Language/phobos/commit/5c2890f23eddb8ac72ed57e44e1c54b6f43593e1 Merge pull request #1244 from AndrejMitrovic/Fix7666

Issue 7666 - Implement function to reverse fields of a tuple.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 05, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=7666


Andrei Alexandrescu <andrei@erdani.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |andrei@erdani.com
         Resolution|                            |FIXED


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
« First   ‹ Prev
1 2