Given an iterator (for example from the ‘Interfaces’ doc).
s = Squares(9)
collect(s) # [1, 4, 9, 16, 25, 36, 49, 64, 81]
Now given a StepRange
R = 1:2:11 # 1, 3, 5, 7, 9, 11
how can I restrict the iteration over the Squares to the values of R to get the squares restricted to R:
[1, 9, 25, 49, 81]
Neither collect(R, s) nor collect(s, R) works, somewhat surprisingly.
More generally, how can I restrict an iteration to an iteration respecting a given StepRange?
Edit: Steps of the form 0:2:n seems to be a special problem. Both suggestions below lead to errors in this case. Here is a minimal example of what I expect a RangeIterator to do:
struct Squares count::Int end
Base.iterate(S::Squares, state=0) = state > S.count ? nothing : (state*state, state+1)
Base.length(S::Squares) = S.count
EvenBisectSquares(n) = RangeIterator(Squares(n), 0:2:n)
OddBisectSquares(n) = RangeIterator(Squares(n), 1:2:n)
EvenBisectSquares(9) |> println ∘ collect # [0, 4, 16, 36, 64]
OddBisectSquares(9) |> println ∘ collect # [1, 9, 25, 49, 81]