www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - can't zip a char[5], string[5], real[5]

reply Shriramana Sharma <samjnaa_dont_spam_me gmail.com> writes:
import std.stdio, std.range;
void mywrite(char [5] chars, real[5] vals)
{
    static string [5] fmts = ["%9.4f, ", "%9.4f; ", "%3d, ", "%3d, ", 
"%3d\n"];
    foreach (e; zip(chars, fmts, vals)) write(e[0], " = ", 
e[1].format(e[2]));
}

Compiling gives:

zip_string.d(5): Error: template std.range.zip cannot deduce function from 
argument types !()(char[5], string[5], real[5]), candidates are:
/usr/include/dmd/phobos/std/range/package.d(3678):        
std.range.zip(Ranges...)(Ranges ranges) if (Ranges.length && allSatisfy!
(isInputRange, Ranges))
/usr/include/dmd/phobos/std/range/package.d(3711):        
std.range.zip(Ranges...)(StoppingPolicy sp, Ranges ranges) if (Ranges.length 
&& allSatisfy!(isInputRange, Ranges))

I would have thought there would be no problem in creating a zip of three 
arrays of equal length. What's the problem here?

-- 

Oct 21 2015
parent reply anonymous <anonymous example.com> writes:
On Wednesday, 21 October 2015 at 14:06:54 UTC, Shriramana Sharma 
wrote:
 import std.stdio, std.range;
 void mywrite(char [5] chars, real[5] vals)
 {
     static string [5] fmts = ["%9.4f, ", "%9.4f; ", "%3d, ", 
 "%3d, ",
 "%3d\n"];
     foreach (e; zip(chars, fmts, vals)) write(e[0], " = ",
 e[1].format(e[2]));
 }

 Compiling gives:
[...]
 I would have thought there would be no problem in creating a 
 zip of three arrays of equal length. What's the problem here?
Static arrays are not ranges. You can't popFront them. Try slicing them: chars[], fmts[], vals[]
Oct 21 2015
parent Jonathan M Davis via Digitalmars-d-learn writes:
On Wednesday, October 21, 2015 14:11:20 anonymous via Digitalmars-d-learn wrote:
 On Wednesday, 21 October 2015 at 14:06:54 UTC, Shriramana Sharma
 wrote:
 import std.stdio, std.range;
 void mywrite(char [5] chars, real[5] vals)
 {
     static string [5] fmts = ["%9.4f, ", "%9.4f; ", "%3d, ",
 "%3d, ",
 "%3d\n"];
     foreach (e; zip(chars, fmts, vals)) write(e[0], " = ",
 e[1].format(e[2]));
 }

 Compiling gives:
[...]
 I would have thought there would be no problem in creating a
 zip of three arrays of equal length. What's the problem here?
Static arrays are not ranges. You can't popFront them. Try slicing them: chars[], fmts[], vals[]
Though when you do that, be careful that you don't keep the dynamic arrays (or any other ranges created from them) around longer than the static arrays; otherwise, the dynamic arrays will then be referring to invalid memory, and nasty things will happen... But as long as the dynamic arrays don't exist longer than the static ones that they refer to, then you're fine. - Jonathan M Davis
Oct 21 2015