Skip to content

Instantly share code, notes, and snippets.

@jakobjpeters
Last active January 15, 2024 14:49
Show Gist options
  • Save jakobjpeters/c58e87df98008baebcbcac55a569c84b to your computer and use it in GitHub Desktop.
Save jakobjpeters/c58e87df98008baebcbcac55a569c84b to your computer and use it in GitHub Desktop.
Fizz Buzz with lazy iterators
#=
Adapted from:
Kevlin Henney
https://www.youtube.com/watch?v=nrVIlhtoE3Y
=#
julia> fizz_buzz(words...) = Iterators.map(max,
Iterators.map(string, Iterators.countfrom()),
Iterators.map(*, Iterators.map(
((s, n),) -> Iterators.cycle(push!(repeat([""], n - 1), s)),
words)...)
);
julia> foreach(println, Iterators.take(fizz_buzz("Fizz" => 3, "Buzz" => 5), 15))
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment