Skip to content

Instantly share code, notes, and snippets.

@roberthoenig
Created July 26, 2018 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roberthoenig/0a4d4ac59c2573930b04bdbf26097880 to your computer and use it in GitHub Desktop.
Save roberthoenig/0a4d4ac59c2573930b04bdbf26097880 to your computer and use it in GitHub Desktop.
Defining an iterator over the fibonacci sequence in Julia.
struct Fibonacci{I}
first::I
second::I
end
function Base.iterate(iter::Fibonacci, state=nothing)
if state == nothing
iter.first, (iter.first, nothing)
elseif state[2] == nothing
iter.second, (iter.first, iter.second)
else
sum(state), (state[2], sum(state))
end
end
for num in Fibonacci(0, 1)
println(num)
sleep(1)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment