From 7c9a4dfce31ce3c0020b8cf05704a348ff4b27a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bogumi=C5=82=20Kami=C5=84ski?= <bkamins@sgh.waw.pl> Date: Thu, 11 Nov 2021 16:03:16 +0100 Subject: [PATCH 1/4] Add iterate for Reverse{<:NamedTuple} Fixes the problem mentioned in https://github.com/JuliaLang/julia/pull/42991#issuecomment-966190513 --- base/iterators.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From 7dd0eb73196fa6eb2a2d2908cb0340b972f21c6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bogumi=C5=82=20Kami=C5=84ski?= <bkamins@sgh.waw.pl> Date: Thu, 11 Nov 2021 16:06:54 +0100 Subject: [PATCH 2/4] Add tests for NamedTuple --- test/iterators.jl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/iterators.jl b/test/iterators.jl index 5002e7ec473e6..645195cb01c5b 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), NamedTuple(), (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)) == [11, 7, 5, 3, 2] end end From ce781e93f6a0322c752484b61d6e640dbcfd0c9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bogumi=C5=82=20Kami=C5=84ski?= <bkamins@sgh.waw.pl> Date: Thu, 11 Nov 2021 16:08:26 +0100 Subject: [PATCH 3/4] Update test/iterators.jl --- test/iterators.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/iterators.jl b/test/iterators.jl index 645195cb01c5b..6b45fa1c813fe 100644 --- a/test/iterators.jl +++ b/test/iterators.jl @@ -699,7 +699,7 @@ end @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)) == [11, 7, 5, 3, 2] + @test collect(Iterators.reverse(t)) == reverse(collect(t)) end end From e1e04fac180c3ac150e6fd1165ac28a97c775c32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bogumi=C5=82=20Kami=C5=84ski?= <bkamins@sgh.waw.pl> Date: Thu, 11 Nov 2021 20:23:45 +0100 Subject: [PATCH 4/4] Update test/iterators.jl --- test/iterators.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/iterators.jl b/test/iterators.jl index 6b45fa1c813fe..273b4a1c615fc 100644 --- a/test/iterators.jl +++ b/test/iterators.jl @@ -695,7 +695,7 @@ end let r = repeated(3.14159) @test Iterators.reverse(r) === r end - for t in [(), (1,), (2, 3, 5, 7, 11), NamedTuple(), (a=1,), (a=2, b=3, c=5, d=7, e=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)