Fold2
Applies a function to corresponding elements of two collections, threading an accumulator argument through the computation.
Parameters
Returns
Func<TState, T, T2, TState> folder
TState state
IEnumerable<T> source
IEnumerable<T2> source2
TState
Usage
Accumulating the bigger value in the same position of these two collections
//IEnumerable<int> source = { 1, 2, 3 }
//IEnumerable<int> source2 = { 3, 2, 1 }
int result =
source.Fold2(
source2,
12,
(_state, element1, element2) =>
_state + Math.Max(element1, element2) );
//result = 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.Fold2(source2, 30, IntegerOperations.Add3);
//result = 50
Last updated
Was this helpful?