Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 35 additions & 8 deletions std/algorithm/searching.d
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Copy link
Contributor

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) ?

+/
template any(alias pred = "a")
{
Expand All @@ -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[]));
Expand All @@ -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`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Auto-highlighting has been removed recently - _ is no longer necessary :)
dlang/dlang.org#2307

+/
template none(alias pred = "a")
{
/++
Returns `true` if and only if $(I _no) value `v` found in the
Copy link
Contributor

Choose a reason for hiding this comment

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

Auto-highlighting has been removed recently - _ is no longer necessary :)
dlang/dlang.org#2307

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
Expand Down