Created
March 5, 2020 22:18
-
-
Save FrankKerrigan/64b24b3c98023f8438f57947d04c199e to your computer and use it in GitHub Desktop.
FizzBuzz using Yield, Func and IEnumerable
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
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