From c7221d916f99a3477e0fc443298ebea506e47df1 Mon Sep 17 00:00:00 2001 From: Tim Holy Date: Fri, 29 May 2015 10:19:21 -0500 Subject: [PATCH 1/2] Deprecate ntuple(::Integer, ::Function) Some day we will want to duck-type the Function argument to ntuple, but we need one release cycle with a deprecation warning before we can do that. So let's do the deprecation now. --- base/abstractarray.jl | 6 +- base/array.jl | 4 +- base/base.jl | 4 - base/bitarray.jl | 12 +- base/deepcopy.jl | 2 +- base/deprecated.jl | 2 + base/essentials.jl | 4 + base/inference.jl | 2 +- base/mmap.jl | 2 +- base/multidimensional.jl | 2 +- base/serialize.jl | 2 +- base/subarray.jl | 2 +- base/tuple.jl | 3 +- doc/stdlib/base.rst | 2 +- test/arrayops.jl | 8 +- test/arrayperf.jl | 263 --------------------------------------- test/serialize.jl | 2 +- 17 files changed, 30 insertions(+), 292 deletions(-) delete mode 100644 test/arrayperf.jl diff --git a/base/abstractarray.jl b/base/abstractarray.jl index bf18b1daac852..7014523b88e9d 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -81,7 +81,7 @@ function stride(a::AbstractArray, i::Integer) return s end -strides(a::AbstractArray) = ntuple(ndims(a), i->stride(a,i))::Dims +strides(a::AbstractArray) = ntuple(i->stride(a,i), ndims(a))::Dims function isassigned(a::AbstractArray, i::Int...) # TODO @@ -738,7 +738,7 @@ function hvcat(nbc::Integer, as...) throw(ArgumentError("all rows must have the same number of block columns")) end nbr = div(n,nbc) - hvcat(ntuple(nbr, i->nbc), as...) + hvcat(ntuple(i->nbc, nbr), as...) end function hvcat{T}(rows::Tuple{Vararg{Int}}, as::AbstractMatrix{T}...) @@ -1049,7 +1049,7 @@ end function ind2sub{N,T<:Integer}(dims::NTuple{N,Integer}, ind::AbstractVector{T}) M = length(ind) - t = NTuple{N,Vector{T}}(ntuple(N,n->Array{T}(M))) + t = NTuple{N,Vector{T}}(ntuple(n->Array{T}(M),N)) copy!(t[1],ind) for j = 1:N-1 d = dims[j] diff --git a/base/array.jl b/base/array.jl index 3765af07d19f4..3341ef11b057a 100644 --- a/base/array.jl +++ b/base/array.jl @@ -1278,8 +1278,8 @@ function indcopy(sz::Dims, I::Tuple{Vararg{RangeIndex}}) for i = n+1:length(sz) s *= sz[i] end - dst::typeof(I) = ntuple(n, i-> findin(I[i], i < n ? (1:sz[i]) : (1:s)))::typeof(I) - src::typeof(I) = ntuple(n, i-> I[i][findin(I[i], i < n ? (1:sz[i]) : (1:s))])::typeof(I) + dst::typeof(I) = ntuple(i-> findin(I[i], i < n ? (1:sz[i]) : (1:s)), n)::typeof(I) + src::typeof(I) = ntuple(i-> I[i][findin(I[i], i < n ? (1:sz[i]) : (1:s))], n)::typeof(I) dst, src end diff --git a/base/base.jl b/base/base.jl index d36e9210266a2..419add05c2e8a 100644 --- a/base/base.jl +++ b/base/base.jl @@ -59,10 +59,6 @@ type AssertionError <: Exception AssertionError(msg) = new(msg) end -# For passing constants through type inference -immutable Val{T} -end - ccall(:jl_get_system_hooks, Void, ()) diff --git a/base/bitarray.jl b/base/bitarray.jl index 14cb014aa2cb8..961bb998f5c12 100644 --- a/base/bitarray.jl +++ b/base/bitarray.jl @@ -280,7 +280,7 @@ function reshape{N}(B::BitArray, dims::NTuple{N,Int}) prod(dims) == length(B) || throw(DimensionMismatch("new dimensions $(dims) must be consistent with array size $(length(B))")) dims == size(B) && return B - Br = BitArray{N}(ntuple(N,i->0)...) + Br = BitArray{N}(ntuple(i->0,N)...) Br.chunks = B.chunks Br.len = prod(dims) N != 1 && (Br.dims = dims) @@ -1710,7 +1710,7 @@ function permutedims(B::Union(BitArray,StridedArray), perm) dimsB = size(B) ndimsB = length(dimsB) (ndimsB == length(perm) && isperm(perm)) || throw(ArgumentError("no valid permutation of dimensions")) - dimsP = ntuple(ndimsB, i->dimsB[perm[i]])::typeof(dimsB) + dimsP = ntuple(i->dimsB[perm[i]], ndimsB)::typeof(dimsB) P = similar(B, dimsP) permutedims!(P, B, perm) end @@ -1831,7 +1831,7 @@ function cat(catdim::Integer, X::Union(BitArray, Integer)...) end end - cat_ranges = ntuple(nargs, i->(catdim <= ndimsX[i] ? dimsX[i][catdim] : 1)) + cat_ranges = ntuple(i->(catdim <= ndimsX[i] ? dimsX[i][catdim] : 1), nargs) function compute_dims(d) if d == catdim @@ -1844,7 +1844,7 @@ function cat(catdim::Integer, X::Union(BitArray, Integer)...) end ndimsC = max(catdim, d_max) - dimsC = ntuple(ndimsC, compute_dims)::Tuple{Vararg{Int}} + dimsC = ntuple(compute_dims, ndimsC)::Tuple{Vararg{Int}} typeC = promote_type(map(x->isa(x,BitArray) ? eltype(x) : typeof(x), X)...) if !has_integer || typeC == Bool C = BitArray(dimsC) @@ -1855,8 +1855,8 @@ function cat(catdim::Integer, X::Union(BitArray, Integer)...) range = 1 for k = 1:nargs nextrange = range + cat_ranges[k] - cat_one = ntuple(ndimsC, i->(i != catdim ? - (1:dimsC[i]) : (range:nextrange-1) )) + cat_one = ntuple(i->(i != catdim ? (1:dimsC[i]) : (range:nextrange-1)), + ndimsC) # note: when C and X are BitArrays, this calls # the special assign with ranges C[cat_one...] = X[k] diff --git a/base/deepcopy.jl b/base/deepcopy.jl index ab866fd4cbee6..0f7fb648773bc 100644 --- a/base/deepcopy.jl +++ b/base/deepcopy.jl @@ -11,7 +11,7 @@ deepcopy_internal(x::Union(Symbol,LambdaStaticData,TopNode,QuoteNode, DataType,UnionType,Task), stackdict::ObjectIdDict) = x deepcopy_internal(x::Tuple, stackdict::ObjectIdDict) = - ntuple(length(x), i->deepcopy_internal(x[i], stackdict)) + ntuple(i->deepcopy_internal(x[i], stackdict), length(x)) deepcopy_internal(x::Module, stackdict::ObjectIdDict) = error("deepcopy of Modules not supported") function deepcopy_internal(x::Function, stackdict::ObjectIdDict) diff --git a/base/deprecated.jl b/base/deprecated.jl index b2406918faa1d..7cac3c4ae9db6 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -368,6 +368,8 @@ end @deprecate with_env(f::Function, key::AbstractString, val) withenv(f, key=>val) +@deprecate ntuple(n::Integer, f::Function) ntuple(f, n) + # 0.4 discontinued functions @noinline function subtypetree(x::DataType, level=-1) diff --git a/base/essentials.jl b/base/essentials.jl index 702dd82885d74..9c01156055522 100644 --- a/base/essentials.jl +++ b/base/essentials.jl @@ -226,3 +226,7 @@ end type Colon end const (:) = Colon() + +# For passing constants through type inference +immutable Val{T} +end diff --git a/base/inference.jl b/base/inference.jl index dd82a9ad67fba..5f5220592c5c2 100644 --- a/base/inference.jl +++ b/base/inference.jl @@ -1871,7 +1871,7 @@ function type_annotate(ast::Expr, states::Array{Any,1}, sv::ANY, rettype::ANY, a # builtins.c:jl_trampoline. However if jl_trampoline is changed then # this code will need to be restored. #na = length(a.args[1]) - #li.ast, _ = typeinf(li, ntuple(na+1, i->(i>na ? (Tuple)[1] : Any)), + #li.ast, _ = typeinf(li, ntuple(i->(i>na ? (Tuple)[1] : Any), na+1), # li.sparams, li, false) end end diff --git a/base/mmap.jl b/base/mmap.jl index 9ed92112f8325..30705b650732d 100644 --- a/base/mmap.jl +++ b/base/mmap.jl @@ -166,7 +166,7 @@ function mmap_bitarray{N}(dims::NTuple{N,Integer}, s::IOStream, offset::FileOffs throw(ArgumentError("the given file does not contain a valid BitArray of size $(join(dims, 'x')) (open with \"r+\" mode to override)")) end end - B = BitArray{N}(ntuple(N,i->0)...) + B = BitArray{N}(ntuple(i->0,N)...) B.chunks = chunks B.len = n if N != 1 diff --git a/base/multidimensional.jl b/base/multidimensional.jl index fb211e3b29334..d65ab9deb140b 100644 --- a/base/multidimensional.jl +++ b/base/multidimensional.jl @@ -856,7 +856,7 @@ end @generated function findn{N}(B::BitArray{N}) quote nnzB = countnz(B) - I = ntuple($N, x->Array(Int, nnzB)) + I = ntuple(x->Array(Int, nnzB), $N) if nnzB > 0 count = 1 @nloops $N i B begin diff --git a/base/serialize.jl b/base/serialize.jl index 7e84f4d1d4668..d9efd5757acd1 100644 --- a/base/serialize.jl +++ b/base/serialize.jl @@ -403,7 +403,7 @@ function handle_deserialize(s, b) return deserialize(s, tag) end -deserialize_tuple(s, len) = ntuple(len, i->deserialize(s)) +deserialize_tuple(s, len) = ntuple(i->deserialize(s), len) deserialize(s, ::Type{Symbol}) = symbol(read(s, UInt8, Int32(read(s, UInt8)))) deserialize(s, ::Type{LongSymbol}) = symbol(read(s, UInt8, read(s, Int32))) diff --git a/base/subarray.jl b/base/subarray.jl index d26ba1eec2e6e..d8bb2870d3296 100644 --- a/base/subarray.jl +++ b/base/subarray.jl @@ -31,7 +31,7 @@ parent(V::SubArray) = V.parent parentindexes(V::SubArray) = V.indexes parent(a::AbstractArray) = a -parentindexes(a::AbstractArray) = ntuple(ndims(a), i->1:size(a,i)) +parentindexes(a::AbstractArray) = ntuple(i->1:size(a,i), ndims(a)) ## SubArray creation # Drops singleton dimensions (those indexed with a scalar) diff --git a/base/tuple.jl b/base/tuple.jl index c7977af2e02ed..06e72b8e58ec2 100644 --- a/base/tuple.jl +++ b/base/tuple.jl @@ -28,7 +28,6 @@ eltype{T,_}(::Type{NTuple{_,T}}) = T ## mapping ## -ntuple(n::Integer, f::Function) = ntuple(f, n) # TODO: deprecate this? ntuple(f::Function, n::Integer) = n<=0 ? () : n==1 ? (f(1),) : @@ -36,7 +35,7 @@ ntuple(f::Function, n::Integer) = 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(n-2,f)..., f(n-1), f(n)) + tuple(ntuple(f,n-5)..., f(n-4), f(n-3), f(n-2), f(n-1), f(n)) # 0 argument function map(f) = f() diff --git a/doc/stdlib/base.rst b/doc/stdlib/base.rst index f959f827c1ca8..03533e38e138c 100644 --- a/doc/stdlib/base.rst +++ b/doc/stdlib/base.rst @@ -220,7 +220,7 @@ All Objects Construct a tuple of the given objects. -.. function:: ntuple(n, f::Function) +.. function:: ntuple(f::Function, n) Create a tuple of length ``n``, computing each element as ``f(i)``, where ``i`` is the index of the element. diff --git a/test/arrayops.jl b/test/arrayops.jl index 8985ee265995b..4ec10bcacd1cc 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -882,12 +882,12 @@ A = [NaN]; B = [NaN] Nmax = 3 # TODO: go up to CARTESIAN_DIMS+2 (currently this exposes problems) for N = 1:Nmax #indexing with (UnitRange, UnitRange, UnitRange) - args = ntuple(N, d->UnitRange{Int}) + args = ntuple(d->UnitRange{Int}, N) @test Base.return_types(getindex, Tuple{Array{Float32, N}, args...}) == [Array{Float32, N}] @test Base.return_types(getindex, Tuple{BitArray{N}, args...}) == Any[BitArray{N}] @test Base.return_types(setindex!, Tuple{Array{Float32, N}, Array{Int, 1}, args...}) == [Array{Float32, N}] # Indexing with (UnitRange, UnitRange, Float64) - args = ntuple(N, d->dd 1 && @test Base.return_types(getindex, Tuple{Array{Float32, N}, args...}) == [Array{Float32, N-1}] N > 1 && @test Base.return_types(getindex, Tuple{BitArray{N}, args...}) == [BitArray{N-1}] N > 1 && @test Base.return_types(setindex!, Tuple{Array{Float32, N}, Array{Int, 1}, args...}) == [Array{Float32, N}] @@ -995,7 +995,7 @@ for i = 1:10 @test mdsum(A) == 15 @test mdsum2(A) == 15 AA = reshape(aa, tuple(2, shp...)) - B = sub(AA, 1:1, ntuple(i, i->Colon())...) + B = sub(AA, 1:1, ntuple(i->Colon(), i)...) @test isa(Base.linearindexing(B), Base.IteratorsMD.LinearSlow) @test mdsum(B) == 15 @test mdsum2(B) == 15 @@ -1008,7 +1008,7 @@ for i = 2:10 A = reshape(a, tuple(shp...)) @test mdsum(A) == 55 @test mdsum2(A) == 55 - B = sub(A, ntuple(i, i->Colon())...) + B = sub(A, ntuple(i->Colon(), i)...) @test mdsum(B) == 55 @test mdsum2(B) == 55 insert!(shp, 2, 1) diff --git a/test/arrayperf.jl b/test/arrayperf.jl deleted file mode 100644 index 1d3bc51ea39ae..0000000000000 --- a/test/arrayperf.jl +++ /dev/null @@ -1,263 +0,0 @@ -# This file is a part of Julia. License is MIT: http://julialang.org/license - -n_evals = 1e7 -run_ref = true -run_assign = true - -if run_ref - println("#### Ref ####") - println("Whole array operations:") - println("Small arrays:") - lensmall = 4 - for n_dims in 1:10 - sz = ntuple(n_dims,i->lensmall) - A = randn(sz) - n_el = prod(sz) - n_r = ceil(Int,n_evals/n_el) - print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") - print(n_dims, " dimensions (", n_r, " repeats): ") - @time for i = 1:n_r - B = A[:] - end - end - println("Big arrays:") - lenbig = [1000000,1000,100,32,16,10,10] - for n_dims in 1:length(lenbig) - sz = ntuple(n_dims,i->lenbig[n_dims]) - A = randn(sz) - n_el = prod(sz) - n_r = ceil(Int,n_evals/n_el) - print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") - print(n_dims, " dimensions (", n_r, " repeats): ") - @time for i = 1:n_r - B = A[:] - end - end - println("\n") - - println("Slicing with contiguous blocks:") - println("Small arrays:") - for n_dims in 1:10 - sz = ntuple(n_dims,i->lensmall) - A = randn(sz) - ind = ntuple(n_dims,i -> ((i <= ceil(n_dims/2)) ? (1:sz[i]) : (rand(1:sz[i])))) - n_el = prod(map(length,ind)) - n_r = ceil(Int,n_evals/n_el) - print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") - @time for i = 1:n_r - # ind = ntuple(n_dims,i -> ((i <= n_dims/2) ? (1:sz[i]) : (rand(1:sz[i])))) - B = A[ind...] - end - end - println("Big arrays:") - for n_dims in 1:length(lenbig) - sz = ntuple(n_dims,i->lenbig[n_dims]) - A = randn(sz) - ind = ntuple(n_dims,i -> ((i <= ceil(n_dims/2)) ? (1:sz[i]) : (rand(1:sz[i])))) - n_el = prod(map(length,ind)) - n_r = ceil(Int,n_evals/n_el) - print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") - @time for i = 1:n_r - # ind = ntuple(n_dims,i -> ((i <= n_dims/2) ? (1:sz[i]) : (rand(1:sz[i])))) - B = A[ind...] - end - end - println("\n") - - println("Slicing with non-contiguous blocks:") - println("Small arrays:") - for n_dims in 1:10 - sz = ntuple(n_dims,i->lensmall) - A = randn(sz) - ind = ntuple(n_dims,i -> ((i > n_dims/2) ? (1:sz[i]) : (rand(1:sz[i])))) - n_el = prod(map(length,ind)) - n_r = ceil(Int,n_evals/n_el) - print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") - @time for i = 1:n_r - # ind = ntuple(n_dims,i -> ((i <= n_dims/2) ? (1:sz[i]) : (rand(1:sz[i])))) - B = A[ind...] - end - end - println("Big arrays:") - for n_dims in 1:length(lenbig) - sz = ntuple(n_dims,i->lenbig[n_dims]) - A = randn(sz) - ind = ntuple(n_dims,i -> ((i > n_dims/2) ? (1:sz[i]) : (rand(1:sz[i])))) - n_el = prod(map(length,ind)) - n_r = ceil(Int,n_evals/n_el) - print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") - @time for i = 1:n_r - # ind = ntuple(n_dims,i -> ((i <= n_dims/2) ? (1:sz[i]) : (rand(1:sz[i])))) - B = A[ind...] - end - end - println("\n") - - - println("Random operations:") - println("Small arrays:") - function randind(len) - i = rand(1:6) - indchoices = [1:len,1:ceil(Int,len/2),1:ceil(Int,3*len/4),2:2:len,1:ceil(Int,len/2):len,len:-1:1] - return indchoices[i] - end - #indsmall = [1:4,1:2,1:3,2:2:4,1:3:4,4:-1:1] - for n_dims in 1:10 - sz = ntuple(n_dims,i->lensmall) - A = randn(sz) - ind = ntuple(n_dims,i->randind(sz[i])) - n_el = prod(map(length,ind)) - n_r = ceil(Int,n_evals/n_el) - print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") - @time for i = 1:n_r - # ind = ntuple(n_dims,i->randind(sz[i])) - B = A[ind...] - end - end - println("Big arrays:") - for n_dims in 1:length(lenbig) - sz = ntuple(n_dims,i->lenbig[n_dims]) - A = randn(sz) - ind = ntuple(n_dims,i->randind(sz[i])) - n_el = prod(map(length,ind)) - n_r = ceil(Int,n_evals/n_el) - print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") - @time for i = 1:n_r - # ind = ntuple(n_dims,i->randind(sz[i])) - B = A[ind...] - end - end -end - -if run_assign - println("\n\n\n#### Assign ####") - println("Whole array operations:") - println("Small arrays:") - for n_dims in 1:10 - sz = ntuple(n_dims,i->lensmall) - B = zeros(sz) - A = randn(sz) - n_r = ceil(Int,n_evals/prod(sz)) - print(n_dims, " dimensions (", n_r, " repeats): ") - @time for i = 1:n_r - B[:] = A - end - end - println("Big arrays:") - for n_dims in 1:length(lenbig) - sz = ntuple(n_dims,i->lenbig[n_dims]) - B = zeros(sz) - A = randn(sz) - n_r = ceil(Int,n_evals/prod(sz)) - print(n_dims, " dimensions (", n_r, " repeats): ") - @time for i = 1:n_r - B[:] = A - end - end - println("\n") - - println("Slicing with contiguous blocks:") - println("Small arrays:") - for n_dims in 1:10 - sz = ntuple(n_dims,i->lensmall) - B = zeros(sz) - ind = ntuple(n_dims,i -> ((i <= ceil(n_dims/2)) ? (1:sz[i]) : (rand(1:sz[i])))) - A = randn(map(length,ind)) - n_el = prod(map(length,ind)) - n_r = ceil(Int,n_evals/n_el) - print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") - @time for i = 1:n_r - # ind = ntuple(n_dims,i -> ((i <= n_dims/2) ? (1:sz[i]) : (rand(1:sz[i])))) - # A = randn(map(length,ind)) - B[ind...] = A - end - end - println("Big arrays:") - for n_dims in 1:length(lenbig) - sz = ntuple(n_dims,i->lenbig[n_dims]) - B = zeros(sz) - ind = ntuple(n_dims,i -> ((i <= ceil(n_dims/2)) ? (1:sz[i]) : (rand(1:sz[i])))) - A = randn(map(length,ind)) - n_el = prod(map(length,ind)) - n_r = ceil(Int,n_evals/n_el) - print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") - @time for i = 1:n_r -# ind = ntuple(n_dims,i -> ((i <= n_dims/2) ? (1:sz[i]) : (rand(1:sz[i])))) - # A = randn(map(length,ind)) - B[ind...] = A - end - end - println("\n") - - println("Slicing with non-contiguous blocks:") - println("Small arrays:") - for n_dims in 1:10 - sz = ntuple(n_dims,i->lensmall) - B = zeros(sz) - ind = ntuple(n_dims,i -> ((i > n_dims/2) ? (1:sz[i]) : (rand(1:sz[i])))) - A = randn(map(length,ind)) - n_el = prod(map(length,ind)) - n_r = ceil(Int,n_evals/n_el) - print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") - @time for i = 1:n_r - # ind = ntuple(n_dims,i -> ((i <= n_dims/2) ? (1:sz[i]) : (rand(1:sz[i])))) - # A = randn(map(length,ind)) - B[ind...] = A - end - end - println("Big arrays:") - for n_dims in 1:length(lenbig) - sz = ntuple(n_dims,i->lenbig[n_dims]) - B = zeros(sz) - ind = ntuple(n_dims,i -> ((i > n_dims/2) ? (1:sz[i]) : (rand(1:sz[i])))) - A = randn(map(length,ind)) - n_el = prod(map(length,ind)) - n_r = ceil(Int,n_evals/n_el) - print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") - @time for i = 1:n_r - # ind = ntuple(n_dims,i -> ((i <= n_dims/2) ? (1:sz[i]) : (rand(1:sz[i])))) - # A = randn(map(length,ind)) - B[ind...] = A - end - end - println("\n") - - - println("Random operations:") - println("Small arrays:") - function randind(len) - i = rand(1:6) - indchoices = [1:len,1:ceil(Int,len/2),1:ceil(Int,3*len/4),2:2:len,1:ceil(Int,len/2):len,len:-1:1] - return indchoices[i] - end - #indsmall = [1:4,1:2,1:3,2:2:4,1:3:4,4:-1:1] - for n_dims in 1:10 - sz = ntuple(n_dims,i->lensmall) - B = zeros(sz) - ind = ntuple(n_dims,i->randind(sz[i])) - A = randn(map(length,ind)) - n_el = prod(map(length,ind)) - n_r = ceil(Int,n_evals/n_el) - print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") - @time for i = 1:n_r - # ind = ntuple(n_dims,i->randind(sz[i])) - # A = randn(map(length,ind)) - B[ind...] = A - end - end - println("Big arrays:") - for n_dims in 1:length(lenbig) - sz = ntuple(n_dims,i->lenbig[n_dims]) - B = zeros(sz) - ind = ntuple(n_dims,i->randind(sz[i])) - A = randn(map(length,ind)) - n_el = prod(map(length,ind)) - n_r = ceil(Int,n_evals/n_el) - print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") - @time for i = 1:n_r - # ind = ntuple(n_dims,i->randind(sz[i])) - # A = randn(map(length,ind)) - B[ind...] = A - end - end -end diff --git a/test/serialize.jl b/test/serialize.jl index 5dd034e6efc34..e2a55f729209d 100644 --- a/test/serialize.jl +++ b/test/serialize.jl @@ -59,7 +59,7 @@ create_serialization_stream() do s serialize(s, tpl) len = 257 - lt = ntuple(len, i->0x1) + lt = ntuple(i->0x1, len) serialize(s, lt) serialize(s, Tuple{}) From ae0ce5f4e5dc97b44c9437d95cdabe9a2de11ffd Mon Sep 17 00:00:00 2001 From: Tim Holy Date: Fri, 29 May 2015 10:21:17 -0500 Subject: [PATCH 2/2] Add an inferrable ntuple function based on Val It's inferrable with Functors and FastAnonymous, but not (currently) generic functions. --- base/tuple.jl | 11 +++++++++++ test/tuple.jl | 3 +++ 2 files changed, 14 insertions(+) diff --git a/base/tuple.jl b/base/tuple.jl index 06e72b8e58ec2..6fe5a67354f9f 100644 --- a/base/tuple.jl +++ b/base/tuple.jl @@ -37,6 +37,17 @@ ntuple(f::Function, n::Integer) = 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)) +ntuple(f, ::Type{Val{0}}) = () +ntuple(f, ::Type{Val{1}}) = (f(1),) +ntuple(f, ::Type{Val{2}}) = (f(1),f(2)) +ntuple(f, ::Type{Val{3}}) = (f(1),f(2),f(3)) +ntuple(f, ::Type{Val{4}}) = (f(1),f(2),f(3),f(4)) +ntuple(f, ::Type{Val{5}}) = (f(1),f(2),f(3),f(4),f(5)) +@generated function ntuple{N}(f, ::Type{Val{N}}) + M = N-5 + :(tuple(ntuple(f, Val{$M})..., f($N-4), f($N-3), f($N-2), f($N-1), f($N))) +end + # 0 argument function map(f) = f() # 1 argument function diff --git a/test/tuple.jl b/test/tuple.jl index 90187f6442df6..41e5d1a27c697 100644 --- a/test/tuple.jl +++ b/test/tuple.jl @@ -132,3 +132,6 @@ foo(x, y, z) = x + y + z @test any((true, true)) === true @test any((true, false)) === true @test any((false, false)) === false + +@test @inferred(ntuple(Base.Abs2Fun(), Val{2})) == (1, 4) +@test @inferred(ntuple(Base.Abs2Fun(), Val{6})) == (1, 4, 9, 16, 25, 36)