Skip to content

Issue 10265 - RandomSample fails when passed an InputRange as input #1332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 7, 2013

Conversation

WebDrake
Copy link
Contributor

@WebDrake WebDrake commented Jun 4, 2013

http://d.puremagic.com/issues/show_bug.cgi?id=10265
Issue fixed by making presence of save() property conditional on input being a ForwardRange.

 * Issue fixed by making presence of save() property conditional
   on input being a ForwardRange.
@WebDrake
Copy link
Contributor Author

WebDrake commented Jun 4, 2013

This probably needs an extra unittest for the case where randomSample is passed an InputRange. Any suggestions for something to use? file.byLine springs to mind, but would require specifying a file to open.

@jmdavis
Copy link
Member

jmdavis commented Jun 4, 2013

@WebDrake It's simple enough to create one that I'd just create a simple one for the test. Something like

struct InputRange
{
    @property bool empty() const { return _arr.empty; }
    @property int front() const { return _arr.front; }
    void popFront() { _arr.popFront(); }
    int[] _arr;
}

@WebDrake
Copy link
Contributor Author

WebDrake commented Jun 4, 2013

It's simple enough to create one that I'd just create a simple one for the test.

Seems your example may have uncovered another issue with RandomSample. If I use it, I get the following error message:

std/random.d(1649): Error: constructor std.random.RandomSample!(TestInputRange, void).RandomSample.this
field _input must be initialized in constructor, because it is nested struct

This is because all the initialization of variables is done in a separate initialize() function which is called by the constructors.

Is there any other way of fixing this other than moving the _input = input line to the constructors? The whole point of the initialize() function was to avoid that kind of duplication :-P

The assignment of _input = input has been moved from
initialize() to the constructors so as to avoid
problems with nested structs.
@WebDrake
Copy link
Contributor Author

WebDrake commented Jun 4, 2013

Without a solution for the nested struct issue mentioned in the previous post, I've moved the assignment of _input to constructors for now -- to be moved back if anyone can suggest a superior alternative.

@WebDrake
Copy link
Contributor Author

WebDrake commented Jun 7, 2013

Ping ... ? I think this is in an acceptable state now.

andralex added a commit that referenced this pull request Jun 7, 2013
Issue 10265 - RandomSample fails when passed an InputRange as input
@andralex andralex merged commit 783902c into dlang:master Jun 7, 2013
@WebDrake WebDrake deleted the randomsample-save branch June 16, 2013 13:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants