Skip to content

Commit ecb2b94

Browse files
mfaltararslan
authored andcommitted
Fixed iterate over Variable (#270)
1 parent 1e7dd29 commit ecb2b94

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/utilities/iteration.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Base.iterate
22
export iterate
33

4-
function iterate(x::Variable, (el, s)=(x[1], 0))
5-
return s >= length(x) ? nothing : (el, (x[s+1], s+1))
4+
function iterate(x::Variable, s=0)
5+
return s >= length(x) ? nothing : (x[s+1], s+1)
66
end

test/test_utilities.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,22 @@
7171
@test Convex.imag_conic_form(Constant([1.0, 2.0])) == [0.0, 0.0]
7272
end
7373

74+
@testset "Iteration" begin
75+
x = Variable(2,3)
76+
s = sum([xi for xi in x])
77+
x.value = [1 2 3; 4 5 6]
78+
# evaluate(s) == [21] (which might be wrong? expected 21)
79+
# but [21][1] === 21[1] === 21
80+
# so this should pass even after "fixing" that
81+
@test evaluate(s)[1] == 21
82+
83+
x = Variable(4)
84+
@test [xi.inds for xi in x] == [1:1, 2:2, 3:3, 4:4]
85+
86+
x = Variable(0)
87+
@test [xi for xi in x] == []
88+
@test iterate(x) == nothing
89+
end
7490
# returns [21]; not sure why
7591
# context("iteration") do
7692
# x = Variable(2,3)

0 commit comments

Comments
 (0)