-
-
Notifications
You must be signed in to change notification settings - Fork 747
std.algorithm.searching: Implement none as shorthand for !any.
#6421
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 |
|---|---|---|
|
|
@@ -155,9 +155,8 @@ are true. | |
|
|
||
| /++ | ||
| Checks if $(I _any) of the elements verifies `pred`. | ||
| `!any` can be used to verify that $(I none) of the elements verify | ||
| `pred`. | ||
| This is sometimes called `exists` in other languages. | ||
| `! any!pred` is equivalent to `none!pred`. | ||
| +/ | ||
| template any(alias pred = "a") | ||
| { | ||
|
|
@@ -179,19 +178,15 @@ template any(alias pred = "a") | |
| import std.ascii : isWhite; | ||
| assert( all!(any!isWhite)(["a a", "b b"])); | ||
| assert(!any!(all!isWhite)(["a a", "b b"])); | ||
| assert(none!(all!isWhite)(["a a", "b b"])); | ||
| } | ||
|
|
||
| /++ | ||
| `any` can also be used without a predicate, if its items can be | ||
| evaluated to true or false in a conditional statement. `!any` can be a | ||
| convenient way to quickly test that $(I none) of the elements of a range | ||
| evaluate to true. | ||
| evaluated to true or false in a conditional statement. | ||
| +/ | ||
| @safe unittest | ||
| { | ||
| int[3] vals1 = [0, 0, 0]; | ||
| assert(!any(vals1[])); //none of vals1 evaluate to true | ||
|
|
||
| int[3] vals2 = [2, 0, 2]; | ||
| assert( any(vals2[])); | ||
| assert(!all(vals2[])); | ||
|
|
@@ -207,6 +202,38 @@ evaluate to true. | |
| assert(any!"a == 2"(a)); | ||
| } | ||
|
|
||
| /++ | ||
| Checks if $(I _none) of the elements verify `pred`. This is a convenience wrapper for `!any!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. Auto-highlighting has been removed recently - |
||
| +/ | ||
| template none(alias pred = "a") | ||
| { | ||
| /++ | ||
| Returns `true` if and only if $(I _no) value `v` found in the | ||
|
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. Auto-highlighting has been removed recently - |
||
| input _range `range` satisfies the predicate `pred`. | ||
| Performs (at most) $(BIGOH range.length) evaluations of `pred`. | ||
| +/ | ||
| bool none(Range)(Range range) | ||
| if (isInputRange!Range && is(typeof(unaryFun!pred(range.front)))) | ||
| { | ||
| return !any!pred(range); | ||
| } | ||
| } | ||
|
|
||
| /++ | ||
| `none` can also be used without a predicate, if its items can be | ||
| evaluated to true or false in a conditional statement. | ||
| +/ | ||
| @safe unittest | ||
| { | ||
| int[3] vals1 = [0, 0, 0]; | ||
| assert(none(vals1[])); | ||
|
|
||
| int[3] vals2 = [2, 0, 2]; | ||
| assert(!none(vals2[])); | ||
|
|
||
| assert(none((int[]).init)); | ||
| } | ||
|
|
||
| // balancedParens | ||
| /** | ||
| Checks whether `r` has "balanced parentheses", i.e. all instances | ||
|
|
||
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.
See_Also: $(LREF all), $(LREF none)?