From ddd0e537bc201792cf5adea2d5242ee00da2f5d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bogumi=C5=82=20Kami=C5=84ski?= Date: Sat, 13 Nov 2021 19:04:56 +0100 Subject: [PATCH] Add iterate for Reverse{<:NamedTuple} (#43038) Fixes the problem mentioned in https://github.com/JuliaLang/julia/pull/42991#issuecomment-966190513 --- base/iterators.jl | 2 +- test/iterators.jl | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/base/iterators.jl b/base/iterators.jl index cc3adad3816e4..d5636fc5341c4 100644 --- a/base/iterators.jl +++ b/base/iterators.jl @@ -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 diff --git a/test/iterators.jl b/test/iterators.jl index 5002e7ec473e6..273b4a1c615fc 100644 --- a/test/iterators.jl +++ b/test/iterators.jl @@ -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