mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 17:41:41 +00:00
Resolving conflict in #1710
This commit is contained in:
parent
49c04a01c2
commit
0737d9be0b
@ -218,7 +218,7 @@ void main() {
|
|||||||
// from 1 to 100. Easy!
|
// from 1 to 100. Easy!
|
||||||
|
|
||||||
// Just pass lambda expressions as template parameters!
|
// Just pass lambda expressions as template parameters!
|
||||||
// You can pass any old function you like, but lambdas are convenient here.
|
// You can pass any function you like, but lambdas are convenient here.
|
||||||
auto num = iota(1, 101).filter!(x => x % 2 == 0)
|
auto num = iota(1, 101).filter!(x => x % 2 == 0)
|
||||||
.map!(y => y ^^ 2)
|
.map!(y => y ^^ 2)
|
||||||
.reduce!((a, b) => a + b);
|
.reduce!((a, b) => a + b);
|
||||||
@ -228,7 +228,7 @@ void main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
Notice how we got to build a nice Haskellian pipeline to compute num?
|
Notice how we got to build a nice Haskellian pipeline to compute num?
|
||||||
That's thanks to a D innovation know as Uniform Function Call Syntax.
|
That's thanks to a D innovation know as Uniform Function Call Syntax (UFCS).
|
||||||
With UFCS, we can choose whether to write a function call as a method
|
With UFCS, we can choose whether to write a function call as a method
|
||||||
or free function call! Walter wrote a nice article on this
|
or free function call! Walter wrote a nice article on this
|
||||||
[here.](http://www.drdobbs.com/cpp/uniform-function-call-syntax/232700394)
|
[here.](http://www.drdobbs.com/cpp/uniform-function-call-syntax/232700394)
|
||||||
@ -238,21 +238,23 @@ is of some type A on any expression of type A as a method.
|
|||||||
I like parallelism. Anyone else like parallelism? Sure you do. Let's do some!
|
I like parallelism. Anyone else like parallelism? Sure you do. Let's do some!
|
||||||
|
|
||||||
```c
|
```c
|
||||||
|
// Let's say we want to populate a large array with the square root of all
|
||||||
|
// consecutive integers starting from 1 (up until the size of the array), and we
|
||||||
|
// want to do this concurrently taking advantage of as many cores as we have
|
||||||
|
// available.
|
||||||
|
|
||||||
import std.stdio;
|
import std.stdio;
|
||||||
import std.parallelism : parallel;
|
import std.parallelism : parallel;
|
||||||
import std.math : sqrt;
|
import std.math : sqrt;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
// We want take the square root every number in our array,
|
// Create your large array
|
||||||
// and take advantage of as many cores as we have available.
|
|
||||||
auto arr = new double[1_000_000];
|
auto arr = new double[1_000_000];
|
||||||
|
|
||||||
// Use an index, and an array element by reference,
|
// Use an index, access every array element by reference (because we're
|
||||||
// and just call parallel on the array!
|
// going to change each element) and just call parallel on the array!
|
||||||
foreach(i, ref elem; parallel(arr)) {
|
foreach(i, ref elem; parallel(arr)) {
|
||||||
ref = sqrt(i + 1.0);
|
ref = sqrt(i + 1.0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
```
|
```
|
||||||
|
Loading…
Reference in New Issue
Block a user