Syntax Parallel.run

I just watched this introduction with Dart Summit . During the conversation, this code was inserted:

=> new List.generate(100, (y) => renderLine(y));

I am sure I understand this line. The arrow function is new to me, but everything is fine - it looks like a little coffee. The fact is that this function was changed to work in parallel, which was done as follows:

=> Parallel.run(new List.generate(100, (y) => () => renderLine(y)));

Can someone explain the syntax (y) => () => renderLine(y)?

+4
source share
3 answers

(y) => () => renderLine(y)is a function that returns a function. If you write it without a transcript =>, this is the same as:

(y) {
  return () {
    return renderLine(y);
  };
}

, List.generate 100 , y. , renderLine .

( ). Parallel.run.

, , :

var tempList = [];
for (int y = 0; y < 100; y++) tempList.add(() => renderLine(y));
Parallel.run(tempList);

Parallel.run , 100 renderLine, , .

+3

(y) => () => renderLine() - , Parallel.run(), , .

, .

(x, y) => x + y; (x, y) { return x + y; }

0

( ), Parallel.run() ( iterable) no-arg . List.generate() int (length) ( int ) , int 0 .. n-1. , new List.generate(100, (y) => renderLine(y)) List 100 , renderLine() (.. renderLine(0), renderLine(1), ... , renderLine(99)).

new List.generate(100, (y) => () => renderLine(y)) 100 , , renderLine() . , : () => renderLine(0), () => renderLine(1), () => renderLine(2), ..., () => renderLine(99).

I assume that Parallel.run()then it runs all these functions in parallel, maybe it aggregates the results in the list and returns it? If so, then the code Parallel.run(new List.generate(100, (y) => () => renderLine(y)))does something similar to new List.generate(100, (y) => () => renderLine(y)), with the exception of parallel, not serial.

0
source

All Articles