Skip to content

Commit c0d1fc5

Browse files
committed
simplify iterator logic
1 parent cea2295 commit c0d1fc5

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

src/Primes.jl

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -675,11 +675,9 @@ struct NextPrimes{T<:Integer}
675675
start::T
676676
end
677677

678-
function iterate(np::NextPrimes,
679-
state = p = np.start < 2 ? np.start : add(np.start, -1)
680-
)
681-
p = nextprime(add(state, 1))
682-
(p, p)
678+
function iterate(np::NextPrimes, state=np.start)
679+
p = nextprime(state)
680+
(p, add(p, 1))
683681
end
684682

685683
IteratorSize(::Type{<:NextPrimes}) = Base.IsInfinite()
@@ -725,17 +723,13 @@ struct PrevPrimes{T<:Integer}
725723
start::T
726724
end
727725

728-
function iterate(np::PrevPrimes,
729-
state = np.start+one(np.start) # allow wrap-around
730-
)
731-
c = state-one(state)
732-
if isone(c)
726+
iterate(np::PrevPrimes, state=np.start) =
727+
if isone(state)
733728
nothing
734729
else
735-
p = prevprime(c)
736-
(p, p)
730+
p = prevprime(state)
731+
(p, p-one(p))
737732
end
738-
end
739733

740734
IteratorSize(::Type{<:PrevPrimes}) = Base.SizeUnknown()
741735
Iteratoreltype(::Type{<:PrevPrimes}) = Base.HasEltype()

0 commit comments

Comments
 (0)