Skip to content

Instantly share code, notes, and snippets.

@FrankKerrigan
Created March 5, 2020 22:18
Show Gist options
  • Save FrankKerrigan/64b24b3c98023f8438f57947d04c199e to your computer and use it in GitHub Desktop.
Save FrankKerrigan/64b24b3c98023f8438f57947d04c199e to your computer and use it in GitHub Desktop.
FizzBuzz using Yield, Func and IEnumerable
class Program
{
static void Main(string[] args)
{
foreach (var i in FizzBuzz(100))
{
Console.WriteLine(i);
}
}
private static IEnumerable<string> FizzBuzz(int maxvalue)
{
int count = 0;
//yield return count.ToString();
Func<int, string> FizzBuzz = (x) => ((x % 5 == 0 && x % 3 == 0) ? "FizzBuzz" : null);
Func<int, string> Buzz = (x) => ((x % 5 == 0) ? "Buzz" : null);
Func<int, string> Fizz = (x) => ((x % 3 == 0) ? "Fizz" : null);
Func<int, string> Number = (x) => x.ToString();
while (count < maxvalue)
{
count++;
yield return FizzBuzz(count) ?? Buzz(count) ?? Fizz(count) ?? Number(count);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment