[Issue 13104] New: std.typecons.tupleOp
via Digitalmars-d-bugs
digitalmars-d-bugs at puremagic.com
Sat Jul 12 01:32:12 PDT 2014
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 at puremagic.com
Reporter: bearophile_hugs at 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)
--
More information about the Digitalmars-d-bugs
mailing list