Skip to content

Instantly share code, notes, and snippets.

@JustinJohnWilliams
Last active April 20, 2018 20:31
Show Gist options
  • Save JustinJohnWilliams/3791622 to your computer and use it in GitHub Desktop.
Save JustinJohnWilliams/3791622 to your computer and use it in GitHub Desktop.
FizzBuzz with Linq and Lambdas
var 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 % 15 != 0, x => x.ToString());
rules.Add(x => true, x => "\n");
var results = (from n in Enumerable.Range(1, 100)
from r in rules
where r.Key(n)
select r.Value(n));
results.ToList().ForEach(c => Console.Write(c));
Console.ReadLine();
@JustinJohnWilliams
Copy link
Author

aren't you a little smarty pants

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment