Skip to content

Commit 298204e

Browse files
committed
Define length methods for various iterators, using Inf as length for infinite iterators. See JuliaCollections/Iterators.jl#42
1 parent ca56bcc commit 298204e

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

base/iterator.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ countfrom(start::Number) = Count(start, one(start))
120120
countfrom() = Count(1, 1)
121121

122122
eltype{S}(it::Count{S}) = S
123+
length(it::Count) = Inf
123124

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

136137
eltype(it::Take) = eltype(it.xs)
138+
length(it::Take) = min(length(it.xs), n)
137139

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

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

159161
eltype(it::Drop) = eltype(it.xs)
162+
length(it::Drop) = max(length(it.xs)-n, 0)
160163

161164
function start(it::Drop)
162165
xs_state = start(it.xs)
@@ -181,6 +184,7 @@ end
181184
cycle(xs) = Cycle(xs)
182185

183186
eltype(it::Cycle) = eltype(it.xs)
187+
length(it::Cycle) = Inf
184188

185189
function start(it::Cycle)
186190
s = start(it.xs)
@@ -205,6 +209,8 @@ immutable Repeated{O}
205209
end
206210
repeated(x) = Repeated(x)
207211
eltype{O}(r::Repeated{O}) = O
212+
length(x::Repeated) = Inf
213+
208214
start(it::Repeated) = nothing
209215
next(it::Repeated, state) = (it.x, nothing)
210216
done(it::Repeated, state) = false

test/functional.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ let b = IOBuffer("1\n2\n3\n"), a = []
4848
push!(a, (i,x))
4949
end
5050
@test a == [(1,"1\n"),(2,"2\n"),(3,"3\n")]
51+
@test length(enumerate(eachline(b))) == 3
5152
end
5253

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

6368
# rest
@@ -67,6 +72,8 @@ let s = "hello"
6772
@test collect(rest(s, st)) == ['e','l','l','o']
6873
end
6974

75+
76+
7077
# countfrom
7178
# ---------
7279

@@ -77,6 +84,8 @@ let i = 0
7784
i <= 10 || break
7885
end
7986
end
87+
@test length(countfrom(0,2)) == Inf
88+
@test length(countfrom(0)) == Inf
8089

8190
# take
8291
# ----
@@ -99,6 +108,10 @@ let i = 0
99108
@test i == 10
100109
end
101110

111+
@test length(take(1:10,5)) == 5
112+
@test length(take(1:10,15)) == 10
113+
@test length(take(countfrom(1),5)) == 5
114+
102115
# drop
103116
# ----
104117

@@ -110,6 +123,11 @@ let i = 0
110123
@test i == 4
111124
end
112125

126+
@test length(drop(1:10,5)) == 5
127+
@test length(drop(1:10,15)) == 0
128+
@test length(drop(countfrom(0),5)) == Inf
129+
130+
113131
# cycle
114132
# -----
115133

@@ -121,6 +139,8 @@ let i = 0
121139
end
122140
end
123141

142+
@test length(cycle(0:3)) == Inf
143+
124144
# repeated
125145
# --------
126146

@@ -138,3 +158,6 @@ let i = 0
138158
i <= 10 || break
139159
end
140160
end
161+
162+
@test length(repeated(1)) == Inf
163+
@test length(repeated(1,10)) == 10

0 commit comments

Comments
 (0)