digitalmars.D.learn - How to map elements of a tuple?
- Andrey (7/9) Aug 22 2018 Hello,
- Alex (4/13) Aug 22 2018 Could you explain, how you mix a type "bool" and a value "Qwerty"
- Simen =?UTF-8?B?S2rDpnLDpXM=?= (17/26) Aug 22 2018 I believe this should be what you're looking for:
Hello, Is there a template/function/mixin... in the library that I can use to map elements of a tuple?object.foo(Mapper!myMapFunction(1, bool, "Qwerty", EnumedColor.Red));where "Mapper" is this mapper and "myMapFunction" is a template function that I want to apply to each member in tuple. I know that there is std.algorithm.map but as I understand it is suitable only for arrays (types are the same).
Aug 22 2018
On Wednesday, 22 August 2018 at 10:36:32 UTC, Andrey wrote:Hello, Is there a template/function/mixin... in the library that I can use to map elements of a tuple?Could you explain, how you mix a type "bool" and a value "Qwerty" in a single tuple? Especially, which value do you pass to you function, when the template parameter becomes bool?object.foo(Mapper!myMapFunction(1, bool, "Qwerty", EnumedColor.Red));where "Mapper" is this mapper and "myMapFunction" is a template function that I want to apply to each member in tuple. I know that there is std.algorithm.map but as I understand it is suitable only for arrays (types are the same).
Aug 22 2018
On Wednesday, 22 August 2018 at 10:36:32 UTC, Andrey wrote:Hello, Is there a template/function/mixin... in the library that I can use to map elements of a tuple?I believe this should be what you're looking for: import std.typecons; auto map(alias fn, T...)(Tuple!T arg) { import std.conv : text; import std.range : iota; import std.algorithm.iteration : joiner, map; return mixin(text("tuple(",T.length.iota.map!(i => text("fn(arg[",i,"])")).joiner(", "),")")); } unittest { import std.conv : to; auto a = tuple(1,2,"").map!(a => a.to!string); assert(a == tuple("1","2","")); } -- Simenobject.foo(Mapper!myMapFunction(1, bool, "Qwerty", EnumedColor.Red));where "Mapper" is this mapper and "myMapFunction" is a template function that I want to apply to each member in tuple. I know that there is std.algorithm.map but as I understand it is suitable only for arrays (types are the same).
Aug 22 2018