Skip to content

Commit dffe13a

Browse files
committed
Fix for non-array iterables
1 parent f57b318 commit dffe13a

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/progress.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ end
9898

9999
function _progress(name, thresh, ex, target, result, loop, iter_vars, ranges, body)
100100
count_vars = [Symbol("i$k") for k=1:length(iter_vars)]
101-
iter_exprs = [:(($i,$(esc(v))) = pairs($(esc(r))))
101+
iter_exprs = [:(($i,$(esc(v))) = _vec_pairs($(esc(r))))
102102
for (i,v,r) in zip(count_vars,iter_vars,ranges)]
103103
_id = "progress_$(gensym())"
104104
quote
@@ -107,7 +107,7 @@ function _progress(name, thresh, ex, target, result, loop, iter_vars, ranges, bo
107107
$target = try
108108
ranges = $(Expr(:vect,esc.(ranges)...))
109109
nranges = length(ranges)
110-
starts = firstindex.(ranges)
110+
starts = first.(only.(axes.(ranges)))
111111
lens = length.(ranges)
112112
n = prod(lens)
113113
strides = cumprod([1;lens[1:end-1]])
@@ -139,3 +139,6 @@ end
139139

140140
_comprehension(iter_exprs, body,) = Expr(:comprehension, Expr(:generator, body, iter_exprs...))
141141
_for(iter_exprs, body) = Expr(:for, Expr(:block, reverse(iter_exprs)...), body)
142+
143+
# like Base.pairs, but for any vector with `axes`
144+
_vec_pairs(v) = zip(only(axes(v)), v)

test/runtests.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,24 @@ let off1 = -2, off2 = 21
7272
@test x == y == OffsetArray([-1 0 1; -2 0 2; -3 0 3], off1, off2)
7373
end
7474

75+
# non-indexable iterables with axes
76+
let r = 1:5
77+
x1 = @progress y1 = [i for i in (x^2 for x in r)]
78+
x2 = @progress y2 = [i for i in zip(r,r)]
79+
@test x1 == y1 == r.^2
80+
@test x2 == y2 == collect(zip(r,r))
81+
82+
y1, y2 = [], []
83+
x1 = @progress for i in (x^2 for x in r)
84+
push!(y1, i)
85+
end
86+
x2 = @progress for i in zip(r,r)
87+
push!(y2, i)
88+
end
89+
@test x1 == x2 == nothing
90+
@test y1 == r.^2
91+
@test y2 == collect(zip(r,r))
92+
end
93+
94+
7595
@test Juno.notify("hi") == nothing

0 commit comments

Comments
 (0)