Skip to content

Instantly share code, notes, and snippets.

@Kralizek
Last active November 26, 2019 19:54
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 Kralizek/8ac7a5a8a17ecf0f8eb0f0dd48428730 to your computer and use it in GitHub Desktop.
Save Kralizek/8ac7a5a8a17ecf0f8eb0f0dd48428730 to your computer and use it in GitHub Desktop.
FizBuz using pattern matching and LINQ
Enumerable.Range(1, 100)
.Select(CheckModulo)
.Select(FizBuz)
.ToList()
.ForEach(Console.WriteLine);
static (bool isFizz, bool isBuzz, int n) CheckModulo(int n)
{
return (n % 3 == 0, n % 5 == 0, n);
}
static string FizBuz((bool isFizz, bool isbuzz, int n) item)
{
string result = "";
if (item is (true, _, _)) result += "Fiz";
if (item is (_, true, _)) result += "Buz";
if (item is (false, false, var v)) result = $"{v}";
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment