Skip to content

Instantly share code, notes, and snippets.

@miklund
Last active May 16, 2018 13:39
Show Gist options
  • Save miklund/67e78ac70eb935164411cc12ee39aa22 to your computer and use it in GitHub Desktop.
Save miklund/67e78ac70eb935164411cc12ee39aa22 to your computer and use it in GitHub Desktop.
FizzBuzz
public IEnumerable<string> GetFizzBuzz(int max)
{
var result = new List<string>();
for (int number = 1; number <= max; number++)
{
if (number % 15 == 0)
{
result.Add("fizzbuzz");
}
else if (number % 3 == 0)
{
result.Add("fizz");
}
else if (number % 5 == 0)
{
result.Add("buzz");
}
else
{
result.Add(number.ToString());
}
}
return result;
}
// sequence where every third is fizz
let fizzes = cycle [""; ""; "fizz"]
// sequence where every fifth is buzz
let buzzes = cycle [""; ""; ""; ""; "buzz"]
// combined sequence of fizzes and buzzes
let words = Seq.map2 (+) fizzes buzzes
// sequence of numbers starting with "1"
let numbers = Seq.initInfinite show |> Seq.skip 1
// pick the largest string
let choice = max
// combined words and numbers, pick the largest string
let fizzbuzz = Seq.zip words numbers |> Seq.map choice
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment