Last active
July 14, 2020 18:10
-
-
Save sunaku/ea8c5818010308c86f16 to your computer and use it in GitHub Desktop.
A functional FizzBuzz (without any integer modulus or division) in Elixir. See https://pragprog.com/magazines/2012-08/thinking-functionally-with-haskell
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
# A functional FizzBuzz (without any integer modulus or division) in Elixir | |
# https://pragprog.com/magazines/2012-08/thinking-functionally-with-haskell | |
nums = Stream.iterate(1, &(&1 + 1)) | |
fizz = Stream.cycle ["", "", "Fizz"] | |
buzz = Stream.cycle ["", "", "", "", "Buzz"] | |
fizzbuzz = Stream.zip(fizz, buzz) |> Stream.zip(nums) |> Stream.map(fn | |
{{"", "" }, number} -> number | |
{{fizzword, buzzword}, _number} -> fizzword <> buzzword | |
end) | |
fizzbuzz |> Stream.take(100) |> Enum.each(&IO.puts/1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this is pretty cool.