Converting array in to aliased tuple type.
Mengu
mengukagan at gmail.com
Mon Dec 25 14:08:08 UTC 2017
On Monday, 25 December 2017 at 12:03:32 UTC, aliak wrote:
> Hi, been looking for a way to convert an array to a tuple, but
> can't seem to find one. Is there one?
>
> Looking for something like:
>
> alias Point = Tuple!(int, "x", int, "y");
> enum data = "1,2:8,9";
> auto points = data
> .split(':')
> .map!(a => a
> .split(',')
> .map!(to!int)
> )
> .map!Point; // <-- this works if you do `.map!(a =>
> Point(a[0], a[1]));` instead
>
> Cheers!
hi aliak
since Point is a Tuple and does not have a constructor that takes
a list of integers (int[]), you should have a helper function.
import std.stdio: writeln;
import std.string: split;
import std.algorithm: map;
import std.typecons: Tuple;
import std.conv: to;
alias Point = Tuple!(int, "x", int, "y");
enum data = "1,2:8,9";
alias makePoint = (auto ref points) => Point(points[0],
points[1]);
alias convertToInt = (string parts) =>
parts.split(',').map!(to!int);
auto points =
data.split(':').map!(convertToInt).map!(makePoint);
writeln(points);
More information about the Digitalmars-d-learn
mailing list