-
-
Notifications
You must be signed in to change notification settings - Fork 753
skipOver should have a single argument with pred version #5497
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4026,6 +4026,64 @@ if (is(typeof(binaryFun!pred(r.front, e))) && isInputRange!R) | |
| assert(!s2.skipOver('a')); | ||
| } | ||
|
|
||
| /** | ||
| * An overload of `skipOver` which takes a predicate. Skips over any elements | ||
| * from the start of the range that evaluate to `true`. Stops popping elements | ||
| * on the first element which `pred(r.front)` equals `false`. | ||
| * | ||
| * Params: | ||
| * pred = the test for each element | ||
| * r = the range to skip over | ||
| * Returns: | ||
| * `true` if elements were skipped over, `false` otherwise. | ||
| */ | ||
| bool skipOver(alias pred, R)(ref R r) if (ifTestable!(typeof(r.front), unaryFun!pred)) | ||
| { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A)
|
||
| bool ret = false; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Stop creating AST nodes for something that's done automatically (i.e default initialization).
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is bad advice. I can't find a quote at the moment, but Walter has been always saying that default initialization is not meant to provide useful default values, but to make using un-initialized variables deterministic and, wherever possible, an error. When possible, the default value of a type is an invalid one (NaN for floating-point types and '\xFF' for chars, which is an invalid UTF-8 code unit); the reason why most other types are initialized to zero is that they do not have an "invalid" value. So, do initialize your variables explicitly (when you feel it makes sense to) and do not rely on their default value. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What you didn't know is that the author of the PR is also the person who suggested to add a D-Scanner check for default initialization. My review comment couldn't be more legit.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not relevant. The pull request is for Phobos, not some personal repository of Jack's. |
||
|
|
||
| while (!r.empty && unaryFun!pred(r.front)) | ||
| { | ||
| r.popFront; | ||
| ret = true; | ||
| } | ||
|
|
||
| return ret; | ||
| } | ||
|
|
||
| /// | ||
| @safe pure unittest | ||
| { | ||
| import std.ascii : isWhite; | ||
|
|
||
| auto s1 = "\t\nHello"; | ||
| assert(s1.skipOver!(isWhite)); | ||
| assert(s1 == "Hello"); | ||
|
|
||
| auto s2 = "Hello\t\n"; | ||
| assert(s2.skipOver!(a => !isWhite(a))); | ||
| assert(s2 == "\t\n"); | ||
|
|
||
| auto s3 = "Hello\t\n"; | ||
| assert(!s3.skipOver!(isWhite)); | ||
| assert(s3 == "Hello\t\n"); | ||
| } | ||
|
|
||
| @system unittest | ||
| { | ||
| import std.algorithm.comparison : equal; | ||
| import std.ascii : isUpper; | ||
| import std.internal.test.dummyrange; | ||
|
|
||
| auto r1 = new ReferenceInputRange!int([1, 2, 3, 4, 5, 6]); | ||
| auto r2 = new ReferenceInputRange!dchar("Hello"); | ||
|
|
||
| r1.skipOver!(a => a < 3); | ||
| assert(r1.equal([3, 4, 5, 6])); | ||
|
|
||
| r2.skipOver!(isUpper); | ||
| assert(r2.equal("ello")); | ||
| } | ||
|
|
||
| /** | ||
| Checks whether the given | ||
| $(REF_ALTTEXT input range, isInputRange, std,range,primitives) starts with (one | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice. Please add but use
/// dittofor the documentation and consolidate with the existing doc.