On 06/01/2015 03:31 PM, Adam wrote:
Hi,
I have a string of pairs of integers, where pairs are delimited from
each other by commas, and members of the pair are delimited by a space.
I'd like to end up with something like a range of 2-tuples, which I can
then sort with a lambda. I'm running into problems trying to do this,
after splitting on commas:
Tuple!(int,int) coord = to!(int[])(splitter(pairString," ").array[]);
...which may not even be a good idea, I don't know. I've been following
D for a long time, but this is the first time I've tried to actually use
it; this seems like the kind of thing that should take just a few lines,
if only I knew the libraries and range concepts well enough.
To be clear, this is what I have:
"192 14, 301 3, 578 0, 0 17"
...and this is what I want:
[(578,0),(301,3),(192,14),(0,17)]
What's the best way to do this? Should I be using map!() somewhere?
Thanks,
Adam
With no promises on performance and with a bonus mind-blowing format
string... :) I am sure it can be improved a lot.
import std.stdio;
import std.algorithm;
import std.range;
import std.conv;
void main()
{
auto input = "192 14, 301 3, 578 0, 0 17";
auto result = input
.splitter(',')
.map!splitter
.joiner
.map!(to!int)
.chunks(2)
.map!array
.array
.sort()
.retro;
writeln(result);
writefln("[%((%(%s,%))%|,%)]", result);
}
Prints both an array of arrays and the same format that you wanted:
[[578, 0], [301, 3], [192, 14], [0, 17]]
[(578,0),(301,3),(192,14),(0,17)]
Ali