mirror of
https://github.com/dlang/phobos.git
synced 2025-04-29 22:50:38 +03:00
Use Lambdas and UFCS in std.algorithm examples.
Better represent Lambda syntax and UFCS in the map/filter/reduce examples in std.algorithm.
This commit is contained in:
parent
29e043d461
commit
c45e75a97a
1 changed files with 26 additions and 7 deletions
|
@ -348,7 +348,7 @@ Example:
|
|||
----
|
||||
int[] arr1 = [ 1, 2, 3, 4 ];
|
||||
int[] arr2 = [ 5, 6 ];
|
||||
auto squares = map!("a * a")(chain(arr1, arr2));
|
||||
auto squares = map!(a => a * a)(chain(arr1, arr2));
|
||||
assert(equal(squares, [ 1, 4, 9, 16, 25, 36 ]));
|
||||
----
|
||||
|
||||
|
@ -617,19 +617,27 @@ Example:
|
|||
----
|
||||
int[] arr = [ 1, 2, 3, 4, 5 ];
|
||||
// Sum all elements
|
||||
auto sum = reduce!("a + b")(0, arr);
|
||||
auto sum = reduce!((a,b) => a + b)(0, arr);
|
||||
assert(sum == 15);
|
||||
|
||||
// Sum again, using a string predicate with "a" and "b"
|
||||
sum = reduce!"a + b"(0, arr);
|
||||
assert(sum == 15);
|
||||
|
||||
// Compute the maximum of all elements
|
||||
auto largest = reduce!(max)(arr);
|
||||
assert(largest == 5);
|
||||
|
||||
// Max again, but with Uniform Function Call Syntax (UFCS)
|
||||
largest = arr.reduce!(max);
|
||||
assert(largest == 5);
|
||||
|
||||
// Compute the number of odd elements
|
||||
auto odds = reduce!("a + (b & 1)")(0, arr);
|
||||
auto odds = reduce!((a,b) => a + (b & 1))(0, arr);
|
||||
assert(odds == 3);
|
||||
|
||||
// Compute the sum of squares
|
||||
auto ssquares = reduce!("a + b * b")(0, arr);
|
||||
auto ssquares = reduce!((a,b) => a + b * b)(0, arr);
|
||||
assert(ssquares == 55);
|
||||
|
||||
// Chain multiple ranges into seed
|
||||
|
@ -642,6 +650,10 @@ assert(r == 107);
|
|||
double[] c = [ 2.5, 3.0 ];
|
||||
auto r1 = reduce!("a + b")(chain(a, b, c));
|
||||
assert(r1 == 112.5);
|
||||
|
||||
// To minimize nesting of parentheses, Uniform Function Call Syntax can be used
|
||||
auto r2 = chain(a, b, c).reduce!("a + b");
|
||||
assert(r2 == 112.5);
|
||||
----
|
||||
|
||||
$(DDOC_SECTION_H Multiple functions:) Sometimes it is very useful to
|
||||
|
@ -1255,17 +1267,24 @@ which $(D predicate(x)) is $(D true).
|
|||
Example:
|
||||
----
|
||||
int[] arr = [ 1, 2, 3, 4, 5 ];
|
||||
|
||||
// Sum all elements
|
||||
auto small = filter!("a < 3")(arr);
|
||||
auto small = filter!(a => a < 3)(arr);
|
||||
assert(equal(small, [ 1, 2 ]));
|
||||
|
||||
// Sum again, but with Uniform Function Call Syntax (UFCS)
|
||||
auto sum = arr.filter!(a => a < 3);
|
||||
assert(equal(sum, [ 1, 2 ]));
|
||||
|
||||
// In combination with chain() to span multiple ranges
|
||||
int[] a = [ 3, -2, 400 ];
|
||||
int[] b = [ 100, -101, 102 ];
|
||||
auto r = filter!("a > 0")(chain(a, b));
|
||||
auto r = chain(a, b).filter!(a => a > 0);
|
||||
assert(equal(r, [ 3, 400, 100, 102 ]));
|
||||
|
||||
// Mixing convertible types is fair game, too
|
||||
double[] c = [ 2.5, 3.0 ];
|
||||
auto r1 = filter!("cast(int) a != a")(chain(c, a, b));
|
||||
auto r1 = chain(c, a, b).filter!(a => cast(int) a != a);
|
||||
assert(equal(r1, [ 2.5 ]));
|
||||
----
|
||||
*/
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue