Last active
November 26, 2019 19:54
-
-
Save Kralizek/8ac7a5a8a17ecf0f8eb0f0dd48428730 to your computer and use it in GitHub Desktop.
FizBuz using pattern matching and LINQ
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
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