Skip to content

Provide pred-only overload to std.algorithm.searching.findSkip#5577

Merged
dlang-bot merged 3 commits intodlang:masterfrom
wilzbach:algorithm-find-skip
Nov 19, 2017
Merged

Provide pred-only overload to std.algorithm.searching.findSkip#5577
dlang-bot merged 3 commits intodlang:masterfrom
wilzbach:algorithm-find-skip

Conversation

@wilzbach
Copy link
Contributor

@wilzbach wilzbach commented Jul 8, 2017

As I pointed out in #5497 (comment), findSkip is very similar to skipOver - in fact before #5497 neither skipOver nor findSkip has a pred-only overload.
My point is that there's no argument to add this overload to skipOver and not to findSkip - on the contrary there's a even a case for only findSkip that @CyberShadow pointed out:

That loop simply consumes the two input ranges for comparison; it does not consume multiple instances of the "prefix". Consider the difference between s.skipOver(' ') and s.skipOver!(c => c == ' ') - intuitively one may think that these should behave the same, but they don't!

findSkip intuitively implies that it loops.

@dlang-bot
Copy link
Contributor

Thanks for your pull request, @wilzbach!

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

bool findSkip(alias pred, R1)(ref R1 haystack)
if (isForwardRange!R1 && is(typeof(unaryFun!pred(haystack.front))))
{
return skipOver!(pred, R1)(haystack);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course, if we decide to change the behavior introduced in #5543, we could easily inline #5543 here ...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We certainly do, as seems like that idea has been pre-approved.

@wilzbach wilzbach changed the title Provide pred-only overload to std.algorithm.searching Provide pred-only overload to std.algorithm.searching.findSkip Jul 8, 2017
@wilzbach wilzbach force-pushed the algorithm-find-skip branch from 47ba808 to 82dffd5 Compare July 8, 2017 01:15
bool findSkip(alias pred, R1)(ref R1 haystack)
if (isForwardRange!R1 && is(typeof(unaryFun!pred(haystack.front))))
{
return skipOver!(pred, R1)(haystack);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We certainly do, as seems like that idea has been pre-approved.

* the first occurrence of $(D needle); otherwise $(D false),
* leaving $(D haystack) untouched.
*/
bool findSkip(alias pred, R1)(ref R1 haystack)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice if this could return a size_t instead of just a bool.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I just would like to wait for @andralex's opinion on this matter before investing more work.

@wilzbach wilzbach added this to the 2.076.0 milestone Jul 9, 2017
@wilzbach wilzbach requested a review from andralex July 9, 2017 00:40
@wilzbach wilzbach force-pushed the algorithm-find-skip branch from 82dffd5 to c1c2df6 Compare July 9, 2017 00:50
@wilzbach
Copy link
Contributor Author

wilzbach commented Jul 9, 2017

Would be nice if this could return a size_t instead of just a bool.

Ah - it was really trivial, so I added support for returning size_t quickly. I wonder whether skipOver should also return size_t ...

*
* Returns: The number of times `pred` was truthfully evaluated.
*/
size_t findSkip(alias pred, R1)(ref R1 haystack)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

predicate must be ifTestable

{
haystack.popFront();
c++;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not a loop with initial test? No need to duplicate the test in the prelude.

size_t result;
while (!haystack.empty && unaryFun!pred(haystack.front))
{
    ++result;
}
return result;

@andralex
Copy link
Member

I'm OK with the addition.

@JackStouffer JackStouffer removed this from the 2.076.0 milestone Jul 27, 2017
@JackStouffer
Copy link
Contributor

Ping @wilzbach

@wilzbach
Copy link
Contributor Author

@JackStouffer @ZombineDev I finally got around rebasing this PR. It should be ready now (:

Copy link
Member

@andralex andralex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a doc nit


/**
* Advances the `haystack` as long as `pred` evaluates to `true`.
* The `haystack` is positioned before position of first falsely evaluation of `pred`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The haystack is positioned so as pred evaluates to false for haystack.front.

@dlang-bot dlang-bot merged commit 7a2732e into dlang:master Nov 19, 2017
@wilzbach wilzbach deleted the algorithm-find-skip branch December 11, 2017 02:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants

Comments