i have a list of double values...
1.23, 1.24, 1.78, 1,74...
so i want to calculate the differences between the successor -> only adding (negative values shou
I'm not sure what you mean about the negative bits, but this might do what you want. It's horrible because it uses side effects, but...
double prev = 0d;
var differences = list.Select(current =>
{
double diff = prev - current;
prev = current;
return Math.Abs(diff);
}).Skip(1);
(The first value is skipped because it just gives the difference between the first original value and 0d.)
EDIT: What might be slightly nicer would be an extension method to project based on pairs of elements. This isolates the side-effects in one place, which is fine:
using System.Collections.Generic;
// This must be a non-nested type, and must be static to allow the extension
// method.
public static class Extensions
{
public static IEnumerable SelectPairs
(this IEnumerable source,
Func selector)
{
using (IEnumerator iterator = source.GetEnumerator())
{
if (!iterator.MoveNext())
{
yield break;
}
TSource prev = iterator.Current;
while (iterator.MoveNext())
{
TSource current = iterator.Current;
yield return selector(prev, current);
prev = current;
}
}
}
}
To use this in your particular case, you'd do:
var differences = list.SelectPairs((x, y) => Math.Abs(x-y));