Fixed matrix rows joining
Timon Gehr
timon.gehr at gmx.ch
Sun Jan 15 09:06:35 PST 2012
On 01/15/2012 02:38 AM, bearophile wrote:
> If I have a simple fixed-size matrix and I need to linearize (flatten) it, the function join() seems to not not work:
>
>
> import std.array: join;
> void main() {
> int[4][4] table;
> join(table);
> }
>
>
> test.d(4): Error: template std.array.join(RoR,R) if (isInputRange!(RoR)&& isInputRange!(ElementType!(RoR))&& isForwardRange!(R)&& is(Unqual!(ElementType!(ElementType!(RoR))) == Unqual!(ElementType!(R)))) does not match any function template declaration
> test.d(4): Error: template std.array.join(RoR,R) if (isInputRange!(RoR)&& isInputRange!(ElementType!(RoR))&& isForwardRange!(R)&& is(Unqual!(ElementType!(ElementType!(RoR))) == Unqual!(ElementType!(R)))) cannot deduce template function from argument types !()(int[4u][4u])
>
>
> This too doesn't work:
> join(table[]);
>
>
>
> This compiles:
> join(map!q{ a[] }(table[]));
>
>
> But this program shows there is something wrong (I know what's wrong), it prints:
> [6, 4219787, 4, 6, 4219787, 4]
>
>
> import std.stdio: writeln;
> import std.algorithm: map;
> import std.array: join;
> void main() {
> int[3][2] table = [[1,2,3],[4,5,6]];
> int[] result = join(map!q{ a[] }(table[]));
> writeln(result);
> }
>
>
>
> This prints the right output, but it allocates lot of memory:
> [1, 2, 3, 4, 5, 6]
>
>
> import std.stdio: writeln;
> import std.algorithm: map;
> import std.array: join;
> void main() {
> int[3][2] table = [[1,2,3],[4,5,6]];
> int[] result2 = join(map!q{ a.dup }(table[]));
> writeln(result2);
> }
>
>
> Do you have better suggestions?
join(map!((int[] a)=>a)(table[]))
although I'd like
join(map!((ref a)=>a[])(table[]))
to work. I'll file an enhancement.
> Is the function join() worth fixing/changing to improve this use case?
>
> Bye,
> bearophile
More information about the Digitalmars-d-learn
mailing list