Skip to content

Commit 7fa1aea

Browse files
committed
extras/iterators: some fixes + added tests
1 parent ea1855c commit 7fa1aea

File tree

3 files changed

+27
-20
lines changed

3 files changed

+27
-20
lines changed

extras/iterators.jl

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ export
1515
# Infinite counting
1616

1717
type Count
18-
18+
start::Any
1919
step::Any
2020
end
2121

22-
count(start, step) = Count(start, step)
23-
count{T <: Number}(start::T) = Count(start, convert(T, 1))
24-
count() = Count(0, 1)
22+
count(start, step) = Count(start, step)
23+
count(start) = Count(start, one(start))
24+
count() = Count(0, 1)
2525

2626
start(it::Count) = it.start
27-
next(it::Count, state) = (state, state + 1)
27+
next(it::Count, state) = (state, state + it.step)
2828
done(it::Count, state) = false
2929

3030

@@ -40,13 +40,13 @@ take(xs, n) = Take(xs, n)
4040
start(it::Take) = (it.n, start(it.xs))
4141

4242
function next(it::Take, state)
43-
(n, xs_state) = state
44-
(v, xs_state) = next(it.xs, xs_state)
45-
(v, (n - 1, xs_state))
43+
n, xs_state = state
44+
v, xs_state = next(it.xs, xs_state)
45+
return v, (n - 1, xs_state)
4646
end
4747

4848
function done(it::Take, state)
49-
(n, xs_state) = state
49+
n, xs_state = state
5050
return n <= 0 || done(it.xs, xs_state)
5151
end
5252

@@ -67,7 +67,7 @@ function start(it::Drop)
6767
break
6868
end
6969

70-
(_, xs_state) = next(it.xs, xs_state)
70+
_, xs_state = next(it.xs, xs_state)
7171
end
7272
xs_state
7373
end
@@ -84,19 +84,24 @@ end
8484

8585
cycle(xs) = Cycle(xs)
8686

87-
start(it::Cycle) = start(it.xs)
87+
function start(it::Cycle)
88+
s = start(it.xs)
89+
return s, done(it.xs, s)
90+
end
8891

8992
function next(it::Cycle, state)
90-
if done(it.xs, state)
91-
state = start(it.xs)
93+
s, d = state
94+
if done(it.xs, s)
95+
s = start(it.xs)
9296
end
93-
v = next(it.xs, state)
97+
v, s = next(it.xs, s)
98+
return v, (s, false)
9499
end
95100

96-
done(it::Cycle, state) = false
101+
done(it::Cycle, state) = state[2]
97102

98103

99-
# Repeat an object n (or infinately many) times.
104+
# Repeat an object n (or infinitely many) times.
100105

101106
type Repeat
102107
x
@@ -143,20 +148,20 @@ function start(it::Chain)
143148
end
144149
i += 1
145150
end
146-
(i, xs_state)
151+
return i, xs_state
147152
end
148153

149154
function next(it::Chain, state)
150155
i, xs_state = state
151-
(v, xs_state) = next(it.xss[i], xs_state)
156+
v, xs_state = next(it.xss[i], xs_state)
152157
while done(it.xss[i], xs_state)
153158
i += 1
154159
if i > length(it.xss)
155160
break
156161
end
157162
xs_state = start(it.xss[i])
158163
end
159-
(v, (i, xs_state))
164+
return v, (i, xs_state)
160165
end
161166

162167
done(it::Chain, state) = state[1] > length(it.xss)

test/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ TESTS = default all extra \
88
core numbers strings unicode corelib hashing \
99
arrayops linalg fft sparse arpack bitarray \
1010
random special functional bigint dists combinatorics statistics \
11-
glpk linprog sparse bigfloat poly file Rmath remote zlib sound image
11+
glpk linprog sparse bigfloat poly file Rmath remote zlib sound image \
12+
iterators
1213

1314
$(TESTS) ::
1415
$(QUIET_JULIA) $(JULIA_EXECUTABLE) ./runtests.jl $@

test/extra.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ runtests("Rmath")
77
runtests("zlib")
88
runtests("options")
99
runtests("image")
10+
runtests("iterators")
1011

1112
runtests("perf")

0 commit comments

Comments
 (0)