Skip to content

Commit

Permalink
Merge pull request #11486 from JuliaLang/teh/ntuple_argorder
Browse files Browse the repository at this point in the history
Deprecate ntuple(::Integer, ::Function)
  • Loading branch information
timholy committed May 31, 2015
2 parents 6fd6c8f + ae0ce5f commit 7fa2cf1
Show file tree
Hide file tree
Showing 18 changed files with 44 additions and 292 deletions.
6 changes: 3 additions & 3 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}...)
Expand Down Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 0 additions & 4 deletions base/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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, ())


Expand Down
12 changes: 6 additions & 6 deletions base/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion base/deepcopy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions base/essentials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,7 @@ end
type Colon
end
const (:) = Colon()

# For passing constants through type inference
immutable Val{T}
end
2 changes: 1 addition & 1 deletion base/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion base/mmap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion base/serialize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,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)))
Expand Down
2 changes: 1 addition & 1 deletion base/subarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 12 additions & 2 deletions base/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,25 @@ 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),) :
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(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))

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()
Expand Down
2 changes: 1 addition & 1 deletion doc/stdlib/base.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
8 changes: 4 additions & 4 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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->d<N ? UnitRange{Int} : Float64)
args = ntuple(d->d<N ? UnitRange{Int} : Float64, N)
N > 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}]
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
Loading

0 comments on commit 7fa2cf1

Please sign in to comment.