MoreLinq's Scan and For Loop returns different results

111 views Asked by At

I need to compute outputs from inputs using the formula:

Output(i) = inputs(i) * factor + outputs(i - 1) * (1 - factor)

I implemented this using a for loop and MoreLinq's Scan extension:

Int32 p = 5;

Decimal factor = (Decimal) 2 / (p + 1);

List<Decimal?> inputs = Enumerable.Range(1, 40).Select(x => (Decimal?)x).ToList(); 

// Using Scan extension  

List<Decimal?> outputs1 = inputs.Scan((x, y) => x * factor + (y * (1 - factor)) ?? 0).ToList(); 

// Using for loop

List<Decimal?> outputs2 = new List<Decimal?> { inputs[0] };

for (int i = 1; i < inputs.Count(); i++) 
  outputs2.Add(inputs[i] * factor + (outputs2[i - 1] * (1 - factor)) ?? 0);

However, I get different outputs results. What am I missing?

Am I using Scan incorrectly?

1

There are 1 answers

0
Guru Stron On BEST ANSWER

You are interpreting parameters of transformation function in the wrong order (check out the source code to see how transformation is invoked). Change your code to:

inputs.Scan((x, y) => y * factor + x * (1 - factor) ?? 0).ToList()

x is aggregator, i.e. previous value, y is the current one.