Formatted output of range of tuples
anonymous via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Oct 8 14:34:54 PDT 2014
On Wednesday, 8 October 2014 at 21:21:47 UTC, antropod wrote:
> Hello!
>
> Consider this code:
>
> +++
> import std.stdio;
> import std.range;
> import std.algorithm;
>
> void printIndexedArray1(T, Range)(T[] source, Range indexes)
> {
> foreach(row; zip(indexes, source))
> {
> foreach(col; row) {
> write(col, " ");
> }
> writeln;
> }
> }
>
> void printIndexedArray2(T, Range)(T[] source, Range indexes)
> {
> writef("%(%(%s %)\n%)", zip(indexes, source));
> }
>
> void main()
> {
> uint[] data = [17, 30, 48, 140, 10, 01, 126, 138, 140, 3, 501];
> printIndexedArray1(data, sequence!"n");
> printIndexedArray2(data, sequence!"n");
>
> }
> +++
>
> Looks fairly straightforward. But, the second function causes
> compilation error:
>
> std.format.FormatException at C:\D\dmd2\windows\bin\..\..\src\phobos\std\format.d(2
> 585): Expected '%s' format specifier for type 'Tuple!(uint,
> uint)'
>
> Can you help me with that?
You can turn the tuples into ranges with `only`:
writef("%(%(%s %)\n%)", zip(indexes, source).map!(t =>
only(t.expand)));
More information about the Digitalmars-d-learn
mailing list