Skip to content

Instantly share code, notes, and snippets.

@sunaku
Last active July 14, 2020 18:10
Show Gist options
  • Save sunaku/ea8c5818010308c86f16 to your computer and use it in GitHub Desktop.
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
# 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)
@elbow-jason
Copy link

this is pretty cool.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment