Skip to content

Instantly share code, notes, and snippets.

@MikeMKH
Created May 10, 2014 20:10
Show Gist options
  • Save MikeMKH/b9d8dff2b290ff53e23b to your computer and use it in GitHub Desktop.
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.
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;
}
}
}
}
@MikeMKH
Copy link
Author

MikeMKH commented May 10, 2014

See also my blog post which goes with this gist.
http://comp-phil.blogspot.com/2014/05/the-form-of-katas.html

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