Skip to content

Instantly share code, notes, and snippets.

@antimon2
Last active August 8, 2018 01:01
Show Gist options
  • Save antimon2/1fda74ae88f9178e4bdf5fa784e253aa to your computer and use it in GitHub Desktop.
Save antimon2/1fda74ae88f9178e4bdf5fa784e253aa to your computer and use it in GitHub Desktop.
Julia 0.7-DEV の新しい Iteration に触れてみた。 ref: https://qiita.com/antimon2/items/8b1a96d1370bb6252757
for cw in Zundoko()
print(cw)
end
iter = Zundoko()
state = start(iter)
while !done(iter, state)
(cw, state) = next(iter, state)
print(cw)
end
iter = Zundoko()
_next = iterate(iter)
while _next !== nothing
(cw, state) = _next
print(cw)
_next = iterate(iter, state)
end
for line in iter
println(line)
end
_next = iterate(iter)
while _next !== nothing
(line, state) = _next
println(line)
_next = iterate(iter, state)
end
state = start(iter)
while !done(iter, state)
(line, state) = next(iter, state)
println(line)
end
struct ZunDoko end
Base.start(::ZunDoko) = 0
Base.done(::ZunDoko, i::Int) = i == 6
function Base.next(::ZunDoko, i::Int)
if i == 5
("キ・ヨ・シ!", 6)
elseif i == 4
rand([("ズン", 4), ("ドコ", 5)])
else
rand([("ズン", i + 1), ("ドコ", 0)])
end
end
struct ZunDoko end
function Base.iterate(::ZunDoko, i::Int = 0)
i == 6 && return nothing
if i == 5
("キ・ヨ・シ!", 6)
elseif i == 4
rand([("ズン", 4), ("ドコ", 5)])
else
rand([("ズン", i + 1), ("ドコ", 0)])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment