Created
December 18, 2019 15:44
-
-
Save ythirion/b71a02a5732f18ef006872b03ad3a074 to your computer and use it in GitHub Desktop.
Fold vs reduce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Fact] | |
public void fold_vs_reduce() | |
{ | |
//fold takes an explicit initial value for the accumulator | |
//Can choose the result type | |
var foldResult = List(1, 2, 3, 4, 5) | |
.Map(x => x * 10) | |
.Fold(0m, (x, s) => s + x); // 150m | |
//reduce uses the first element of the input list as the initial accumulator value | |
//Result type will be the one of the list | |
var reduceResult = List(1, 2, 3, 4, 5) | |
.Map(x => x * 10) | |
.Reduce((x, s) => s + x); // 150 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment