View mode: basic / threaded / horizontal-split · Log in · Help
April 13, 2012
Re: Sampling algorithms for D
On 13/04/12 01:44, bearophile wrote:
> final size_t select(ref UniformRNG urng)
> in {
>      assert(_recordsRemaining>  0);
>      assert(_sampleRemaining>  0);
> } body {
>      ...
> }

OK.  I'm confused by these asserts, because if I go beyond what is acceptable by 
calling select() even after I've collected a complete sample, no error is 
thrown.  e.g. if I put in place a function:

void sampling_test_simple(SamplerType, UniformRNG)
                         (size_t records, size_t samples, ref UniformRNG urng)
{
      auto s = new SamplerType(records,samples,urng);
      while(s.sampleRemaining > 0) {
            write("\trecord selected: ", s.select(urng), ".");
            write("\trecords remaining: ", s.recordsRemaining, ".");
            writeln("\tstill to sample: ", s.sampleRemaining, ".");
      }
      // Let's see if we can bust this ...

      writeln("Selecting 1 more just for luck: ", s.select(urng), ", records 
remaining: ", s.recordsRemaining, ", still to sample: ", s.sampleRemaining);
      writeln("Selecting 1 more just for luck: ", s.select(urng), ", records 
remaining: ", s.recordsRemaining, ", still to sample: ", s.sampleRemaining);
      writeln("Selecting 1 more just for luck: ", s.select(urng), ", records 
remaining: ", s.recordsRemaining, ", still to sample: ", s.sampleRemaining);
      writeln("Selecting 1 more just for luck: ", s.select(urng), ", records 
remaining: ", s.recordsRemaining, ", still to sample: ", s.sampleRemaining);
      writeln("Selecting 1 more just for luck: ", s.select(urng), ", records 
remaining: ", s.recordsRemaining, ", still to sample: ", s.sampleRemaining);
      writeln("Selecting 1 more just for luck: ", s.select(urng), ", records 
remaining: ", s.recordsRemaining, ", still to sample: ", s.sampleRemaining);
      writeln("Selecting 1 more just for luck: ", s.select(urng), ", records 
remaining: ", s.recordsRemaining, ", still to sample: ", s.sampleRemaining);
      writeln("Selecting 1 more just for luck: ", s.select(urng), ", records 
remaining: ", s.recordsRemaining, ", still to sample: ", s.sampleRemaining);
      writeln("Selecting 1 more just for luck: ", s.select(urng), ", records 
remaining: ", s.recordsRemaining, ", still to sample: ", s.sampleRemaining);
}

(... see current GitHub code: https://github.com/WebDrake/SampleD )

It doesn't make any difference if I compile with -debug enabled:

   gdmd -debug -oftest sampled.d
April 13, 2012
Re: Sampling algorithms for D
Dmitry Olshansky:

> I believe it's something that reasonable people may disagree on.
> To me it's perfectly easy to see what return x++; does.

I agree that "return x++;" is not too bad for a human reader, but code with mutation inside expressions (mostly written by other people) has caused me tons of troubles (and the semantics of ++ and -- are as much undefined in D as in C, still). So I usually kit it with fire as soon as I see it.
I'd like to modify the -- and ++ to make them return void. I think Go is designed like that.


> or use some std.range primitives ( I think iota does a [begin, end) range)
> foreach( x ; iota(recordRemaining-1, limit+1, -1)){
> 	y2 *= top--/bottom--;
> }

Only if you don't care a lot for the performance of that specific loop. (DMD is sometimes not even able to optimize foreach() loops as well as for() loops. Go figure what it does on iota()).

Bye,
bearophile
April 13, 2012
Re: Sampling algorithms for D
Joseph Rushton Wakeling:

> > final size_t select(ref UniformRNG urng)
> > in {
> >      assert(_recordsRemaining>  0);
> >      assert(_sampleRemaining>  0);
> > } body {
> >      ...
> > }
> 
> OK.  I'm confused by these asserts,

What's confusing? I don't understand. It's contract-based programming, the code is essentially the same as before:
http://dlang.org/dbc.html

Bye,
bearophile
April 13, 2012
Re: Sampling algorithms for D
On 13/04/12 13:10, bearophile wrote:
> What's confusing? I don't understand. It's contract-based programming, the code is essentially the same as before:
> http://dlang.org/dbc.html

No, I understand the principle; I just don't understand why the code is running 
without errors being displayed when I violate the bounds of those asserts.
April 13, 2012
Re: Sampling algorithms for D
On 04/13/2012 02:41 AM, Joseph Rushton Wakeling wrote:
> On 13/04/12 01:44, bearophile wrote:
>> final size_t select(ref UniformRNG urng)
>> in {
>> assert(_recordsRemaining> 0);
>> assert(_sampleRemaining> 0);
>> } body {
>> ...
>> }
>
> OK. I'm confused by these asserts, because if I go beyond what is
> acceptable by calling select() even after I've collected a complete
> sample, no error is thrown.

This is a complicated issue that touches how in contracts are not 
inherited. I think your issue is because the interface does not define 
any in contracts, effectively allowing every call to select(). Please 
start reading here: :)

  http://d.puremagic.com/issues/show_bug.cgi?id=6856

As a general reminder, there is also the invariant() blocks, and 
sometimes enforce() is more appropriate than assert.

Ali
April 13, 2012
Re: Sampling algorithms for D
On 13/04/12 19:49, Ali Çehreli wrote:
> This is a complicated issue that touches how in contracts are not inherited. I
> think your issue is because the interface does not define any in contracts,
> effectively allowing every call to select(). Please start reading here: :)

You're absolutely right.  I think I will kill the interface; it's not really 
clear that it adds anything to the whole setup.

Thanks very much!
Next ›   Last »
1 2
Top | Discussion index | About this forum | D home