Created
July 26, 2018 19:14
-
-
Save roberthoenig/0a4d4ac59c2573930b04bdbf26097880 to your computer and use it in GitHub Desktop.
Defining an iterator over the fibonacci sequence in Julia.
This file contains hidden or 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
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