Skip to content

Instantly share code, notes, and snippets.

@latticetower
Created April 28, 2016 19:22
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 latticetower/a6859b7226e5d73a5b01a516ba9d5e78 to your computer and use it in GitHub Desktop.
Save latticetower/a6859b7226e5d73a5b01a516ba9d5e78 to your computer and use it in GitHub Desktop.
for i in Sequence([(1, 2, 3, 4, 5), (6, 5, 4, 3), (-3, -3, -3 )])
println(i)
end
immutable Sequence
iter :: Any
end
Base.start(s :: Sequence) = (1, 1, 0)
#this is helper function, returns pair of states for iterator s1, s2, and inner_iter (inner list iterator)
function skip_to_next(iter, s1, s2)
(inner_iter, s1_) = next(iter, s1)
while done(inner_iter, s2)
s1 = s1_
if done(iter, s1)
break
end
(inner_iter, s1_) = next(iter, s1)
s2 = start(inner_iter)
end
(s1, s2, inner_iter)
end
function Base.done(s :: Sequence, state)
(s1, s2, _) = state
if done(s.iter, s1)
true
else
(s1, s2, inner_iter) = skip_to_next(s.iter, s1, s2)
done(inner_iter, s2)
end
end
function Base.next(s :: Sequence, state)
(s1, s2, l) = state
(inner_iter, s1_) = next(s.iter, s1)
if done(inner_iter, s2)
(s1, s2, inner_iter) = skip_to_next(s.iter, s1, s2)
(el, _) = next(inner_iter, s2)
return ((el+l)/2.0, (s1, s2, el))
end
(el, s2) = next(inner_iter, s2)
(el, (s1, s2, el))
end
Base.eltype(:: Type{Sequence}) = Int
Base.length(s :: Sequence) = sum(map(x->length(x)+1, s.iter)) - 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment