Bug 13104 – std.typecons.tupleOp

Status
NEW
Severity
enhancement
Priority
P4
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-07-12T08:32:12Z
Last change time
2024-12-01T16:21:51Z
Assigned to
No Owner
Creator
bearophile_hugs
Moved to GitHub: phobos#10072 →

Comments

Comment #0 by bearophile_hugs — 2014-07-12T08:32:12Z
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)
Comment #1 by greensunny12 — 2018-03-31T14:10:26Z
Have a look at https://github.com/dlang/phobos/pull/6386 with this the following will work: (tuple(1, 2) ~ tuple(3, 4)).expand.only.sum = 10 (tuple(1, 2) ~ tuple(3, 4)).expand.only.max = 4 > tupleOp2!q{a + b}(tuple(1, 1.5), tuple(2, 3.2)) So here's what we got today: zip(tuple(1, 1.5).expand.only, tuple(2, 3.2).expand.only).map!(a => a[0] + a[1]).writeln; https://run.dlang.io/is/6rq9MB What I think could be improved: - make tuples ranges by default (no .expand.only hacks) - allow map to take a multi-argument lambda if the size of the front's element is statically known
Comment #2 by robert.schadek — 2024-12-01T16:21:51Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/phobos/issues/10072 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB