Thread overview | |||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
June 24, 2013 Trouble with lockstep | ||||
---|---|---|---|---|
| ||||
Hello, I have code which generates a histogram from an array and prints out the histogram bins/counts. However I cannot get the code to iterate and print out the histogram to work. The following is a minimal example: import std.range; import std.stdio; void main() { ubyte[] data = [17,32,32,32,38,39,39,47,47,47,47,109,111,111,128]; uint[ubyte.max - ubyte.min] bins; foreach(ubyte val; data) bins[val]++; foreach( idx, count; lockstep( iota!ubyte(ubyte.min, ubyte.max), bins ) ) { if(count > 0 ) { writeln("Bin = ", idx, " count = ", count ); } } } Alternately: http://dpaste.com/hold/1267929/ I get the following error messages which I cannot decipher. test.d(11): Error: template std.range.lockstep does not match any function template declaration. Candidates are: /usr/include/dmd/phobos/std/range.d(4724): std.range.lockstep(Ranges...)(Ranges ranges) if (allSatisfy!(isInputRange, Ranges)) /usr/include/dmd/phobos/std/range.d(4730): std.range.lockstep(Ranges...)(Ranges ranges, StoppingPolicy s) if (allSatisfy!(isInputRange, Ranges)) /usr/include/dmd/phobos/std/range.d(4724): Error: template std.range.lockstep cannot deduce template function from argument types !()(Result, uint[255LU]) Failed: 'dmd' '-v' '-o-' 'test.d' '-I.' Can anyone identify what I am doing wrong. Also I am curious to know why std.range includes both Lockstep and lockstep - they seem like the same thing. Craig |
June 24, 2013 Re: Trouble with lockstep | ||||
---|---|---|---|---|
| ||||
Posted in reply to Craig Dillabaugh | On 06/24/2013 03:05 PM, Craig Dillabaugh wrote:
> Can anyone identify what I am doing wrong. Also I am curious to know why std.range includes both Lockstep and lockstep - they seem like the same thing.
lockstep is a helper function that returns an instance of Lockstep.
The reason is that if you instantiate a struct/class template directly you have to explicitly state the template parameters. A function template call can infer the template parameters and use them to instantiate a class instance.
|
June 24, 2013 Re: Trouble with lockstep | ||||
---|---|---|---|---|
| ||||
Posted in reply to Craig Dillabaugh | lockstep(iota!ubyte(ubyte.min, ubyte.max), bins[]) Works, but this leaves you with other errors, I couldn't find a solution for. Something seems to be wrong with opApply of lockstep. Or maybe I miss something obvious... |
June 24, 2013 Re: Trouble with lockstep | ||||
---|---|---|---|---|
| ||||
Posted in reply to Joseph Rushton Wakeling | On Monday, 24 June 2013 at 14:15:55 UTC, Joseph Rushton Wakeling
wrote:
> On 06/24/2013 03:05 PM, Craig Dillabaugh wrote:
>> Can anyone identify what I am doing wrong. Also I am curious to
>> know why std.range includes both Lockstep and lockstep - they
>> seem like the same thing.
>
> lockstep is a helper function that returns an instance of Lockstep.
>
> The reason is that if you instantiate a struct/class template directly you have
> to explicitly state the template parameters. A function template call can infer
> the template parameters and use them to instantiate a class instance.
Thanks. That makes sense ... I think.
|
June 24, 2013 Re: Trouble with lockstep | ||||
---|---|---|---|---|
| ||||
Posted in reply to Craig Dillabaugh | Craig Dillabaugh:
> foreach( idx, count; lockstep( iota!ubyte(ubyte.min,
> ubyte.max), bins ) )
Also note that doesn't iterate the whole ubyte range. Maybe we need another iota range for that, with a different name or with an optional template argument string like "[]" as std.random.uniform. Opinions welcome.
Bye,
bearophile
|
June 24, 2013 Re: Trouble with lockstep | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Monday, 24 June 2013 at 14:33:48 UTC, bearophile wrote:
> Craig Dillabaugh:
>
>> foreach( idx, count; lockstep( iota!ubyte(ubyte.min,
>> ubyte.max), bins ) )
>
> Also note that doesn't iterate the whole ubyte range. Maybe we need another iota range for that, with a different name or with an optional template argument string like "[]" as std.random.uniform. Opinions welcome.
>
> Bye,
> bearophile
Opps. Of course.
The optional template argument sounds like a good idea.
Any idea why the original wouldn't compile though?
Craig
|
June 24, 2013 Re: Trouble with lockstep | ||||
---|---|---|---|---|
| ||||
Posted in reply to David | David: > Something seems to be wrong with opApply of lockstep. > Or maybe I miss something obvious... I have suggested to remove lockstep from Phobos: http://d.puremagic.com/issues/show_bug.cgi?id=8155 Why don't you try to use std.range.zip? Bye, bearophile |
June 24, 2013 Re: Trouble with lockstep | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Monday, 24 June 2013 at 14:56:41 UTC, bearophile wrote: > David: > >> Something seems to be wrong with opApply of lockstep. >> Or maybe I miss something obvious... > > I have suggested to remove lockstep from Phobos: > http://d.puremagic.com/issues/show_bug.cgi?id=8155 > > Why don't you try to use std.range.zip? > > Bye, > bearophile Funny you should mention that, I vaguely recalled you old thread, and replaced lockstep with zip. After making the following changes: uint[ubyte.max - ubyte.min+1] bins; ... foreach( e; zip( iota(ubyte.min, ubyte.max+1), bins ) ) { if(count > 0 ) { writeln("Bin = ", e[0], " count = ", e[1] ); } } I now get the error (which seems to be the same problem I had before - see the last error): test.d(11): Error: template std.range.zip does not match any function template declaration. Candidates are: /usr/include/dmd/phobos/std/range.d(4451): std.range.zip(Ranges...)(Ranges ranges) if (Ranges.length && allSatisfy!(isInputRange, Ranges)) /usr/include/dmd/phobos/std/range.d(4458): std.range.zip(Ranges...)(StoppingPolicy sp, Ranges ranges) if (Ranges.length && allSatisfy!(isInputRange, Ranges)) /usr/include/dmd/phobos/std/range.d(4451): Error: template std.range.zip cannot deduce template function from argument types !()(Result, uint[256LU]) |
June 24, 2013 Re: Trouble with lockstep | ||||
---|---|---|---|---|
| ||||
Posted in reply to Craig Dillabaugh | Craig Dillabaugh:
> I now get the error (which seems to be the same problem I had
> before - see the last error):
> ...
> /usr/include/dmd/phobos/std/range.d(4451): Error: template
> std.range.zip cannot deduce template function from argument types
> !()(Result, uint[256LU])
Most range/algorithm functions unfortunately don't accept a fixes size array. So you have to slice it:
void main() {
import std.stdio, std.range;
ubyte[] data = [17, 32, 32, 32, 38, 39, 39, 47,
47, 47, 47, 109, 111, 111, 128];
uint[ubyte.max - ubyte.min + 1] bins;
foreach (immutable val; data)
bins[val]++;
foreach (uint idx, count; iota(ubyte.min, ubyte.max + 1).zip(bins[]))
if (count > 0)
writeln("Bin = ", idx, " count = ", count);
}
Bye,
bearophile
|
June 24, 2013 Re: Trouble with lockstep | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Monday, 24 June 2013 at 15:15:46 UTC, bearophile wrote:
> Craig Dillabaugh:
>
>> clip
>
> Most range/algorithm functions unfortunately don't accept a fixes size array. So you have to slice it:
>
> void main() {
> import std.stdio, std.range;
>
> ubyte[] data = [17, 32, 32, 32, 38, 39, 39, 47,
> 47, 47, 47, 109, 111, 111, 128];
> uint[ubyte.max - ubyte.min + 1] bins;
>
> foreach (immutable val; data)
> bins[val]++;
>
> foreach (uint idx, count; iota(ubyte.min, ubyte.max + 1).zip(bins[]))
> if (count > 0)
> writeln("Bin = ", idx, " count = ", count);
> }
>
> Bye,
> bearophile
Thanks. Now it works, if I use .zip().
|
Copyright © 1999-2021 by the D Language Foundation