Created
July 14, 2016 20:13
-
-
Save simpleprogrammer-shared/35634599cf7b0e3460af7ab1e12e8e1c to your computer and use it in GitHub Desktop.
Lambda Extensible FizzBuzz 2.0 With LINQ
This file contains hidden or 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
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