Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save simpleprogrammer-shared/35634599cf7b0e3460af7ab1e12e8e1c to your computer and use it in GitHub Desktop.
Save simpleprogrammer-shared/35634599cf7b0e3460af7ab1e12e8e1c to your computer and use it in GitHub Desktop.
Lambda Extensible FizzBuzz 2.0 With LINQ
public static void FizzBuzz()
{
Dictionary<Func<int, bool>, Func<int, string>> rules = new Dictionary<Func<int, bool>, Func<int, string>>();
rules.Add(x => x % 3 == 0, x => "fizz");
rules.Add(x => x % 5 == 0, x => "buzz");
rules.Add(x => x % 5 != 0 && x % 3 != 0, x => x.ToString());
rules.Add(x => true, x => "\n");
var output = from n in Enumerable.Range(1, 100)
from f in rules
where f.Key(n)
select f.Value(n);
output.ToList().ForEach(x => Console.Write(x));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment