digitalmars.D.learn - std.range.zip and tuple labels
- Joseph Rushton Wakeling (8/8) May 15 2013 Hi all,
- bearophile (15/20) May 15 2013 See:
- Joseph Rushton Wakeling (8/10) May 15 2013 Feels a bit nasty, and I have the feeling it would probably slow things ...
Hi all, If I use zip() to collate together one or more ranges, the individual elements get returned as a Tuple of the corresponding types. Is there any way to get zip to label those elements, i.e. to return e.g. Tuple!(int, "one", float, "two") instead of Tuple!(int, float) .... ?
May 15 2013
Joseph Rushton Wakeling:If I use zip() to collate together one or more ranges, the individual elements get returned as a Tuple of the corresponding types. Is there any way to get zip to label those elements, i.e. to return e.g.See: http://d.puremagic.com/issues/show_bug.cgi?id=8715 I think currently you have to use map!() to replace the tuple. Maybe am unsafe map-cast suffices: import std.stdio, std.range, std.algorithm, std.typecons; void main() { auto r1 = zip(["red", "blue"], [10, 20]); r1.writeln; alias T2 = Tuple!(string,"col", int,"val"); r1.map!(r => T2(r[])).writeln; r1.map!(r => cast(T2)r).writeln; } Bye, bearophile
May 15 2013
On 05/15/2013 08:00 PM, bearophile wrote:I think currently you have to use map!() to replace the tuple. Maybe am unsafe map-cast suffices:Feels a bit nasty, and I have the feeling it would probably slow things down ... ? :-( I like the zipWith idea, though I can't help but wish zip() itself was flexible enough to allow something like, zip(arr1, "one", arr2, "two") ... though I guess that would fall over if you ever wanted to use zip to lock together two immutable strings.
May 15 2013