Scan2

Applies the folder function to each pair of elements of the collections, threading an accumulator argument through the computation.

Take the second argument, and apply the function to it and the first pair of elements of the collections. Then feed this result into the function along with the second pair of elements and so on.

Returns the collection of intermediate results and the final result.

This method is similar to Fold2, but in this case the intermediate results are returned as well.

Parameters

Returns

Func<TState, T, T2, TState> folder

TState state

IEnumerable<T> source

IEnumerable<T2> source2

TState

Usage

Accumulating an individual property of each element through a collection

//IEnumerable<int> source = { 1, 2, 3 }
//IEnumerable<int> source2 = { 3, 2, 1 }

int result =
source.Scan2(
        source2,
        12,
        (_state, element1, element2) =>
        _state + Math.Max(element1, element2) );

//result = {15, 17, 20 }

When the type of elements in your collection are: int, decimal, double, string or bool you can also use this function combined with the Operations described in operations section as folder functions.

Using an operation as a folder

//IEnumerable<int> source = { 2, 3, 5, 0 }
//IEnumerable<int> source2 = { 3, 2, 0, 5 }

source.Scan2(source2, 30, IntegerOperations.Add3);

//result = { 35, 40, 45, 50}

Last updated