Skip to content

Instantly share code, notes, and snippets.

@ythirion
Created December 18, 2019 15:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ythirion/b71a02a5732f18ef006872b03ad3a074 to your computer and use it in GitHub Desktop.
Save ythirion/b71a02a5732f18ef006872b03ad3a074 to your computer and use it in GitHub Desktop.
Fold vs reduce
[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