Skip to content

Commit

Permalink
Fixed iterate over Variable (#270)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfalt authored and ararslan committed Jan 23, 2019
1 parent 1e7dd29 commit ecb2b94
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/utilities/iteration.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Base.iterate
export iterate

function iterate(x::Variable, (el, s)=(x[1], 0))
return s >= length(x) ? nothing : (el, (x[s+1], s+1))
function iterate(x::Variable, s=0)
return s >= length(x) ? nothing : (x[s+1], s+1)
end
16 changes: 16 additions & 0 deletions test/test_utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,22 @@
@test Convex.imag_conic_form(Constant([1.0, 2.0])) == [0.0, 0.0]
end

@testset "Iteration" begin
x = Variable(2,3)
s = sum([xi for xi in x])
x.value = [1 2 3; 4 5 6]
# evaluate(s) == [21] (which might be wrong? expected 21)
# but [21][1] === 21[1] === 21
# so this should pass even after "fixing" that
@test evaluate(s)[1] == 21

x = Variable(4)
@test [xi.inds for xi in x] == [1:1, 2:2, 3:3, 4:4]

x = Variable(0)
@test [xi for xi in x] == []
@test iterate(x) == nothing
end
# returns [21]; not sure why
# context("iteration") do
# x = Variable(2,3)
Expand Down

0 comments on commit ecb2b94

Please sign in to comment.