Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Functional FizzBuzz
Func<int, string> fizzBuzz = i =>
{
if (i % 5 == 0 && i % 3 == 0) return "FizzBuzz";
else if (i % 3 == 0) return "Fizz";
else if (i % 5 == 0) return "Buzz";
return i.ToString();
};
Func<StringBuilder, string, StringBuilder> reduce = (sb, s) => sb.AppendLine(s);
var message = System.Linq.Enumerable.Range(1, 100).Select(fizzBuzz).Aggregate (new StringBuilder(), reduce).ToString();
Console.WriteLine(message);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment