Created
May 10, 2014 20:10
-
-
Save MikeMKH/b9d8dff2b290ff53e23b to your computer and use it in GitHub Desktop.
FizzBuzz kata in C# using LINQ and a helper class to how the translation rules.
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace FizzBuzz | |
{ | |
public class FizzBuzzer | |
{ | |
public string Translate(int value) | |
{ | |
var result = new List<Translator> | |
{ | |
new Translator(() => value%3 == 0, "Fizz"), | |
new Translator(() => value%5 == 0, "Buzz") | |
}.Aggregate(string.Empty, (s, t) => s += t.Translate()); | |
return string.IsNullOrEmpty(result) ? value.ToString() : result; | |
} | |
class Translator | |
{ | |
Func<bool> Test { get; set; } | |
string Translation { get; set; } | |
public Translator(Func<bool> test, string translation) | |
{ | |
Test = test; | |
Translation = translation; | |
} | |
public string Translate() | |
{ | |
return Test() ? Translation : string.Empty; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See also my blog post which goes with this gist.
http://comp-phil.blogspot.com/2014/05/the-form-of-katas.html