Is there a way to pipeline program with random-access ranges in C#?

Dukc ajieskola at gmail.com
Tue Mar 20 15:06:14 UTC 2018


On Tuesday, 20 March 2018 at 08:05:14 UTC, Kagamin wrote:
> On Monday, 19 March 2018 at 17:33:31 UTC, Dukc wrote:
>> public static int Foo(int[] input)
>> {   int result = 10;
>>     for (int i = input.Length / 4; i >= 0; i -= 4)
>>     {   int sum = 0;
>>         for (int j = i; j < i +4 && j < input.Length; j++) sum 
>> += input[j];
>>         sum *= i;
>>         result = (result + sum) / 2;
>>     }
>>     return result;
>> }
>
> Looks like you need to partition, select, aggregate and another 
> aggregate.

Won't quite do it, because that would not iterate backwards.

But anyway, I made this extension function today which solves 
most of my problems, albeit not that one above:

public static IEnumerable<Sequence<T, int>> Enumerate<T>(this 
IEnumerable<T> range)
{   return range.Zip(Enumerable.Range(0, int.MaxValue), (x, y) => 
new Sequence<T, int>(x, y));
}

(Of course, were I compiling to .net framework instead of 
JavaScript I would have to use Tuple or ValueTuple instead of 
Sequence)


More information about the Digitalmars-d-learn mailing list