You must replace Microsoft PPL with PLINQ. The code uses combinable
. I know for sure that there is an analogue in PLINQ.
1 answer
Looks like you need an Aggregate
method. An example of using combinable
, which combinable
sum of prime numbers:
combinable<int> sum; parallel_for_each(begin(a), end(a), [&](int i) { sum.local() += (is_prime(i) ? i : 0); }); prime_sum = sum.combine(plus<int>());
Using Aggregate
looks like this:
var primeSum = a.AsParallel().Aggregate( // ΠΈΠ½ΠΈΡΠΈΠ°Π»ΠΈΠ·ΠΈΡΡΠ΅ΠΌ Π½Π°ΡΠ°Π»ΡΠ½ΠΎΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ 0, // Π²ΡΠΏΠΎΠ»Π½ΡΠ΅ΠΌ ΡΡΠΌΠΌΠΈΡΠΎΠ²Π°Π½ΠΈΠ΅ ΠΏΠ°ΡΠ°Π»Π»Π΅Π»ΡΠ½ΠΎ (Π°Π½Π°Π»ΠΎΠ³ local()) (subtotal, item) => subtotal + (IsPrime(item) ? item : 0), // ΠΏΠΎΠ»ΡΡΠ°Π΅ΠΌ ΠΎΠ±ΡΡΡ ΡΡΠΌΠΌΡ (Π°Π½Π°Π»ΠΎΠ³ combine()) (total, subtotal) => total + subtotal, // ΠΎΡΠ΄Π°Π΅ΠΌ ΡΡΠΌΠΌΡ Π±Π΅Π· ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΠΉ (final) => final);
- The person who gave me the assignment said that it was not an aggregate. those. through the aggregate, the same is possible and your answer is definitely true, but there is still something that βconsists of two wordsβ (this is the only thing he remembered). - user3239600
- @ user3239600 in principle, the same can be achieved using
Parallel.ForEach
. But what's the guessing game to play? - andreycha - @ user3239600: With us here, you see, usually help solve a programming problem, and not read the thoughts of another person. - VladD
|