From 4a131802c5aed6222c0492286dc62caa5e9cc757 Mon Sep 17 00:00:00 2001 From: "Steven G. Johnson" Date: Thu, 20 Dec 2018 16:29:08 -0500 Subject: [PATCH 1/4] faster mapfoldl for tuples --- base/tuple.jl | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/base/tuple.jl b/base/tuple.jl index 0bdec7547c164..c90465d392a97 100644 --- a/base/tuple.jl +++ b/base/tuple.jl @@ -212,6 +212,18 @@ function map(f, t1::Any16, t2::Any16, ts::Any16...) (A...,) end +# mapafoldl, based on afold in operators.jl +mapafoldl(F,op,a) = a +mapafoldl(F,op,a,b) = op(a,F(b)) +mapafoldl(F,op,a,b,c...) = mapafoldl(op, op(a,F(b)), c...) +function mapafoldl(F,op,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,qs...) + y = op(op(op(op(op(op(op(op(op(op(op(op(op(op(op(a,F(b)),F(c)),F(d)),F(e)),F(f)),F(g)),F(h)),F(i)),F(j)),F(k)),F(l)),F(m)),F(n)),F(o)),F(p)) + for x in qs; y = op(y,F(x)); end + y +end +mapfoldl_impl(f, op, nt::NamedTuple{(:init,)}, t::Tuple) = mapafoldl(f, op, nt.init, t...) +mapfoldl_impl(f, op, nt::NamedTuple{()}, t::Tuple) = mapafoldl(f, op, f(t[1]), tail(t)...) +mapfoldl_impl(f, op, nt::NamedTuple{()}, t::Tuple{}) = mapreduce_empty_iter(f, op, t, IteratorEltype(t)) # type-stable padding fill_to_length(t::NTuple{N,Any}, val, ::Val{N}) where {N} = t From c02e146005f507e191fb9b59f5426a59965e1f7e Mon Sep 17 00:00:00 2001 From: "Steven G. Johnson" Date: Thu, 20 Dec 2018 16:48:46 -0500 Subject: [PATCH 2/4] whoops --- base/tuple.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/tuple.jl b/base/tuple.jl index c90465d392a97..3852ae14368c1 100644 --- a/base/tuple.jl +++ b/base/tuple.jl @@ -215,7 +215,7 @@ end # mapafoldl, based on afold in operators.jl mapafoldl(F,op,a) = a mapafoldl(F,op,a,b) = op(a,F(b)) -mapafoldl(F,op,a,b,c...) = mapafoldl(op, op(a,F(b)), c...) +mapafoldl(F,op,a,b,c...) = mapafoldl(F, op, op(a,F(b)), c...) function mapafoldl(F,op,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,qs...) y = op(op(op(op(op(op(op(op(op(op(op(op(op(op(op(a,F(b)),F(c)),F(d)),F(e)),F(f)),F(g)),F(h)),F(i)),F(j)),F(k)),F(l)),F(m)),F(n)),F(o)),F(p)) for x in qs; y = op(y,F(x)); end From 5d3daba7313def20c721652212693329f1185923 Mon Sep 17 00:00:00 2001 From: "Steven G. Johnson" Date: Thu, 20 Dec 2018 16:49:30 -0500 Subject: [PATCH 3/4] tests --- test/tuple.jl | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/tuple.jl b/test/tuple.jl index 3a22f3b089557..2b34547100309 100644 --- a/test/tuple.jl +++ b/test/tuple.jl @@ -237,6 +237,14 @@ end end end +@testset "mapreduce" begin + @test (((1=>2)=>3)=>4) == foldl(=>, (1,2,3,4)) == + mapfoldl(identity, =>, (1,2,3,4)) == mapfoldl(abs, =>, (-1,-2,-3,-4)) + @test mapfoldl(abs, =>, (-1,-2,-3,-4), init=-10) == ((((-10=>1)=>2)=>3)=>4) + @test mapfoldl(abs, =>, (), init=-10) == -10 + @test_throws ArgumentError mapfoldl(abs, =>, ()) +end + @testset "comparison and hash" begin @test isequal((), ()) @test isequal((1,2,3), (1,2,3)) From 0b46d825be9a6069d80403c81ce1b7967697b986 Mon Sep 17 00:00:00 2001 From: "Steven G. Johnson" Date: Thu, 20 Dec 2018 21:13:56 -0500 Subject: [PATCH 4/4] another test --- test/tuple.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/tuple.jl b/test/tuple.jl index 2b34547100309..9b56ccff20fbe 100644 --- a/test/tuple.jl +++ b/test/tuple.jl @@ -237,11 +237,12 @@ end end end -@testset "mapreduce" begin +@testset "mapfoldl" begin @test (((1=>2)=>3)=>4) == foldl(=>, (1,2,3,4)) == mapfoldl(identity, =>, (1,2,3,4)) == mapfoldl(abs, =>, (-1,-2,-3,-4)) @test mapfoldl(abs, =>, (-1,-2,-3,-4), init=-10) == ((((-10=>1)=>2)=>3)=>4) @test mapfoldl(abs, =>, (), init=-10) == -10 + @test mapfoldl(abs, Pair{Any,Any}, (-30:-1...,)) == mapfoldl(abs, Pair{Any,Any}, [-30:-1...,]) @test_throws ArgumentError mapfoldl(abs, =>, ()) end