diff --git a/base/tuple.jl b/base/tuple.jl index eabbce23e4f18..3942202d90d97 100644 --- a/base/tuple.jl +++ b/base/tuple.jl @@ -63,13 +63,16 @@ end ## mapping ## ntuple(f::Function, n::Integer) = - n<=0 ? () : - n==1 ? (f(1),) : - n==2 ? (f(1),f(2),) : - n==3 ? (f(1),f(2),f(3),) : - n==4 ? (f(1),f(2),f(3),f(4),) : - n==5 ? (f(1),f(2),f(3),f(4),f(5),) : - tuple(ntuple(f,n-5)..., f(n-4), f(n-3), f(n-2), f(n-1), f(n)) + n <= 0 ? () : + n == 1 ? (f(1),) : + n == 2 ? (f(1),f(2),) : + n == 3 ? (f(1),f(2),f(3),) : + n == 4 ? (f(1),f(2),f(3),f(4),) : + n == 5 ? (f(1),f(2),f(3),f(4),f(5),) : + n < 16 ? (ntuple(f,n-5)..., f(n-4), f(n-3), f(n-2), f(n-1), f(n)) : + _ntuple(f, n) + +_ntuple(f::Function, n::Integer) = (@_noinline_meta; ((f(i) for i = 1:n)...)) # inferrable ntuple function ntuple{F,N}(f::F, ::Type{Val{N}}) diff --git a/test/tuple.jl b/test/tuple.jl index 5d7240f15bbed..dbb7570273cba 100644 --- a/test/tuple.jl +++ b/test/tuple.jl @@ -162,3 +162,11 @@ end # issue #12854 @test_throws TypeError ntuple(identity, Val{1:2}) + +for n = 0:20 + t = ntuple(identity, n) + @test length(t) == n + for i = 1:n + @test t[i] == i + end +end