mirror of
https://github.com/dlang/phobos.git
synced 2025-05-06 02:45:12 +03:00
Merge pull request #3800 from JackStouffer/lambda
Removed string predicates from std.algorithm.iteration documentation
This commit is contained in:
commit
731e47e242
1 changed files with 39 additions and 21 deletions
|
@ -20,7 +20,7 @@ $(T2 each,
|
||||||
$(D each!writeln([1, 2, 3])) eagerly prints the numbers $(D 1), $(D 2)
|
$(D each!writeln([1, 2, 3])) eagerly prints the numbers $(D 1), $(D 2)
|
||||||
and $(D 3) on their own lines.)
|
and $(D 3) on their own lines.)
|
||||||
$(T2 filter,
|
$(T2 filter,
|
||||||
$(D filter!"a > 0"([1, -1, 2, 0, -3])) iterates over elements $(D 1)
|
$(D filter!(a => a > 0)([1, -1, 2, 0, -3])) iterates over elements $(D 1)
|
||||||
and $(D 2).)
|
and $(D 2).)
|
||||||
$(T2 filterBidirectional,
|
$(T2 filterBidirectional,
|
||||||
Similar to $(D filter), but also provides $(D back) and $(D popBack) at
|
Similar to $(D filter), but also provides $(D back) and $(D popBack) at
|
||||||
|
@ -33,12 +33,12 @@ $(T2 joiner,
|
||||||
over the characters $(D "hello; world!"). No new string is created -
|
over the characters $(D "hello; world!"). No new string is created -
|
||||||
the existing inputs are iterated.)
|
the existing inputs are iterated.)
|
||||||
$(T2 map,
|
$(T2 map,
|
||||||
$(D map!"2 * a"([1, 2, 3])) lazily returns a range with the numbers
|
$(D map!(a => a * 2)([1, 2, 3])) lazily returns a range with the numbers
|
||||||
$(D 2), $(D 4), $(D 6).)
|
$(D 2), $(D 4), $(D 6).)
|
||||||
$(T2 permutations,
|
$(T2 permutations,
|
||||||
Lazily computes all permutations using Heap's algorithm.)
|
Lazily computes all permutations using Heap's algorithm.)
|
||||||
$(T2 reduce,
|
$(T2 reduce,
|
||||||
$(D reduce!"a + b"([1, 2, 3, 4])) returns $(D 10).)
|
$(D reduce!((a, b) => a + b)([1, 2, 3, 4])) returns $(D 10).)
|
||||||
$(T2 splitter,
|
$(T2 splitter,
|
||||||
Lazily splits a range by a separator.)
|
Lazily splits a range by a separator.)
|
||||||
$(T2 sum,
|
$(T2 sum,
|
||||||
|
@ -169,8 +169,8 @@ if (isBidirectionalRange!Range)
|
||||||
}
|
}
|
||||||
// Without cache, with array (greedy)
|
// Without cache, with array (greedy)
|
||||||
auto result1 = iota(-4, 5).map!(a =>tuple(a, fun(a)))()
|
auto result1 = iota(-4, 5).map!(a =>tuple(a, fun(a)))()
|
||||||
.filter!"a[1]<0"()
|
.filter!(a => a[1] < 0)()
|
||||||
.map!"a[0]"()
|
.map!(a => a[0])()
|
||||||
.array();
|
.array();
|
||||||
|
|
||||||
// the values of x that have a negative y are:
|
// the values of x that have a negative y are:
|
||||||
|
@ -184,8 +184,8 @@ if (isBidirectionalRange!Range)
|
||||||
// Without array, with cache (lazy)
|
// Without array, with cache (lazy)
|
||||||
auto result2 = iota(-4, 5).map!(a =>tuple(a, fun(a)))()
|
auto result2 = iota(-4, 5).map!(a =>tuple(a, fun(a)))()
|
||||||
.cache()
|
.cache()
|
||||||
.filter!"a[1]<0"()
|
.filter!(a => a[1] < 0)()
|
||||||
.map!"a[0]"();
|
.map!(a => a[0])();
|
||||||
|
|
||||||
// the values of x that have a negative y are:
|
// the values of x that have a negative y are:
|
||||||
assert(equal(result2, [-3, -2, 2]));
|
assert(equal(result2, [-3, -2, 2]));
|
||||||
|
@ -231,8 +231,8 @@ same cost or side effects.
|
||||||
import std.algorithm.comparison : equal;
|
import std.algorithm.comparison : equal;
|
||||||
import std.range;
|
import std.range;
|
||||||
auto a = [1, 2, 3, 4];
|
auto a = [1, 2, 3, 4];
|
||||||
assert(equal(a.map!"(a - 1)*a"().cache(), [ 0, 2, 6, 12]));
|
assert(equal(a.map!(a => (a - 1) * a)().cache(), [ 0, 2, 6, 12]));
|
||||||
assert(equal(a.map!"(a - 1)*a"().cacheBidirectional().retro(), [12, 6, 2, 0]));
|
assert(equal(a.map!(a => (a - 1) * a)().cacheBidirectional().retro(), [12, 6, 2, 0]));
|
||||||
auto r1 = [1, 2, 3, 4].cache() [1 .. $];
|
auto r1 = [1, 2, 3, 4].cache() [1 .. $];
|
||||||
auto r2 = [1, 2, 3, 4].cacheBidirectional()[1 .. $];
|
auto r2 = [1, 2, 3, 4].cacheBidirectional()[1 .. $];
|
||||||
assert(equal(r1, [2, 3, 4]));
|
assert(equal(r1, [2, 3, 4]));
|
||||||
|
@ -924,7 +924,9 @@ unittest
|
||||||
/**
|
/**
|
||||||
$(D auto filter(Range)(Range rs) if (isInputRange!(Unqual!Range));)
|
$(D auto filter(Range)(Range rs) if (isInputRange!(Unqual!Range));)
|
||||||
|
|
||||||
Implements the higher order _filter function.
|
Implements the higher order _filter function. The predicate is passed to
|
||||||
|
$(XREF functional,unaryFun), and can either accept a string, or any callable
|
||||||
|
that can be executed via $(D pred(element)).
|
||||||
|
|
||||||
Params:
|
Params:
|
||||||
predicate = Function to apply to each element of range
|
predicate = Function to apply to each element of range
|
||||||
|
@ -1128,6 +1130,9 @@ private struct FilterResult(alias pred, Range)
|
||||||
* that the filtered range can be spanned from both directions. Also,
|
* that the filtered range can be spanned from both directions. Also,
|
||||||
* $(XREF range, retro) can be applied against the filtered range.
|
* $(XREF range, retro) can be applied against the filtered range.
|
||||||
*
|
*
|
||||||
|
* The predicate is passed to $(XREF functional,unaryFun), and can either
|
||||||
|
* accept a string, or any callable that can be executed via $(D pred(element)).
|
||||||
|
*
|
||||||
* Params:
|
* Params:
|
||||||
* pred = Function to apply to each element of range
|
* pred = Function to apply to each element of range
|
||||||
* r = Bidirectional range of elements
|
* r = Bidirectional range of elements
|
||||||
|
@ -1293,7 +1298,9 @@ Similarly to $(D uniq), $(D group) produces a range that iterates over unique
|
||||||
consecutive elements of the given range. Each element of this range is a tuple
|
consecutive elements of the given range. Each element of this range is a tuple
|
||||||
of the element and the number of times it is repeated in the original range.
|
of the element and the number of times it is repeated in the original range.
|
||||||
Equivalence of elements is assessed by using the predicate $(D pred), which
|
Equivalence of elements is assessed by using the predicate $(D pred), which
|
||||||
defaults to $(D "a == b").
|
defaults to $(D "a == b"). The predicate is passed to $(XREF functional,binaryFun),
|
||||||
|
and can either accept a string, or any callable that can be executed via
|
||||||
|
$(D pred(element, element)).
|
||||||
|
|
||||||
Params:
|
Params:
|
||||||
pred = Binary predicate for determining equivalence of two elements.
|
pred = Binary predicate for determining equivalence of two elements.
|
||||||
|
@ -1655,9 +1662,11 @@ unittest
|
||||||
* Chunks an input range into subranges of equivalent adjacent elements.
|
* Chunks an input range into subranges of equivalent adjacent elements.
|
||||||
*
|
*
|
||||||
* Equivalence is defined by the predicate $(D pred), which can be either
|
* Equivalence is defined by the predicate $(D pred), which can be either
|
||||||
* binary or unary. In the binary form, two _range elements $(D a) and $(D b)
|
* binary, which is passed to $(XREF functional,binaryFun), or unary, which is
|
||||||
* are considered equivalent if $(D pred(a,b)) is true. In unary form, two
|
* passed to $(XREF functional,unaryFun). In the binary form, two _range elements
|
||||||
* elements are considered equivalent if $(D pred(a) == pred(b)) is true.
|
* $(D a) and $(D b) are considered equivalent if $(D pred(a,b)) is true. In
|
||||||
|
* unary form, two elements are considered equivalent if $(D pred(a) == pred(b))
|
||||||
|
* is true.
|
||||||
*
|
*
|
||||||
* This predicate must be an equivalence relation, that is, it must be
|
* This predicate must be an equivalence relation, that is, it must be
|
||||||
* reflexive ($(D pred(x,x)) is always true), symmetric
|
* reflexive ($(D pred(x,x)) is always true), symmetric
|
||||||
|
@ -2865,6 +2874,9 @@ Two adjacent separators are considered to surround an empty element in
|
||||||
the split range. Use $(D filter!(a => !a.empty)) on the result to compress
|
the split range. Use $(D filter!(a => !a.empty)) on the result to compress
|
||||||
empty elements.
|
empty elements.
|
||||||
|
|
||||||
|
The predicate is passed to $(XREF functional,binaryFun), and can either accept
|
||||||
|
a string, or any callable that can be executed via $(D pred(element, s)).
|
||||||
|
|
||||||
If the empty range is given, the result is a range with one empty
|
If the empty range is given, the result is a range with one empty
|
||||||
element. If a range with one separator is given, the result is a range
|
element. If a range with one separator is given, the result is a range
|
||||||
with two empty elements.
|
with two empty elements.
|
||||||
|
@ -3148,7 +3160,9 @@ if (is(typeof(binaryFun!pred(r.front, s)) : bool)
|
||||||
/**
|
/**
|
||||||
Similar to the previous overload of $(D splitter), except this one uses another
|
Similar to the previous overload of $(D splitter), except this one uses another
|
||||||
range as a separator. This can be used with any narrow string type or sliceable
|
range as a separator. This can be used with any narrow string type or sliceable
|
||||||
range type, but is most popular with string types.
|
range type, but is most popular with string types. The predicate is passed to
|
||||||
|
$(XREF functional,binaryFun), and can either accept a string, or any callable
|
||||||
|
that can be executed via $(D pred(r.front, s.front)).
|
||||||
|
|
||||||
Two adjacent separators are considered to surround an empty element in
|
Two adjacent separators are considered to surround an empty element in
|
||||||
the split range. Use $(D filter!(a => !a.empty)) on the result to compress
|
the split range. Use $(D filter!(a => !a.empty)) on the result to compress
|
||||||
|
@ -3420,6 +3434,8 @@ if (is(typeof(binaryFun!pred(r.front, s.front)) : bool)
|
||||||
|
|
||||||
Similar to the previous overload of $(D splitter), except this one does not use a separator.
|
Similar to the previous overload of $(D splitter), except this one does not use a separator.
|
||||||
Instead, the predicate is an unary function on the input range's element type.
|
Instead, the predicate is an unary function on the input range's element type.
|
||||||
|
The $(D isTerminator) predicate is passed to $(XREF functional,unaryFun) and can
|
||||||
|
either accept a string, or any callable that can be executed via $(D pred(element, s)).
|
||||||
|
|
||||||
Two adjacent separators are considered to surround an empty element in
|
Two adjacent separators are considered to surround an empty element in
|
||||||
the split range. Use $(D filter!(a => !a.empty)) on the result to compress
|
the split range. Use $(D filter!(a => !a.empty)) on the result to compress
|
||||||
|
@ -3452,16 +3468,16 @@ if (isForwardRange!Range && is(typeof(unaryFun!isTerminator(input.front))))
|
||||||
{
|
{
|
||||||
import std.algorithm.comparison : equal;
|
import std.algorithm.comparison : equal;
|
||||||
|
|
||||||
assert(equal(splitter!"a == ' '"("hello world"), [ "hello", "", "world" ]));
|
assert(equal(splitter!(a => a == ' ')("hello world"), [ "hello", "", "world" ]));
|
||||||
int[] a = [ 1, 2, 0, 0, 3, 0, 4, 5, 0 ];
|
int[] a = [ 1, 2, 0, 0, 3, 0, 4, 5, 0 ];
|
||||||
int[][] w = [ [1, 2], [], [3], [4, 5], [] ];
|
int[][] w = [ [1, 2], [], [3], [4, 5], [] ];
|
||||||
assert(equal(splitter!"a == 0"(a), w));
|
assert(equal(splitter!(a => a == 0)(a), w));
|
||||||
a = [ 0 ];
|
a = [ 0 ];
|
||||||
assert(equal(splitter!"a == 0"(a), [ (int[]).init, (int[]).init ]));
|
assert(equal(splitter!(a => a == 0)(a), [ (int[]).init, (int[]).init ]));
|
||||||
a = [ 0, 1 ];
|
a = [ 0, 1 ];
|
||||||
assert(equal(splitter!"a == 0"(a), [ [], [1] ]));
|
assert(equal(splitter!(a => a == 0)(a), [ [], [1] ]));
|
||||||
w = [ [0], [1], [2] ];
|
w = [ [0], [1], [2] ];
|
||||||
assert(equal(splitter!"a.front == 1"(w), [ [[0]], [[2]] ]));
|
assert(equal(splitter!(a => a.front == 1)(w), [ [[0]], [[2]] ]));
|
||||||
}
|
}
|
||||||
|
|
||||||
private struct SplitterResult(alias isTerminator, Range)
|
private struct SplitterResult(alias isTerminator, Range)
|
||||||
|
@ -4060,7 +4076,9 @@ unittest
|
||||||
Lazily iterates unique consecutive elements of the given range (functionality
|
Lazily iterates unique consecutive elements of the given range (functionality
|
||||||
akin to the $(WEB wikipedia.org/wiki/_Uniq, _uniq) system
|
akin to the $(WEB wikipedia.org/wiki/_Uniq, _uniq) system
|
||||||
utility). Equivalence of elements is assessed by using the predicate
|
utility). Equivalence of elements is assessed by using the predicate
|
||||||
$(D pred), by default $(D "a == b"). If the given range is
|
$(D pred), by default $(D "a == b"). The predicate is passed to
|
||||||
|
$(XREF functional,binaryFun), and can either accept a string, or any callable
|
||||||
|
that can be executed via $(D pred(element, element)). If the given range is
|
||||||
bidirectional, $(D uniq) also yields a bidirectional range.
|
bidirectional, $(D uniq) also yields a bidirectional range.
|
||||||
|
|
||||||
Params:
|
Params:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue