Skip to content

Commit

Permalink
Define length methods for various iterators, using Inf as length …
Browse files Browse the repository at this point in the history
…for infinite iterators. See JuliaCollections/Iterators.jl#42
  • Loading branch information
simonbyrne committed Jul 1, 2015
1 parent ca56bcc commit a1ce5b0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions base/iterator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ countfrom(start::Number) = Count(start, one(start))
countfrom() = Count(1, 1)

eltype{S}(it::Count{S}) = S
length(it::Count) = Inf

start(it::Count) = it.start
next(it::Count, state) = (state, state + it.step)
Expand All @@ -134,6 +135,7 @@ end
take(xs, n::Int) = Take(xs, n)

eltype(it::Take) = eltype(it.xs)
length(it::Take) = min(length(it.xs), it.n)

start(it::Take) = (it.n, start(it.xs))

Expand All @@ -157,6 +159,7 @@ end
drop(xs, n::Int) = Drop(xs, n)

eltype(it::Drop) = eltype(it.xs)
length(it::Drop) = max(length(it.xs)-it.n, 0)

function start(it::Drop)
xs_state = start(it.xs)
Expand All @@ -181,6 +184,7 @@ end
cycle(xs) = Cycle(xs)

eltype(it::Cycle) = eltype(it.xs)
length(it::Cycle) = Inf

function start(it::Cycle)
s = start(it.xs)
Expand All @@ -205,6 +209,8 @@ immutable Repeated{O}
end
repeated(x) = Repeated(x)
eltype{O}(r::Repeated{O}) = O
length(x::Repeated) = Inf

start(it::Repeated) = nothing
next(it::Repeated, state) = (it.x, nothing)
done(it::Repeated, state) = false
Expand Down
22 changes: 22 additions & 0 deletions test/functional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ let b = IOBuffer("1\n2\n3\n"), a = []
push!(a, (i,x))
end
@test a == [(1,"1\n"),(2,"2\n"),(3,"3\n")]
@test length(enumerate(eachline(b))) == 3
end

# zip eachline (issue #7369)
Expand All @@ -58,6 +59,9 @@ let zeb = IOBuffer("1\n2\n3\n4\n5\n"),
push!(res, (parse(Int,strip(number)), letter))
end
@test res == [(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e')]
@test length(zip(letters, 1:10)) == 5
@test length(zip(letters, countfrom(1))) == 5
@test length(zip(letters, 1:3)) == 3
end

# rest
Expand All @@ -67,6 +71,8 @@ let s = "hello"
@test collect(rest(s, st)) == ['e','l','l','o']
end



# countfrom
# ---------

Expand All @@ -77,6 +83,8 @@ let i = 0
i <= 10 || break
end
end
@test length(countfrom(0,2)) == Inf
@test length(countfrom(0)) == Inf

# take
# ----
Expand All @@ -99,6 +107,10 @@ let i = 0
@test i == 10
end

@test length(take(1:10,5)) == 5
@test length(take(1:10,15)) == 10
@test length(take(countfrom(1),5)) == 5

# drop
# ----

Expand All @@ -110,6 +122,11 @@ let i = 0
@test i == 4
end

@test length(drop(1:10,5)) == 5
@test length(drop(1:10,15)) == 0
@test length(drop(countfrom(0),5)) == Inf


# cycle
# -----

Expand All @@ -121,6 +138,8 @@ let i = 0
end
end

@test length(cycle(0:3)) == Inf

# repeated
# --------

Expand All @@ -138,3 +157,6 @@ let i = 0
i <= 10 || break
end
end

@test length(repeated(1)) == Inf
@test length(repeated(1,10)) == 10

0 comments on commit a1ce5b0

Please sign in to comment.