mirror of
https://github.com/dlang/phobos.git
synced 2025-04-28 22:21:09 +03:00
Add separate example for each
with index
Remove doc-comment for example that wasn't as clear.
This commit is contained in:
parent
1af0e4d5e9
commit
18a647bed4
1 changed files with 11 additions and 5 deletions
|
@ -1122,11 +1122,18 @@ public:
|
|||
// The default predicate consumes the range
|
||||
(&m).each();
|
||||
assert(m.empty);
|
||||
}
|
||||
|
||||
/// `each` can pass an index variable for iterable objects which support this
|
||||
@safe unittest
|
||||
{
|
||||
auto arr = new int[4];
|
||||
|
||||
// Indexes are also available for in-place mutations
|
||||
arr[] = 0;
|
||||
arr.each!"a=i"();
|
||||
assert(arr == [0, 1, 2, 3, 4, 5]);
|
||||
assert(arr == [0, 1, 2, 3]);
|
||||
|
||||
arr.each!((i, ref e) => e = cast(int) i * 2);
|
||||
assert(arr == [0, 2, 4, 6]);
|
||||
}
|
||||
|
||||
/// opApply iterators work as well
|
||||
|
@ -1225,10 +1232,9 @@ public:
|
|||
|
||||
// https://issues.dlang.org/show_bug.cgi?id=15357
|
||||
// `each` should behave similar to foreach
|
||||
/// `each` works with iterable objects which provide an index variable, along with each element
|
||||
@safe unittest
|
||||
{
|
||||
import std.range : iota, lockstep;
|
||||
import std.range : iota;
|
||||
|
||||
auto arr = [1, 2, 3, 4];
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue