Skip to content

Commit

Permalink
Merge pull request #16 from crstnbr/iter
Browse files Browse the repository at this point in the history
Julia 1.0 support: new iterator interface
  • Loading branch information
carstenbauer authored Sep 8, 2018
2 parents c970b79 + 9c15e57 commit e067766
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ os:
julia:
- 0.6
- 0.7
- 1.0
- nightly
notifications:
email: false
Expand All @@ -14,6 +15,9 @@ git:

script:
- julia -e 'Pkg.clone(pwd()); Pkg.build("PeriodicTable"); Pkg.test("PeriodicTable"; coverage=true)'
matrix:
allow_failures:
- julia: nightly
after_success:
# push coverage results to Coveralls
- julia -e 'cd(Pkg.dir("PeriodicTable")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder())'
Expand Down
10 changes: 7 additions & 3 deletions src/PeriodicTable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,13 @@ Base.get(e::Elements, i::Symbol, default) = get(e.bysymbol, i, default)
# support iterating over elements
Base.eltype(e::Elements) = Element
Base.length(e::Elements) = length(e.data)
Base.start(e::Elements) = start(e.data)
Base.next(e::Elements, i) = next(e.data, i)
Base.done(e::Elements, i) = done(e.data, i)
if VERSION < v"0.7-"
Base.start(e::Elements) = start(e.data)
Base.next(e::Elements, i) = next(e.data, i)
Base.done(e::Elements, i) = done(e.data, i)
else
Base.iterate(e::Elements, state...) = iterate(e.data, state...)
end

# compact one-line printing
Base.show(io::IO, e::Elements) = print(io, "Elements(…", length(e), " elements…)")
Expand Down
11 changes: 11 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ F = elements[9]
@test O.symbol == "O"
@test nfields(O) == 21

# iteration protocol
if VERSION < v"0.7-"
@test start(elements) == 1
@test next(elements, 1) == (elements[:H], 2)
@test done(elements, length(elements)+1)
else
@test iterate(elements) == (elements[:H], 2)
@test iterate(elements, 4) == (elements[:Be], 5)
@test iterate(elements, length(elements)+1) == nothing
end

# 2-argument show functions
@test repr(elements) == "Elements(…119 elements…)"
if VERSION < v"0.7-"
Expand Down

0 comments on commit e067766

Please sign in to comment.