www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 13104] New: std.typecons.tupleOp

https://issues.dlang.org/show_bug.cgi?id=13104

          Issue ID: 13104
           Summary: std.typecons.tupleOp
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: Phobos
          Assignee: nobody puremagic.com
          Reporter: bearophile_hugs eml.cc

In some cases I have two tuples of the same type and I need to perform the same
pairwise operation on the tuple items, similar to a vector operation on D
arrays. So I suggest to add to Phobos a high order function like "tupleOp2"
(other better name are possible):

tupleOp2!q{a + b}(tuple(1, 1.5), tuple(2, 3.2))
==>
tuple(3, 4.7)

That is equivalent to:
tuple(tuple(1, 1.5)[0] + tuple(2, 3.2)[0], tuple(1, 1.5)[1] + tuple(2, 3.2)[1])


tupleOp2!max(tuple(1, 3.5), tuple(2, 3.2))
==>
tuple(2, 3.5)

That is equivalent to:
tuple(max(tuple(1, 3.5)[0], tuple(2, 3.2)[0]), max(tuple(1, 3.5)[1], tuple(2,
3.2)[1]))


It can be combined with other high order functions:

[tuple(1, 3.5), tuple(2, 6.1), tuple(-1, 3.2), ].reduce!(tupleOp2!max)
==>
tuple(2, 6.1)

--
Jul 12 2014