Last active
December 21, 2015 21:19
-
-
Save rdurham1007/6367506 to your computer and use it in GitHub Desktop.
FizzBuzz using a rules dictionary and linq
This file contains 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
namespace FizzBuzz | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var rules = new Dictionary<Func<int, bool>, Func<int, string>> | |
{ | |
{x => x%3 == 0, x => "Fizz"}, | |
{x => x%5 == 0, x => "Buzz"}, | |
{x => x%3 != 0 && x%5 != 0, x => x.ToString()}, | |
{x => true, x => Environment.NewLine } | |
}; | |
Enumerable.Range(1,100) | |
.ToList() | |
.ForEach(i => rules.Where(r => r.Key(i)) | |
.Select(s => s.Value(i)).ToList() | |
.ForEach(Console.Write)); | |
Console.ReadKey(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment