Skip to content

Commit

Permalink
Add iterate for Reverse{<:NamedTuple} (#43038)
Browse files Browse the repository at this point in the history
Fixes the problem mentioned in #42991 (comment)
  • Loading branch information
bkamins authored Nov 13, 2021
1 parent 81e3889 commit ddd0e53
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
2 changes: 1 addition & 1 deletion base/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ reverse(r::Reverse) = r.itr
reverse(x::Union{Number,AbstractChar}) = x
reverse(p::Pair) = Base.reverse(p) # copying pairs is cheap

iterate(r::Reverse{<:Tuple}, i::Int = length(r.itr)) = i < 1 ? nothing : (r.itr[i], i-1)
iterate(r::Reverse{<:Union{Tuple, NamedTuple}}, i::Int = length(r.itr)) = i < 1 ? nothing : (r.itr[i], i-1)

# enumerate

Expand Down
6 changes: 4 additions & 2 deletions test/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -687,17 +687,19 @@ end
for itr in (2:10, "∀ϵ>0", 1:0, "", (2,3,5,7,11), [2,3,5,7,11], rand(5,6), Z, 3, true, 'x', 4=>5,
eachindex("∀ϵ>0"), view(Z), view(rand(5,6),2:4,2:6), (x^2 for x in 1:10),
Iterators.Filter(isodd, 1:10), flatten((1:10, 50:60)), enumerate("foo"),
pairs(50:60), zip(1:10,21:30,51:60), product(1:3, 10:12), repeated(3.14159, 5))
pairs(50:60), zip(1:10,21:30,51:60), product(1:3, 10:12), repeated(3.14159, 5),
(a=2, b=3, c=5, d=7, e=11))
@test squash(collect(Iterators.reverse(itr))) == reverse(squash(collect(itr)))
end
@test collect(take(Iterators.reverse(cycle(1:3)), 7)) == collect(take(cycle(3:-1:1), 7))
let r = repeated(3.14159)
@test Iterators.reverse(r) === r
end
let t = (2,3,5,7,11)
for t in [(1,), (2, 3, 5, 7, 11), (a=1,), (a=2, b=3, c=5, d=7, e=11)]
@test Iterators.reverse(Iterators.reverse(t)) === t
@test first(Iterators.reverse(t)) === last(t)
@test last(Iterators.reverse(t)) === first(t)
@test collect(Iterators.reverse(t)) == reverse(collect(t))
end
end

Expand Down

0 comments on commit ddd0e53

Please sign in to comment.