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