Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iterator interface #8

Open
tpapp opened this issue Oct 12, 2016 · 5 comments
Open

iterator interface #8

tpapp opened this issue Oct 12, 2016 · 5 comments

Comments

@tpapp
Copy link

tpapp commented Oct 12, 2016

Thanks for this great library, I find it very useful. However, I am not sure that the library supports the iteration interface of Julia. Eg Sobol.SobolSeq returns not an iterator, but an iterator and a state. So, for example,

import Sobol

function collect_take(iter, n)
    v = Vector{Float64}(0)
    for (i, x) in enumerate(iter)
        push!(v, x[1])
        if (i >= n)
            break
        end
    end
    v
end
double_take(iter, n) = vcat(collect_take(iter,5), collect_take(iter,5))
double_take(Sobol.SobolSeq(1), 5)

returns the first 10 elements (instead of the first 5, twice). Cf, for example,

double_take(countfrom(1),5)

Also, idioms like

[x[1] for x in take(Sobol.SobolSeq(1),5)]

don't work.

@stevengj
Copy link
Member

In this case, the iterator is the state; it's not so easy to separate the two. Iterating twice on the same sobolseq object will given different numbers.

[x[1] for x in take(Sobol.SobolSeq(1),5)] not working seems to be a bug in take: I get ``MethodError: no method matching length(::Sobol.SobolSeq{1}), which means that take` only works on the subset of iterators supporting `length`. This seems like it should be fixed.

@stevengj
Copy link
Member

Oh, looks like I need to define iteratorsize and some other optional methods.

@stevengj
Copy link
Member

Seems like I got bit by JuliaLang/julia#15977.

stevengj added a commit that referenced this issue Oct 12, 2016
@stevengj
Copy link
Member

(Note that the take example now works.)

@tpapp
Copy link
Author

tpapp commented Jan 24, 2019

If supporting iterate and functional style is a goal, then it could be done the following way:

  1. SobolSeq{N} has no fields, just stores N,
  2. x, state = iterate(::SobolSeq) allocates and returns in state what are now the fields of SobolSeq,
  3. iterate(::SobolSeq, state) just updates state in place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants