Skip to content

Instantly share code, notes, and snippets.

@rdurham1007
Last active December 21, 2015 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rdurham1007/6367506 to your computer and use it in GitHub Desktop.
Save rdurham1007/6367506 to your computer and use it in GitHub Desktop.
FizzBuzz using a rules dictionary and linq
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