diff --git a/base/REPLCompletions.jl b/base/REPLCompletions.jl index ccb27426fa25f..2bb0466273332 100644 --- a/base/REPLCompletions.jl +++ b/base/REPLCompletions.jl @@ -48,7 +48,7 @@ function complete_symbol(sym, ffunc) # We're now looking for a type fields = fieldnames(t) found = false - for i in 1:length(fields) + for i in eachindex(fields) s == fields[i] || continue t = t.types[i] Base.isstructtype(t) || return UTF8String[] diff --git a/base/abstractarray.jl b/base/abstractarray.jl index ed9736f0eba61..d219ecdfba00c 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -10,7 +10,7 @@ typealias RangeIndex Union{Int, Range{Int}, UnitRange{Int}, Colon} ## Basic functions ## vect() = Array(Any, 0) -vect{T}(X::T...) = T[ X[i] for i=1:length(X) ] +vect{T}(X::T...) = T[ X[i] for i in eachindex(X) ] const _oldstyle_array_vcat_ = true @@ -44,7 +44,7 @@ if _oldstyle_array_vcat_ else function vect(X...) T = promote_typeof(X...) - #T[ X[i] for i=1:length(X) ] + #T[ X[i] for i in eachindex(X) ] # TODO: this is currently much faster. should figure out why. not clear. copy!(Array(T,length(X)), X) end @@ -703,8 +703,8 @@ hcat() = Array(Any, 0) ## cat: special cases hcat{T}(X::T...) = T[ X[j] for i=1, j=1:length(X) ] hcat{T<:Number}(X::T...) = T[ X[j] for i=1, j=1:length(X) ] -vcat{T}(X::T...) = T[ X[i] for i=1:length(X) ] -vcat{T<:Number}(X::T...) = T[ X[i] for i=1:length(X) ] +vcat{T}(X::T...) = T[ X[i] for i in eachindex(X) ] +vcat{T<:Number}(X::T...) = T[ X[i] for i in eachindex(X) ] function vcat(X::Number...) T = promote_typeof(X...) @@ -723,7 +723,7 @@ function vcat{T}(V::AbstractVector{T}...) end a = similar(full(V[1]), n) pos = 1 - for k=1:length(V) + for k in eachindex(V) Vk = V[k] p1 = pos+length(Vk)-1 a[pos:p1] = Vk @@ -800,12 +800,12 @@ function cat_t(catdims, typeC::Type, X...) ndimsC = max(maximum(ndimsX), maximum(catdims)) catsizes = zeros(Int,(nargs,length(catdims))) dims2cat = zeros(Int,ndimsC) - for k = 1:length(catdims) + for k in eachindex(catdims) dims2cat[catdims[k]]=k end dimsC = Int[d <= ndimsX[1] ? size(X[1],d) : 1 for d=1:ndimsC] - for k = 1:length(catdims) + for k in eachindex(catdims) catsizes[1,k] = dimsC[catdims[k]] end for i = 2:nargs @@ -832,7 +832,7 @@ function cat_t(catdims, typeC::Type, X...) cat_one = [ dims2cat[d] == 0 ? (1:dimsC[d]) : (offsets[dims2cat[d]]+(1:catsizes[i,dims2cat[d]])) for d=1:ndimsC ] C[cat_one...] = X[i] - for k = 1:length(catdims) + for k in eachindex(catdims) offsets[k] += catsizes[i,k] end end @@ -1291,7 +1291,7 @@ end ## 1 argument map!{F}(f::F, A::AbstractArray) = map!(f, A, A) function map!{F}(f::F, dest::AbstractArray, A::AbstractArray) - for i = 1:length(A) + for i in eachindex(A) dest[i] = f(A[i]) end return dest @@ -1328,7 +1328,7 @@ end ## 2 argument function map!{F}(f::F, dest::AbstractArray, A::AbstractArray, B::AbstractArray) - for i = 1:length(A) + for i in eachindex(A) dest[i] = f(A[i], B[i]) end return dest diff --git a/base/abstractarraymath.jl b/base/abstractarraymath.jl index 7fc7a868187a3..4948d5ef88955 100644 --- a/base/abstractarraymath.jl +++ b/base/abstractarraymath.jl @@ -19,7 +19,7 @@ _sub(t::Tuple, ::Tuple{}) = t _sub(t::Tuple, s::Tuple) = _sub(tail(t), tail(s)) function squeeze(A::AbstractArray, dims::Dims) - for i in 1:length(dims) + for i in eachindex(dims) 1 <= dims[i] <= ndims(A) || throw(ArgumentError("squeezed dims must be in range 1:ndims(A)")) size(A, dims[i]) == 1 || throw(ArgumentError("squeezed dims must all be size 1")) for j = 1:i-1 @@ -144,7 +144,7 @@ function cumsum_kbn{T<:AbstractFloat}(A::AbstractArray{T}, axis::Integer=1) B = similar(A) C = similar(A) - for i = 1:length(A) + for i in eachindex(A) if div(i-1, axis_stride) % axis_size == 0 B[i] = A[i] C[i] = zero(T) @@ -167,7 +167,7 @@ end function ipermutedims(A::AbstractArray,perm) iperm = Array(Int,length(perm)) - for i = 1:length(perm) + for i in eachindex(perm) iperm[perm[i]] = i end return permutedims(A,iperm) diff --git a/base/array.jl b/base/array.jl index 1b261a4886fe5..cdede6da4a434 100644 --- a/base/array.jl +++ b/base/array.jl @@ -33,7 +33,7 @@ function call{P<:Ptr,T}(::Type{Ref{P}}, a::Array{T}) # Ref{P<:Ptr}(a::Array) else ptrs = Array(P, length(a)+1) roots = Array(Any, length(a)) - for i = 1:length(a) + for i in eachindex(a) root = cconvert(P, a[i]) ptrs[i] = unsafe_convert(P, root)::P roots[i] = root @@ -163,7 +163,7 @@ similar{T}(a::Array{T,2}, S) = Array(S, size(a,1), size(a,2)) # T[x...] constructs Array{T,1} function getindex(T::Type, vals...) a = Array(T,length(vals)) - @inbounds for i = 1:length(vals) + @inbounds for i in eachindex(vals) a[i] = vals[i] end return a @@ -171,7 +171,7 @@ end function getindex(::Type{Any}, vals::ANY...) a = Array(Any,length(vals)) - @inbounds for i = 1:length(vals) + @inbounds for i in eachindex(vals) a[i] = vals[i] end return a @@ -680,7 +680,7 @@ function hcat{T}(V::Vector{T}...) throw(DimensionMismatch("vectors must have same lengths")) end end - [ V[j][i]::T for i=1:length(V[1]), j=1:length(V) ] + [ V[j][i]::T for i in eachindex(V[1]), j in eachindex(V) ] end @@ -750,7 +750,7 @@ function find(testf::Function, A::AbstractArray) # use a dynamic-length array to store the indexes, then copy to a non-padded # array for the return tmpI = Array(Int, 0) - for i = 1:length(A) + for i in eachindex(A) if testf(A[i]) push!(tmpI, i) end @@ -764,7 +764,7 @@ function find(A::StridedArray) nnzA = countnz(A) I = similar(A, Int, nnzA) count = 1 - for i=1:length(A) + for i in eachindex(A) if A[i] != 0 I[count] = i count += 1 @@ -860,7 +860,7 @@ function findin(a, b::UnitRange) ind = Array(Int, 0) f = first(b) l = last(b) - for i = 1:length(a) + for i in eachindex(a) if f <= a[i] <= l push!(ind, i) end @@ -871,7 +871,7 @@ end function findin(a, b) ind = Array(Int, 0) bset = Set(b) - @inbounds for i = 1:length(a) + @inbounds for i in eachindex(a) a[i] in bset && push!(ind, i) end ind @@ -907,7 +907,7 @@ filter(f, As::AbstractArray) = As[map(f, As)::AbstractArray{Bool}] function filter!(f, a::Vector) insrt = 1 - for curr = 1:length(a) + for curr in eachindex(a) if f(a[curr]) a[insrt] = a[curr] insrt += 1 @@ -919,7 +919,7 @@ end function filter(f, a::Vector) r = Array(eltype(a), 0) - for i = 1:length(a) + for i in eachindex(a) if f(a[i]) push!(r, a[i]) end @@ -934,7 +934,7 @@ function intersect(v1, vs...) ret = Array(eltype(v1),0) for v_elem in v1 inall = true - for i = 1:length(vs) + for i in eachindex(vs) if !in(v_elem, vs[i]) inall=false; break end diff --git a/base/arraymath.jl b/base/arraymath.jl index caa52d79387b5..b6713f1444554 100644 --- a/base/arraymath.jl +++ b/base/arraymath.jl @@ -398,7 +398,7 @@ function ctransposeblock!(B::StridedMatrix,A::StridedMatrix,m::Int,n::Int,offset return B end function ccopy!(B, A) - for i = 1:length(A) + for i in eachindex(A) B[i] = ctranspose(A[i]) end end @@ -480,7 +480,7 @@ for (f, op) = ((:cummin, :min), (:cummax, :max)) B = similar(A) - for i = 1:length(A) + for i in eachindex(A) if div(i-1, axis_stride) % axis_size == 0 B[i] = A[i] else diff --git a/base/ascii.jl b/base/ascii.jl index 4fe48eeb7f0fe..7252a5ca00b02 100644 --- a/base/ascii.jl +++ b/base/ascii.jl @@ -63,7 +63,7 @@ end function uppercase(s::ASCIIString) d = s.data - for i = 1:length(d) + for i in eachindex(d) if 'a' <= Char(d[i]) <= 'z' td = copy(d) for j = i:length(td) @@ -78,7 +78,7 @@ function uppercase(s::ASCIIString) end function lowercase(s::ASCIIString) d = s.data - for i = 1:length(d) + for i in eachindex(d) if 'A' <= Char(d[i]) <= 'Z' td = copy(d) for j = i:length(td) diff --git a/base/bitarray.jl b/base/bitarray.jl index c84397571604c..f692a45699bd3 100644 --- a/base/bitarray.jl +++ b/base/bitarray.jl @@ -295,7 +295,7 @@ convert{T,N}(::Type{Array{T}}, B::BitArray{N}) = convert(Array{T,N},B) function convert{T,N}(::Type{Array{T,N}}, B::BitArray{N}) A = Array(T, size(B)) Bc = B.chunks - @inbounds for i = 1:length(A) + @inbounds for i in eachindex(A) A[i] = unsafe_bitgetindex(Bc, i) end return A @@ -386,11 +386,11 @@ function unsafe_setindex!(B::BitArray, x, I::BitArray) Ic = I.chunks length(Bc) == length(Ic) || throw_boundserror(B, I) @inbounds if y - for i = 1:length(Bc) + for i in eachindex(Bc) Bc[i] |= Ic[i] end else - for i = 1:length(Bc) + for i in eachindex(Bc) Bc[i] &= ~Ic[i] end end @@ -817,7 +817,7 @@ function (~)(B::BitArray) Bc = B.chunks if !isempty(Bc) Cc = C.chunks - for i = 1:length(Bc) + for i in eachindex(Bc) Cc[i] = ~Bc[i] end Cc[end] &= _msk_end(B) @@ -828,7 +828,7 @@ end function flipbits!(B::BitArray) Bc = B.chunks @inbounds if !isempty(Bc) - for i = 1:length(Bc) + for i in eachindex(Bc) Bc[i] = ~Bc[i] end Bc[end] &= _msk_end(B) @@ -901,7 +901,7 @@ function div(x::Number, B::BitArray) all(B) || throw(DivideError()) pt = promote_array_type(IDivFun(), typeof(x), Bool) y = div(x, true) - reshape(pt[ y for i = 1:length(B) ], size(B)) + reshape(pt[ y for i in eachindex(B) ], size(B)) end function mod(A::BitArray, B::BitArray) @@ -922,7 +922,7 @@ function mod(x::Number, B::BitArray) all(B) || throw(DivideError()) pt = promote_array_type(ModFun(), typeof(x), Bool) y = mod(x, true) - reshape(pt[ y for i = 1:length(B) ], size(B)) + reshape(pt[ y for i in eachindex(B) ], size(B)) end for (f,F) in ((:div, IDivFun()), @@ -930,7 +930,7 @@ for (f,F) in ((:div, IDivFun()), @eval begin function ($f)(B::BitArray, x::Number) F = Array(promote_array_type($F, typeof(x), Bool), size(B)) - for i = 1:length(F) + for i in eachindex(F) F[i] = ($f)(B[i], x) end return F @@ -961,7 +961,7 @@ for f in (:&, :|, :$) Ac = A.chunks Bc = B.chunks (isempty(Ac) || isempty(Bc)) && return F - for i = 1:length(Fc) + for i in eachindex(Fc) Fc[i] = ($f)(Ac[i], Bc[i]) end Fc[end] &= _msk_end(F) @@ -983,7 +983,7 @@ end function (.^)(x::Number, B::BitArray) z = x ^ false u = x ^ true - reshape([ B[i] ? u : z for i = 1:length(B) ], size(B)) + reshape([ B[i] ? u : z for i in eachindex(B) ], size(B)) end function (.^)(B::BitArray, x::Integer) if x == 0 @@ -1022,7 +1022,7 @@ function (.^){T<:Number}(B::BitArray, x::T) t = typeof(u) end F = Array(t, size(B)) - for i = 1:length(B) + for i in eachindex(B) if B[i] if uerr === nothing F[i] = u @@ -1271,7 +1271,7 @@ end function countnz(B::BitArray) n = 0 Bc = B.chunks - @inbounds for i = 1:length(Bc) + @inbounds for i in eachindex(Bc) n += count_ones(Bc[i]) end return n @@ -1503,7 +1503,7 @@ function any(B::BitArray) length(B) == 0 && return false Bc = B.chunks @inbounds begin - for i = 1:length(Bc) + for i in eachindex(Bc) Bc[i] == 0 || return true end end @@ -1669,7 +1669,7 @@ function hcat(B::BitVector...) end end M = BitArray(height, length(B)) - for j = 1:length(B) + for j in eachindex(B) copy_chunks!(M.chunks, (height*(j-1))+1, B[j].chunks, 1, height) end return M diff --git a/base/cartesian.jl b/base/cartesian.jl index 07e45e71c1703..dd50ca126f1a3 100644 --- a/base/cartesian.jl +++ b/base/cartesian.jl @@ -208,7 +208,7 @@ function lreplace!(ex::Expr, r::LReplace) return ex end end - for i in 1:length(ex.args) + for i in eachindex(ex.args) ex.args[i] = lreplace!(ex.args[i], r) end ex @@ -255,7 +255,7 @@ exprresolve_conditional(arg) = false, false exprresolve(arg) = arg function exprresolve(ex::Expr) - for i = 1:length(ex.args) + for i in eachindex(ex.args) ex.args[i] = exprresolve(ex.args[i]) end # Handle simple arithmetic diff --git a/base/dates/io.jl b/base/dates/io.jl index 799b73db6e12b..a039e850b3aa4 100644 --- a/base/dates/io.jl +++ b/base/dates/io.jl @@ -171,17 +171,17 @@ format(dt::TimeType,f::AbstractString;locale::AbstractString="english") = format # vectorized DateTime{T<:AbstractString}(y::AbstractArray{T},format::AbstractString;locale::AbstractString="english") = DateTime(y,DateFormat(format,locale)) function DateTime{T<:AbstractString}(y::AbstractArray{T},df::DateFormat=ISODateTimeFormat) - return reshape(DateTime[DateTime(parse(y[i],df)...) for i in 1:length(y)], size(y)) + return reshape(DateTime[DateTime(parse(y[i],df)...) for i in eachindex(y)], size(y)) end Date{T<:AbstractString}(y::AbstractArray{T},format::AbstractString;locale::AbstractString="english") = Date(y,DateFormat(format,locale)) function Date{T<:AbstractString}(y::AbstractArray{T},df::DateFormat=ISODateFormat) - return reshape(Date[Date(parse(y[i],df)...) for i in 1:length(y)], size(y)) + return reshape(Date[Date(parse(y[i],df)...) for i in eachindex(y)], size(y)) end format{T<:TimeType}(y::AbstractArray{T},format::AbstractString;locale::AbstractString="english") = Dates.format(y,DateFormat(format,locale)) function format(y::AbstractArray{Date},df::DateFormat=ISODateFormat) - return reshape([Dates.format(y[i],df) for i in 1:length(y)], size(y)) + return reshape([Dates.format(y[i],df) for i in eachindex(y)], size(y)) end function format(y::AbstractArray{DateTime},df::DateFormat=ISODateTimeFormat) - return reshape([Dates.format(y[i],df) for i in 1:length(y)], size(y)) + return reshape([Dates.format(y[i],df) for i in eachindex(y)], size(y)) end diff --git a/base/dates/periods.jl b/base/dates/periods.jl index 9f0c7bb9e58a5..fbf7398fe46bf 100644 --- a/base/dates/periods.jl +++ b/base/dates/periods.jl @@ -64,7 +64,7 @@ for (op,Ty,Tz) in ((:.*,Real,:P), @eval begin function ($op){P<:Period}(X::StridedArray{P},y::$Ty) Z = similar(X, $Tz) - for i = 1:length(X) + for i in eachindex(X) @inbounds Z[i] = ($op_)(X[i],y) end return Z @@ -206,7 +206,7 @@ for op in (:.+, :.-) @eval begin function ($op){P<:GeneralPeriod}(X::StridedArray{P},y::GeneralPeriod) Z = similar(X, CompoundPeriod) - for i = 1:length(X) + for i in eachindex(X) @inbounds Z[i] = ($op_)(X[i],y) end return Z @@ -258,7 +258,7 @@ end # FixedPeriod conversions and promotion rules const fixedperiod_conversions = [(Week,7),(Day,24),(Hour,60),(Minute,60),(Second,1000),(Millisecond,1)] -for i = 1:length(fixedperiod_conversions) +for i in eachindex(fixedperiod_conversions) (T,n) = fixedperiod_conversions[i] N = 1 for j = i-1:-1:1 # less-precise periods diff --git a/base/deepcopy.jl b/base/deepcopy.jl index 787be75d00165..f71bca50074bc 100644 --- a/base/deepcopy.jl +++ b/base/deepcopy.jl @@ -60,7 +60,7 @@ function _deepcopy_array_t(x, T, stackdict::ObjectIdDict) end dest = similar(x) stackdict[x] = dest - for i=1:length(x) + for i in eachindex(x) if isdefined(x,i) arrayset(dest, deepcopy_internal(x[i], stackdict), i) end diff --git a/base/deprecated.jl b/base/deprecated.jl index 2049cf5ade283..1454baa75c8ea 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -730,7 +730,7 @@ export RopeString function complement!(s::IntSet) depwarn("complement IntSets are deprecated", :complement!); - for n = 1:length(s.bits) + for n in eachindex(s.bits) s.bits[n] = ~s.bits[n] end s.fill1s = !s.fill1s diff --git a/base/docs/helpdb.jl b/base/docs/helpdb.jl index cdea57e776a69..0460c4af84b3e 100644 --- a/base/docs/helpdb.jl +++ b/base/docs/helpdb.jl @@ -4602,7 +4602,7 @@ subtypes doc""" digits(n, [base], [pad]) -Returns an array of the digits of `n` in the given base, optionally padded with zeros to a specified size. More significant digits are at higher indexes, such that `n == sum([digits[k]*base^(k-1) for k=1:length(digits)])`. +Returns an array of the digits of `n` in the given base, optionally padded with zeros to a specified size. More significant digits are at higher indexes, such that `n == sum([digits[k]*base^(k-1) for k in eachindex(digits)])`. """ digits diff --git a/base/docs/utils.jl b/base/docs/utils.jl index 88fc3b9bebeb9..0d62bd82849fc 100644 --- a/base/docs/utils.jl +++ b/base/docs/utils.jl @@ -191,7 +191,7 @@ function levsort(search, candidates) scores = map(cand -> (levenshtein(search, cand), -fuzzyscore(search, cand)), candidates) candidates = candidates[sortperm(scores)] i = 0 - for i = 1:length(candidates) + for i in eachindex(candidates) levenshtein(search, candidates[i]) > 3 && break end return candidates[1:i] @@ -230,7 +230,7 @@ printmatches(args...; cols = Base.tty_size()[2]) = printmatches(STDOUT, args..., function print_joined_cols(io::IO, ss, delim = "", last = delim; cols = Base.tty_size()[2]) i = 0 total = 0 - for i = 1:length(ss) + for i in eachindex(ss) total += length(ss[i]) total + max(i-2,0)*length(delim) + (i>1?1:0)*length(last) > cols && (i-=1; break) end diff --git a/base/essentials.jl b/base/essentials.jl index f7da5dbd601a1..789dad144f944 100644 --- a/base/essentials.jl +++ b/base/essentials.jl @@ -151,7 +151,7 @@ function length_checked_equal(args...) n end -map(f::Function, a::Array{Any,1}) = Any[ f(a[i]) for i=1:length(a) ] +map(f::Function, a::Array{Any,1}) = Any[ f(a[i]) for i in 1:length(a) ] function precompile(f::ANY, args::Tuple) if isa(f,DataType) @@ -219,16 +219,17 @@ start(v::SimpleVector) = 1 next(v::SimpleVector,i) = (v[i],i+1) done(v::SimpleVector,i) = (i > v.length) isempty(v::SimpleVector) = (v.length == 0) +eachindex(v::SimpleVector) = 1:length(v) function ==(v1::SimpleVector, v2::SimpleVector) length(v1)==length(v2) || return false - for i = 1:length(v1) + for i in eachindex(v1) v1[i] == v2[i] || return false end return true end -map(f, v::SimpleVector) = Any[ f(v[i]) for i = 1:length(v) ] +map(f, v::SimpleVector) = Any[ f(v[i]) for i in eachindex(v) ] getindex(v::SimpleVector, I::AbstractArray) = svec(Any[ v[i] for i in I ]...) diff --git a/base/expr.jl b/base/expr.jl index 9a322edc9c573..30bd6e893dd31 100644 --- a/base/expr.jl +++ b/base/expr.jl @@ -136,7 +136,7 @@ function popmeta!(body::Expr, sym::Symbol) return false, [] end metaargs = metaex.args - for i = 1:length(metaargs) + for i in eachindex(metaargs) if isa(metaargs[i], Symbol) && (metaargs[i]::Symbol) == sym deleteat!(metaargs, i) return true, [] diff --git a/base/fft/FFTW.jl b/base/fft/FFTW.jl index 694c9eafff451..2de548c107a06 100644 --- a/base/fft/FFTW.jl +++ b/base/fft/FFTW.jl @@ -441,7 +441,7 @@ function fix_kinds(region, kinds) else kinds = Int32[kinds...] end - for i = 1:length(kinds) + for i in eachindex(kinds) if kinds[i] < 0 || kinds[i] > 10 throw(ArgumentError("invalid transform kind")) end diff --git a/base/floatfuncs.jl b/base/floatfuncs.jl index f3442335f604b..8f146f072e581 100644 --- a/base/floatfuncs.jl +++ b/base/floatfuncs.jl @@ -69,7 +69,7 @@ round{T<:Integer}(::Type{T}, x::AbstractFloat, r::RoundingMode) = trunc(T,round( for f in (:trunc,:floor,:ceil,:round) @eval begin function ($f){T,R}(::Type{T}, x::AbstractArray{R,1}) - [ ($f)(T, x[i])::T for i = 1:length(x) ] + [ ($f)(T, x[i])::T for i in eachindex(x) ] end function ($f){T,R}(::Type{T}, x::AbstractArray{R,2}) [ ($f)(T, x[i,j])::T for i = 1:size(x,1), j = 1:size(x,2) ] @@ -78,7 +78,7 @@ for f in (:trunc,:floor,:ceil,:round) reshape([ ($f)(T, x[i])::T for i in eachindex(x) ], size(x)) end function ($f){R}(x::AbstractArray{R,1}, digits::Integer, base::Integer=10) - [ ($f)(x[i], digits, base) for i = 1:length(x) ] + [ ($f)(x[i], digits, base) for i in eachindex(x) ] end function ($f){R}(x::AbstractArray{R,2}, digits::Integer, base::Integer=10) [ ($f)(x[i,j], digits, base) for i = 1:size(x,1), j = 1:size(x,2) ] @@ -90,7 +90,7 @@ for f in (:trunc,:floor,:ceil,:round) end function round{R}(x::AbstractArray{R,1}, r::RoundingMode) - [ round(x[i], r) for i = 1:length(x) ] + [ round(x[i], r) for i in eachindex(x) ] end function round{R}(x::AbstractArray{R,2}, r::RoundingMode) [ round(x[i,j], r) for i = 1:size(x,1), j = 1:size(x,2) ] @@ -100,7 +100,7 @@ function round(x::AbstractArray, r::RoundingMode) end function round{T,R}(::Type{T}, x::AbstractArray{R,1}, r::RoundingMode) - [ round(T, x[i], r)::T for i = 1:length(x) ] + [ round(T, x[i], r)::T for i in eachindex(x) ] end function round{T,R}(::Type{T}, x::AbstractArray{R,2}, r::RoundingMode) [ round(T, x[i,j], r)::T for i = 1:size(x,1), j = 1:size(x,2) ] diff --git a/base/inference.jl b/base/inference.jl index 4961b1122b957..5fc1cb72f3f84 100644 --- a/base/inference.jl +++ b/base/inference.jl @@ -292,7 +292,7 @@ const getfield_tfunc = function (A, s0, name) end end snames = s.name.names - for i=1:length(snames) + for i in eachindex(snames) if is(snames[i],fld) R = s.types[i] if length(s.parameters) == 0 @@ -1201,7 +1201,7 @@ stupdate(state::Tuple{}, changes::VarTable, vars) = copy(changes) stupdate(state::Tuple{}, changes::StateUpdate, vars) = stupdate(ObjectIdDict(), changes, vars) function stupdate(state::ObjectIdDict, changes::Union{StateUpdate,VarTable}, vars) - for i = 1:length(vars) + for i in eachindex(vars) v = vars[i] newtype = changes[v] oldtype = get(state::ObjectIdDict,v,NF) @@ -1245,7 +1245,7 @@ genlabel(sv) = LabelNode(sv.label_counter += 1) function find_gensym_uses(body) uses = IntSet[] - for line = 1:length(body) + for line in eachindex(body) find_gensym_uses(body[line], uses, line) end return uses @@ -1448,7 +1448,7 @@ function typeinf_uncached(linfo::LambdaStaticData, atypes::ANY, sparams::SimpleV n = length(body) labels = zeros(Int, label_counter(body)+1) - for i=1:length(body) + for i in eachindex(body) b = body[i] if isa(b,LabelNode) labels[b.label+1] = i @@ -1530,7 +1530,7 @@ function typeinf_uncached(linfo::LambdaStaticData, atypes::ANY, sparams::SimpleV end gensym_uses = find_gensym_uses(body) - gensym_init = Any[ NF for i = 1:length(gensym_uses) ] + gensym_init = Any[ NF for i in eachindex(gensym_uses) ] gensym_types = copy(gensym_init) sv = StaticVarInfo(sparams, cenv, vars, gensym_types, vinflist, length(labels), ObjectIdDict()) @@ -1695,7 +1695,7 @@ function typeinf_uncached(linfo::LambdaStaticData, atypes::ANY, sparams::SimpleV frame.result = curtype @goto typeinf_top end - for i = 1:length(gensym_types) + for i in eachindex(gensym_types) if gensym_types[i] === NF gensym_types[i] = Union{} end @@ -1833,7 +1833,7 @@ function type_annotate(ast::Expr, states::Array{Any,1}, sv::ANY, rettype::ANY, a end closures = [] body = ast.args[3].args::Array{Any,1} - for i=1:length(body) + for i in eachindex(body) st_i = states[i] if st_i !== () # st_i === () => unreached statement (see issue #7836) @@ -1914,7 +1914,7 @@ function sym_replace(e::ANY, from1, from2, to1, to2) end e.args[2] = sym_replace(e.args[2], from1, from2, to1, to2) elseif e.head !== :line - for i=1:length(e.args) + for i in eachindex(e.args) e.args[i] = sym_replace(e.args[i], from1, from2, to1, to2) end end @@ -1922,12 +1922,12 @@ function sym_replace(e::ANY, from1, from2, to1, to2) end function _sym_repl(s::Union{Symbol,GenSym}, from1, from2, to1, to2, deflt) - for i=1:length(from1) + for i in eachindex(from1) if is(from1[i],s) return to1[i] end end - for i=1:length(from2) + for i in eachindex(from2) if is(from2[i],s) return to2[i] end @@ -2216,7 +2216,7 @@ function inlineable(f::ANY, e::Expr, atype::ANY, sv::StaticVarInfo, enclosing_as sp = meth[2]::SimpleVector sp = svec(sp..., linfo.sparams...) spvals = Any[ sp[i] for i in 2:2:length(sp) ] - for i=1:length(spvals) + for i in eachindex(spvals) si = spvals[i] if isa(si, TypeVar) return NF @@ -2540,7 +2540,7 @@ function inlineable(f::ANY, e::Expr, atype::ANY, sv::StaticVarInfo, enclosing_as # make labels / goto statements unique newlabels = zeros(Int,label_counter(body.args)+1) - for i = 1:length(body.args) + for i in eachindex(body.args) a = body.args[i] if isa(a,LabelNode) a = a::LabelNode @@ -2549,7 +2549,7 @@ function inlineable(f::ANY, e::Expr, atype::ANY, sv::StaticVarInfo, enclosing_as body.args[i] = newlabel end end - for i = 1:length(body.args) + for i in eachindex(body.args) a = body.args[i] if isa(a,GotoNode) a = a::GotoNode @@ -2641,7 +2641,7 @@ function gensym_increment(body::Expr, incr) if body.head === :line return body end - for i in 1:length(body.args) + for i in eachindex(body.args) body.args[i] = gensym_increment(body.args[i], incr) end return body @@ -2684,7 +2684,7 @@ function inlining_pass(e::Expr, sv, ast) eargs[i] = res[1] if isa(res[2],Array) sts = res[2]::Array{Any,1} - for j = 1:length(sts) + for j in eachindex(sts) insert!(eargs, i, sts[j]) i += 1 end @@ -2833,7 +2833,7 @@ function inlining_pass(e::Expr, sv, ast) elseif (t<:Tuple) && !isvatuple(t) && effect_free(aarg,sv,true) # apply(f,t::(x,y)) => f(t[1],t[2]) tp = t.parameters - newargs[i-3] = Any[ mk_getfield(aarg,j,tp[j]) for j=1:length(tp) ] + newargs[i-3] = Any[ mk_getfield(aarg,j,tp[j]) for j in eachindex(tp) ] else # not all args expandable return (e,stmts) @@ -3016,7 +3016,7 @@ function find_sa_vars(ast) av2 = ObjectIdDict() vinfos = ast.args[2][1]::Array{Any,1} args = ast.args[1] - for i = 1:length(body) + for i in eachindex(body) e = body[i] if isa(e,Expr) && is(e.head,:(=)) lhs = e.args[1] @@ -3084,7 +3084,7 @@ end # replace getfield(tuple(exprs...), i) with exprs[i] function getfield_elim_pass(e::Expr, sv) - for i = 1:length(e.args) + for i in eachindex(e.args) ei = e.args[i] if isa(ei,Expr) getfield_elim_pass(ei, sv) diff --git a/base/intfuncs.jl b/base/intfuncs.jl index 1825215f65ab6..a0e061e579f41 100644 --- a/base/intfuncs.jl +++ b/base/intfuncs.jl @@ -318,7 +318,7 @@ end function digits!{T<:Integer}(a::AbstractArray{T,1}, n::Integer, base::T=10) 2 <= base || throw(ArgumentError("base must be ≥ 2, got $base")) - for i = 1:length(a) + for i in eachindex(a) a[i] = rem(n, base) n = div(n, base) end diff --git a/base/linalg/bitarray.jl b/base/linalg/bitarray.jl index eee50e259145b..53c3777393020 100644 --- a/base/linalg/bitarray.jl +++ b/base/linalg/bitarray.jl @@ -187,7 +187,7 @@ function findmax(a::BitArray) m, mi = false, 1 ti = 1 ac = a.chunks - for i=1:length(ac) + for i in eachindex(ac) @inbounds k = trailing_zeros(ac[i]) ti += k k==64 || return (true, ti) diff --git a/base/linalg/diagonal.jl b/base/linalg/diagonal.jl index b586b6df810f9..96f6d41c66af3 100644 --- a/base/linalg/diagonal.jl +++ b/base/linalg/diagonal.jl @@ -99,7 +99,7 @@ function A_ldiv_B!{T}(D::Diagonal{T}, v::AbstractVector{T}) if length(v) != length(D.diag) throw(DimensionMismatch("diagonal matrix is $(length(D.diag)) by $(length(D.diag)) but right hand side has $(length(v)) rows")) end - for i=1:length(D.diag) + for i in eachindex(D.diag) d = D.diag[i] if d == zero(T) throw(SingularException(i)) @@ -112,7 +112,7 @@ function A_ldiv_B!{T}(D::Diagonal{T}, V::AbstractMatrix{T}) if size(V,1) != length(D.diag) throw(DimensionMismatch("diagonal matrix is $(length(D.diag)) by $(length(D.diag)) but right hand side has $(size(V,1)) rows")) end - for i=1:length(D.diag) + for i in eachindex(D.diag) d = D.diag[i] if d == zero(T) throw(SingularException(i)) @@ -165,7 +165,7 @@ end function inv{T}(D::Diagonal{T}) Di = similar(D.diag) - for i = 1:length(D.diag) + for i in eachindex(D.diag) if D.diag[i] == zero(T) throw(SingularException(i)) end @@ -176,7 +176,7 @@ end function pinv{T}(D::Diagonal{T}) Di = similar(D.diag) - for i = 1:length(D.diag) + for i in eachindex(D.diag) isfinite(inv(D.diag[i])) ? Di[i]=inv(D.diag[i]) : Di[i]=zero(T) end Diagonal(Di) @@ -184,7 +184,7 @@ end function pinv{T}(D::Diagonal{T}, tol::Real) Di = similar(D.diag) if( length(D.diag) != 0 ) maxabsD = maximum(abs(D.diag)) end - for i = 1:length(D.diag) + for i in eachindex(D.diag) if( abs(D.diag[i]) > tol*maxabsD && isfinite(inv(D.diag[i])) ) Di[i]=inv(D.diag[i]) else @@ -207,9 +207,9 @@ function svd{T<:Number}(D::Diagonal{T}) S = abs(D.diag) piv = sortperm(S, rev = true) U = full(Diagonal(D.diag ./ S)) - Up = hcat([U[:,i] for i = 1:length(D.diag)][piv]...) + Up = hcat([U[:,i] for i in eachindex(D.diag)][piv]...) V = eye(D) - Vp = hcat([V[:,i] for i = 1:length(D.diag)][piv]...) + Vp = hcat([V[:,i] for i in eachindex(D.diag)][piv]...) return (Up, S[piv], Vp) end function svdfact(D::Diagonal) diff --git a/base/linalg/generic.jl b/base/linalg/generic.jl index 33ff90cd07391..cb48609941a5f 100644 --- a/base/linalg/generic.jl +++ b/base/linalg/generic.jl @@ -16,7 +16,7 @@ scale{R<:Real}(s::Complex, X::AbstractArray{R}) = scale(X, s) # For better performance when input and output are the same array # See https://github.com/JuliaLang/julia/issues/8415#issuecomment-56608729 function generic_scale!(X::AbstractArray, s::Number) - for i = 1:length(X) + for i in eachindex(X) @inbounds X[i] *= s end X @@ -26,7 +26,7 @@ function generic_scale!(C::AbstractArray, X::AbstractArray, s::Number) if length(C) != length(X) throw(DimensionMismatch("first array has length $(length(C)) which does not match the length of the second, $(length(X)).")) end - for i = 1:length(X) + for i in eachindex(X) @inbounds C[i] = X[i]*s end C @@ -450,7 +450,7 @@ function axpy!{Ti<:Integer,Tj<:Integer}(alpha, x::AbstractArray, rx::AbstractArr elseif length(rx) != length(ry) throw(ArgumentError("rx has length $(length(rx)), but ry has length $(length(ry))")) end - for i = 1:length(rx) + for i in eachindex(rx) @inbounds y[ry[i]] += alpha * x[rx[i]] end y diff --git a/base/linalg/givens.jl b/base/linalg/givens.jl index cd0785560c491..3778be501d708 100644 --- a/base/linalg/givens.jl +++ b/base/linalg/givens.jl @@ -268,13 +268,13 @@ function A_mul_B!(G::Givens, R::Rotation) return R end function A_mul_B!(R::Rotation, A::AbstractMatrix) - @inbounds for i = 1:length(R.rotations) + @inbounds for i in eachindex(R.rotations) A_mul_B!(R.rotations[i], A) end return A end function A_mul_Bc!(A::AbstractMatrix, R::Rotation) - @inbounds for i = 1:length(R.rotations) + @inbounds for i in eachindex(R.rotations) A_mul_Bc!(A, R.rotations[i]) end return A diff --git a/base/linalg/lu.jl b/base/linalg/lu.jl index f3c9f9757dbc0..b9cb600b94ea8 100644 --- a/base/linalg/lu.jl +++ b/base/linalg/lu.jl @@ -107,7 +107,7 @@ size(A::LU,n) = size(A.factors,n) function ipiv2perm{T}(v::AbstractVector{T}, maxi::Integer) p = T[1:maxi;] - @inbounds for i in 1:length(v) + @inbounds for i in eachindex(v) p[i], p[v[i]] = p[v[i]], p[i] end return p diff --git a/base/linalg/symmetric.jl b/base/linalg/symmetric.jl index 408d882e91613..34bb515149797 100644 --- a/base/linalg/symmetric.jl +++ b/base/linalg/symmetric.jl @@ -168,7 +168,7 @@ eigvals!{T<:BlasComplex,S<:StridedMatrix}(A::Hermitian{T,S}, B::Hermitian{T,S}) function svdvals!{T<:Real,S}(A::Union{Hermitian{T,S}, Symmetric{T,S}, Hermitian{Complex{T},S}}) # the union is the same as RealHermSymComplexHerm, but right now parametric typealiases are broken vals = eigvals!(A) - for i = 1:length(vals) + for i in eachindex(vals) vals[i] = abs(vals[i]) end return sort!(vals, rev = true) diff --git a/base/linalg/tridiag.jl b/base/linalg/tridiag.jl index daa970d179982..23e42b07ac3e6 100644 --- a/base/linalg/tridiag.jl +++ b/base/linalg/tridiag.jl @@ -294,7 +294,7 @@ end full{T}(M::Tridiagonal{T}) = convert(Matrix{T}, M) function convert{T}(::Type{Matrix{T}}, M::Tridiagonal{T}) A = zeros(T, size(M)) - for i = 1:length(M.d) + for i in eachindex(M.d) A[i,i] = M.d[i] end for i = 1:length(M.d)-1 diff --git a/base/methodshow.jl b/base/methodshow.jl index 5e6c1a27d822f..bb3a2b7ad31be 100644 --- a/base/methodshow.jl +++ b/base/methodshow.jl @@ -35,7 +35,7 @@ function arg_decl_parts(m::Method) e = uncompressed_ast(li) argnames = e.args[1] s = symbol("?") - decls = [argtype_decl(get(argnames,i,s), m.sig.parameters[i]) for i=1:length(m.sig.parameters)] + decls = [argtype_decl(get(argnames,i,s), m.sig.parameters[i]) for i in eachindex(m.sig.parameters)] return tv, decls, li.file, li.line end diff --git a/base/multi.jl b/base/multi.jl index 58004f45d21ef..32a019ee3469c 100644 --- a/base/multi.jl +++ b/base/multi.jl @@ -116,7 +116,7 @@ type WorkerConfig function WorkerConfig() wc = new() - for n in 1:length(WorkerConfig.types) + for n in eachindex(WorkerConfig.types) T = eltype(fieldtype(WorkerConfig, n)) setfield!(wc, n, Nullable{T}()) end diff --git a/base/multidimensional.jl b/base/multidimensional.jl index 9b127007a0772..b580c92dc979d 100644 --- a/base/multidimensional.jl +++ b/base/multidimensional.jl @@ -34,6 +34,7 @@ length{N}(::Type{CartesianIndex{N}})=N # indexing getindex(index::CartesianIndex, i::Integer) = index.I[i] +eachindex(index::CartesianIndex) = 1:length(index) # arithmetic, min/max for op in (:+, :-, :min, :max) @@ -80,7 +81,7 @@ end startargs = fill(1, K) stopargs = Array(Expr, K) for i = 1:K - Bargs = [:(size(B[$j],$i)) for j = 1:length(B)] + Bargs = [:(size(B[$j],$i)) for j in eachindex(B)] stopargs[i] = :(max(size(A,$i),$(Bargs...))) end meta = Expr(:meta, :inline) @@ -351,7 +352,7 @@ function cartindex_exprs(indexes, syms) exprs = Any[] for (i,ind) in enumerate(indexes) if ind <: CartesianIndex - for j = 1:length(ind) + for j in 1:length(ind) push!(exprs, :($syms[$i][$j])) end else @@ -777,7 +778,7 @@ for (V, PT, BT) in [((:N,), BitArray, BitArray), ((:T,:N), Array, StridedArray)] length(perm) == N || throw(ArgumentError("expected permutation of size $N, but length(perm)=$(length(perm))")) isperm(perm) || throw(ArgumentError("input is not a permutation")) dimsP = size(P) - for i = 1:length(perm) + for i in eachindex(perm) dimsP[i] == dimsB[perm[i]] || throw(DimensionMismatch("destination tensor of incorrect size")) end diff --git a/base/number.jl b/base/number.jl index ff78f6913fb60..a9480045b17a6 100644 --- a/base/number.jl +++ b/base/number.jl @@ -10,6 +10,7 @@ eltype{T<:Number}(::Type{T}) = T ndims(x::Number) = 0 ndims{T<:Number}(::Type{T}) = 0 length(x::Number) = 1 +eachindex(x::Number) = 1 endof(x::Number) = 1 getindex(x::Number) = x getindex(x::Number, i::Integer) = i == 1 ? x : throw(BoundsError()) diff --git a/base/operators.jl b/base/operators.jl index 2b592d98ca19c..54421a50fb9b4 100644 --- a/base/operators.jl +++ b/base/operators.jl @@ -220,7 +220,7 @@ function promote_shape(a::Dims, b::Dims) if length(a) < length(b) return promote_shape(b, a) end - for i=1:length(b) + for i in eachindex(b) if a[i] != b[i] throw(DimensionMismatch("dimensions must match")) end @@ -371,7 +371,7 @@ end macro vectorize_1arg(S,f) S = esc(S); f = esc(f); T = esc(:T) quote - ($f){$T<:$S}(x::AbstractArray{$T,1}) = [ ($f)(x[i]) for i=1:length(x) ] + ($f){$T<:$S}(x::AbstractArray{$T,1}) = [ ($f)(x[i]) for i in eachindex(x) ] ($f){$T<:$S}(x::AbstractArray{$T,2}) = [ ($f)(x[i,j]) for i=1:size(x,1), j=1:size(x,2) ] ($f){$T<:$S}(x::AbstractArray{$T}) = diff --git a/base/profile.jl b/base/profile.jl index b61a055f68772..a36775579950c 100644 --- a/base/profile.jl +++ b/base/profile.jl @@ -257,7 +257,7 @@ function print_flat(io::IO, lilist::Vector{LineInfo}, n::Vector{Int}, combine::B wfunc = floor(Integer,3*ntext/5) end println(io, lpad("Count", wcounts, " "), " ", rpad("File", wfile, " "), " ", rpad("Function", wfunc, " "), " ", lpad("Line", wline, " ")) - for i = 1:length(n) + for i in eachindex(n) li = lilist[i] println(io, lpad(string(n[i]), wcounts, " "), " ", rpad(truncto(li.file, wfile), wfile, " "), " ", rpad(truncto(li.func, wfunc), wfunc, " "), " ", lpad(string(li.line), wline, " ")) end @@ -301,7 +301,7 @@ function tree_format(lilist::Vector{LineInfo}, counts::Vector{Int}, level::Int, nindent -= ndigits(nextra)+2 showextra = true end - for i = 1:length(lilist) + for i in eachindex(lilist) li = lilist[i] if li != UNKNOWN base = " "^nindent @@ -343,7 +343,7 @@ function tree{T<:Unsigned}(io::IO, bt::Vector{Vector{T}}, counts::Vector{Int}, l if combine # Combine based on the line information d = Dict{LineInfo,Vector{Int}}() - for i = 1:length(bt) + for i in eachindex(bt) ip = bt[i][level+1] key = lidict[ip] indx = Base.ht_keyindex(d, key) @@ -368,7 +368,7 @@ function tree{T<:Unsigned}(io::IO, bt::Vector{Vector{T}}, counts::Vector{Int}, l else # Combine based on the instruction pointer d = Dict{T,Vector{Int}}() - for i = 1:length(bt) + for i in eachindex(bt) key = bt[i][level+1] indx = Base.ht_keyindex(d, key) if indx == -1 @@ -401,7 +401,7 @@ function tree{T<:Unsigned}(io::IO, bt::Vector{Vector{T}}, counts::Vector{Int}, l strs = tree_format(lilist, n, level, cols) # Recurse to the next level len = Int[length(x) for x in bt] - for i = 1:length(lilist) + for i in eachindex(lilist) if !isempty(strs[i]) println(io, strs[i]) end @@ -465,7 +465,7 @@ end # Order alphabetically (file, function) and then by line number function liperm(lilist::Vector{LineInfo}) comb = Array(ByteString, length(lilist)) - for i = 1:length(lilist) + for i in eachindex(lilist) li = lilist[i] if li != UNKNOWN comb[i] = @sprintf("%s:%s:%06d", li.file, li.func, li.line) diff --git a/base/quadgk.jl b/base/quadgk.jl index 95696f125896a..b4684c082c724 100644 --- a/base/quadgk.jl +++ b/base/quadgk.jl @@ -169,10 +169,10 @@ end # all the integration-segment endpoints function quadgk(f, a, b, c...; kws...) T = promote_type(typeof(float(a)), typeof(b)) - for i in 1:length(c) + for i in eachindex(c) T = promote_type(T, typeof(c[i])) end - cT = T[ c[i] for i in 1:length(c) ] + cT = T[ c[i] for i in eachindex(c) ] quadgk(f, convert(T, a), convert(T, b), cT...; kws...) end diff --git a/base/reduce.jl b/base/reduce.jl index ce34985e74d46..2e6c72cc4b41b 100644 --- a/base/reduce.jl +++ b/base/reduce.jl @@ -397,7 +397,7 @@ end function count(pred, a::AbstractArray) n = 0 - for i = 1:length(a) + for i in eachindex(a) @inbounds if pred(a[i]) n += 1 end diff --git a/base/regex.jl b/base/regex.jl index ce9e3d958b58c..896fade61bafd 100644 --- a/base/regex.jl +++ b/base/regex.jl @@ -100,7 +100,7 @@ function show(io::IO, m::RegexMatch) idx_to_capture_name = PCRE.capture_names(m.regex.regex) if !isempty(m.captures) print(io, ", ") - for i = 1:length(m.captures) + for i in eachindex(m.captures) # If the capture group is named, show the name. # Otherwise show its index. capture_name = get(idx_to_capture_name, i, i) diff --git a/base/replutil.jl b/base/replutil.jl index 7ea18fdc20531..d4058af53f2a2 100644 --- a/base/replutil.jl +++ b/base/replutil.jl @@ -366,7 +366,7 @@ function process_backtrace(process_func::Function, top_function::Symbol, t, set) lastfile = ""; lastline = -11; lastname = symbol("#") local fname, file, line count = 0 - for i = 1:length(t) + for i in eachindex(t) lkup = ccall(:jl_lookup_code_address, Any, (Ptr{Void}, Cint), t[i]-1, true) if lkup === nothing continue diff --git a/base/serialize.jl b/base/serialize.jl index afd57c6fd6bec..ead3fe46b523f 100644 --- a/base/serialize.jl +++ b/base/serialize.jl @@ -123,7 +123,7 @@ end function serialize(s::SerializationState, v::SimpleVector) writetag(s.io, SIMPLEVECTOR_TAG) write(s.io, Int32(length(v))) - for i = 1:length(v) + for i in eachindex(v) serialize(s.io, v[i]) end end @@ -180,7 +180,7 @@ function serialize(s::SerializationState, a::Array) if isbits(elty) serialize_array_data(s.io, a) else - for i = 1:length(a) + for i in eachindex(a) if isdefined(a, i) serialize(s, a[i]) else @@ -595,7 +595,7 @@ function deserialize_array(s::SerializationState) end A = Array(elty, dims) deserialize_cycle(s, A) - for i = 1:length(A) + for i in eachindex(A) tag = Int32(read(s.io, UInt8)::UInt8) if tag != UNDEFREF_TAG A[i] = handle_deserialize(s, tag) diff --git a/base/sharedarray.jl b/base/sharedarray.jl index 899d5f2214770..73792705caea7 100644 --- a/base/sharedarray.jl +++ b/base/sharedarray.jl @@ -55,7 +55,7 @@ function SharedArray(T::Type, dims::NTuple; init=false, pids=Int[]) end # Wait till all the workers have mapped the segment - for i in 1:length(refs) + for i in eachindex(refs) wait(refs[i]) end @@ -138,7 +138,7 @@ function SharedArray{T,N}(filename::AbstractString, ::Type{T}, dims::NTuple{N,In end # Wait till all the workers have mapped the segment - for i in 1:length(refs) + for i in eachindex(refs) wait(refs[i]) end diff --git a/base/show.jl b/base/show.jl index 2773ba74cf6f7..3e8cf86abf9ff 100644 --- a/base/show.jl +++ b/base/show.jl @@ -1004,7 +1004,7 @@ function print_matrix_row(io::IO, X::AbstractVecOrMat, A::Vector, i::Integer, cols::AbstractVector, sep::AbstractString ) - for k = 1:length(A) + for k in eachindex(A) j = cols[k] if isassigned(X,i,j) x = X[i,j] @@ -1024,7 +1024,7 @@ end function print_matrix_vdots(io::IO, vdots::AbstractString, A::Vector, sep::AbstractString, M::Integer, m::Integer ) - for k = 1:length(A) + for k in eachindex(A) w = A[k][1] + A[k][2] if k % M == m l = repeat(" ", max(0, A[k][1]-length(vdots))) diff --git a/base/sort.jl b/base/sort.jl index 792d2db587cec..ed21828d03707 100644 --- a/base/sort.jl +++ b/base/sort.jl @@ -417,7 +417,7 @@ function selectperm!{I<:Integer}(ix::AbstractVector{I}, v::AbstractVector, order::Ordering=Forward, initialized::Bool=false) if !initialized - @inbounds for i = 1:length(ix) + @inbounds for i in eachindex(ix) ix[i] = i end end diff --git a/base/sparse/cholmod.jl b/base/sparse/cholmod.jl index b80ada8055450..572493e22a736 100644 --- a/base/sparse/cholmod.jl +++ b/base/sparse/cholmod.jl @@ -830,10 +830,10 @@ end function convert{Tv<:VTypes}(::Type{Sparse}, A::SparseMatrixCSC{Tv,SuiteSparse_long}, stype::Integer) o = allocate_sparse(A.m, A.n, length(A.nzval), true, true, stype, Tv) s = unsafe_load(o.p) - for i = 1:length(A.colptr) + for i in eachindex(A.colptr) unsafe_store!(s.p, A.colptr[i] - 1, i) end - for i = 1:length(A.rowval) + for i in eachindex(A.rowval) unsafe_store!(s.i, A.rowval[i] - 1, i) end unsafe_copy!(s.x, pointer(A.nzval), length(A.nzval)) @@ -987,7 +987,7 @@ function sparse(F::Factor) p = get_perm(F) if p != [1:s.n;] pinv = Array(Int, length(p)) - for k = 1:length(p) + for k in eachindex(p) pinv[p[k]] = k end A = A[pinv,pinv] @@ -1115,7 +1115,7 @@ function getLd!(S::SparseMatrixCSC) d = Array(eltype(S), size(S, 1)) fill!(d, 0) col = 1 - for k = 1:length(S.nzval) + for k in eachindex(S.nzval) while k >= S.colptr[col+1] col += 1 end diff --git a/base/sparse/csparse.jl b/base/sparse/csparse.jl index 990935939fc73..057f912dab098 100644 --- a/base/sparse/csparse.jl +++ b/base/sparse/csparse.jl @@ -111,7 +111,7 @@ function sparse{Tv,Ti<:Integer}(I::AbstractVector{Ti}, RpT = cumsum(Wj[1:(ncol+1)]) # Transpose - @simd for i=1:length(RpT); @inbounds Wj[i] = RpT[i]; end + @simd for i in eachindex(RpT); @inbounds Wj[i] = RpT[i]; end @inbounds for j = 1:nrow p1 = Rp[j] p2 = p1 + Rnz[j] - 1 diff --git a/base/sparse/linalg.jl b/base/sparse/linalg.jl index b799060019b12..5149fc3b135d5 100644 --- a/base/sparse/linalg.jl +++ b/base/sparse/linalg.jl @@ -6,14 +6,14 @@ import Base.LinAlg: chksquare # Convert from 1-based to 0-based indices function decrement!{T<:Integer}(A::AbstractArray{T}) - for i in 1:length(A) A[i] -= one(T) end + for i in eachindex(A) A[i] -= one(T) end A end decrement{T<:Integer}(A::AbstractArray{T}) = decrement!(copy(A)) # Convert from 0-based to 1-based indices function increment!{T<:Integer}(A::AbstractArray{T}) - for i in 1:length(A) A[i] += one(T) end + for i in eachindex(A) A[i] += one(T) end A end increment{T<:Integer}(A::AbstractArray{T}) = increment!(copy(A)) @@ -477,7 +477,7 @@ function norm(A::SparseMatrixCSC,p::Real=2) throw(ArgumentError("2-norm not yet implemented for sparse matrices. Try norm(full(A)) or norm(A, p) where p=1 or Inf.")) elseif p==Inf rowSum = zeros(Tsum,m) - for i=1:length(A.nzval) + for i in eachindex(A.nzval) rowSum[A.rowval[i]] += abs(A.nzval[i]) end return convert(Tnorm, maximum(rowSum)) @@ -526,13 +526,13 @@ function normestinv{T}(A::SparseMatrixCSC{T}, t::Integer = min(2,maximum(size(A) S = zeros(T <: Real ? Int : Ti, n, t) function _rand_pm1!(v) - for i = 1:length(v) + for i in eachindex(v) v[i] = rand()<0.5?1:-1 end end function _any_abs_eq(v,n::Int) - for i = 1:length(v) + for i in eachindex(v) if abs(v[i])==n return true end diff --git a/base/sparse/sparsematrix.jl b/base/sparse/sparsematrix.jl index 80e30d9ae7f52..ddf7573de202f 100644 --- a/base/sparse/sparsematrix.jl +++ b/base/sparse/sparsematrix.jl @@ -564,7 +564,7 @@ for op in (:-, :log1p, :expm1) B = similar(A) nzvalB = B.nzval nzvalA = A.nzval - @simd for i=1:length(nzvalB) + @simd for i in eachindex(nzvalB) @inbounds nzvalB[i] = ($op)(nzvalA[i]) end return B @@ -585,7 +585,7 @@ for op in (:abs, :abs2) B = similar(A) nzvalB = B.nzval nzvalA = A.nzval - @simd for i=1:length(nzvalB) + @simd for i in eachindex(nzvalB) @inbounds nzvalB[i] = ($op)(nzvalA[i]) end return B @@ -595,7 +595,7 @@ end function conj!(A::SparseMatrixCSC) nzvalA = A.nzval - @simd for i=1:length(nzvalA) + @simd for i in eachindex(nzvalA) @inbounds nzvalA[i] = conj(nzvalA[i]) end return A @@ -2714,7 +2714,7 @@ end function rot180(A::SparseMatrixCSC) I,J,V = findnz(A) m,n = size(A) - for i=1:length(I) + for i in eachindex(I) I[i] = m - I[i] + 1 J[i] = n - J[i] + 1 end @@ -2725,7 +2725,7 @@ function rotr90(A::SparseMatrixCSC) I,J,V = findnz(A) m,n = size(A) #old col inds are new row inds - for i=1:length(I) + for i in eachindex(I) I[i] = m - I[i] + 1 end return sparse(J, I, V, n, m) @@ -2735,7 +2735,7 @@ function rotl90(A::SparseMatrixCSC) I,J,V = findnz(A) m,n = size(A) #old row inds are new col inds - for i=1:length(J) + for i in eachindex(J) J[i] = n - J[i] + 1 end return sparse(J, I, V, n, m) diff --git a/base/sparse/umfpack.jl b/base/sparse/umfpack.jl index b36cfced934e1..ce3182f61cf7f 100644 --- a/base/sparse/umfpack.jl +++ b/base/sparse/umfpack.jl @@ -336,21 +336,21 @@ A_ldiv_B!{T<:UMFVTypes}(lu::UmfpackLU{T}, b::Matrix{T}) = solve(lu, b, UMFPACK_A function A_ldiv_B!{Tb<:Complex}(lu::UmfpackLU{Float64}, b::Vector{Tb}) r = solve(lu, [convert(Tlu,real(be)) for be in b], UMFPACK_A) i = solve(lu, [convert(Tlu,imag(be)) for be in b], UMFPACK_A) - Tb[r[k]+im*i[k] for k = 1:length(r)] + Tb[r[k]+im*i[k] for k in eachindex(r)] end Ac_ldiv_B!{T<:UMFVTypes}(lu::UmfpackLU{T}, b::VecOrMat{T}) = solve(lu, b, UMFPACK_At) function Ac_ldiv_B!{Tb<:Complex}(lu::UmfpackLU{Float64}, b::Vector{Tb}) r = solve(lu, [convert(Float64,real(be)) for be in b], UMFPACK_At) i = solve(lu, [convert(Float64,imag(be)) for be in b], UMFPACK_At) - Tb[r[k]+im*i[k] for k = 1:length(r)] + Tb[r[k]+im*i[k] for k in eachindex(r)] end At_ldiv_B!{T<:UMFVTypes}(lu::UmfpackLU{T}, b::VecOrMat{T}) = solve(lu, b, UMFPACK_Aat) function At_ldiv_B!{Tb<:Complex}(lu::UmfpackLU{Float64}, b::Vector{Tb}) r = solve(lu, [convert(Float64,real(be)) for be in b], UMFPACK_Aat) i = solve(lu, [convert(Float64,imag(be)) for be in b], UMFPACK_Aat) - Tb[r[k]+im*i[k] for k = 1:length(r)] + Tb[r[k]+im*i[k] for k in eachindex(r)] end function getindex(lu::UmfpackLU, d::Symbol) diff --git a/base/subarray.jl b/base/subarray.jl index 6d874f899c9f2..123309743a539 100644 --- a/base/subarray.jl +++ b/base/subarray.jl @@ -65,7 +65,7 @@ slice_unsafe(A::AbstractArray, J) = _slice_unsafe(A, to_indexes(J...)) N = 0 sizeexprs = Array(Any, 0) Jp = J.parameters - for Jindex = 1:length(Jp) + for Jindex in eachindex(Jp) j = Jp[Jindex] if !(j <: Real) N += 1 @@ -101,7 +101,7 @@ sub_unsafe(A::AbstractArray, J) = _sub_unsafe(A, to_indexes(J...)) while N > 0 && Jp[N] <: Real N -= 1 end - for Jindex = 1:length(Jp) + for Jindex in eachindex(Jp) j = Jp[Jindex] if Jindex <= N push!(sizeexprs, dimsizeexpr(j, Jindex, length(Jp), :A, :J)) @@ -150,7 +150,7 @@ end LD, die_next_vector, jprev, isLDdone = 0, false, Void, false # for linear indexing inference Jindex = 0 IVp = IV.parameters - for IVindex = 1:length(IVp) + for IVindex in eachindex(IVp) iv = IVp[IVindex] if iv <: Real push!(indexexprs, :(V.indexes[$IVindex])) @@ -233,7 +233,7 @@ end preexprs = Array(Any, 0) LD, die_next_vector, jprev, isLDdone = 0, false, Void, false Jindex = 0 - for IVindex = 1:length(IVp) + for IVindex in eachindex(IVp) iv = IVp[IVindex] if iv <: Real push!(indexexprs, :(V.indexes[$IVindex])) @@ -361,7 +361,7 @@ in(::Int, ::Colon) = true strideexprs[1] = 1 i = 1 Vdim = 1 - for i = 1:length(Ip) + for i in eachindex(Ip) if Ip[i] != Int strideexprs[Vdim+1] = copy(strideexprs[Vdim]) strideexprs[Vdim] = :(step(V.indexes[$i])*$(strideexprs[Vdim])) @@ -420,7 +420,7 @@ first_index(V::SubArray) = first_index(V.parent, V.indexes) function first_index(P::AbstractArray, indexes::Tuple) f = 1 s = 1 - for i = 1:length(indexes) + for i in eachindex(indexes) f += (first(indexes[i])-1)*s s *= size(P, i) end @@ -500,7 +500,7 @@ pointer(V::SubArray, i::Int) = pointer(V, ind2sub(size(V), i)) function pointer{T,N,P<:Array,I<:Tuple{Vararg{RangeIndex}}}(V::SubArray{T,N,P,I}, is::Tuple{Vararg{Int}}) index = first_index(V) strds = strides(V) - for d = 1:length(is) + for d in eachindex(is) index += (is[d]-1)*strds[d] end return pointer(V.parent, index) diff --git a/base/sysinfo.jl b/base/sysinfo.jl index 860b2d40ace6b..5ad1d18af9478 100644 --- a/base/sysinfo.jl +++ b/base/sysinfo.jl @@ -113,7 +113,7 @@ function cpu_info() count = Array(Int32,1) uv_error("uv_cpu_info",ccall(:uv_cpu_info, Int32, (Ptr{Ptr{UV_cpu_info_t}}, Ptr{Int32}), UVcpus, count)) cpus = Array(CPUinfo,count[1]) - for i = 1:length(cpus) + for i in eachindex(cpus) cpus[i] = CPUinfo(unsafe_load(UVcpus[1],i)) end ccall(:uv_free_cpu_info, Void, (Ptr{UV_cpu_info_t}, Int32), UVcpus[1], count[1]) diff --git a/base/test.jl b/base/test.jl index 72bd92a9918b2..d2ffd287b2ecf 100644 --- a/base/test.jl +++ b/base/test.jl @@ -81,10 +81,10 @@ end macro test(ex) if typeof(ex) == Expr && ex.head == :comparison - syms = [gensym() for i = 1:length(ex.args)] + syms = [gensym() for i in eachindex(ex.args)] func_block = Expr(:block) # insert assignment into a block - func_block.args = [:($(syms[i]) = $(esc(ex.args[i]))) for i = 1:length(ex.args)] + func_block.args = [:($(syms[i]) = $(esc(ex.args[i]))) for i in eachindex(ex.args)] # finish the block with a return push!(func_block.args, Expr(:return, :(Expr(:comparison, $(syms...)), $(Expr(:comparison, syms...))))) :(do_test(()->($func_block), $(Expr(:quote,ex)))) @@ -124,7 +124,7 @@ function test_approx_eq(va, vb, Eps, astr, bstr) "\n ", bstr, " (length $(length(vb))) = ", vb) end diff = real(zero(eltype(va))) - for i = 1:length(va) + for i in eachindex(va) xa = va[i]; xb = vb[i] if isfinite(xa) && isfinite(xb) diff = max(diff, abs(xa-xb)) diff --git a/base/tuple.jl b/base/tuple.jl index e77abd8e328d9..9d698c737a4ce 100644 --- a/base/tuple.jl +++ b/base/tuple.jl @@ -15,6 +15,7 @@ getindex(t::Tuple, b::AbstractArray{Bool}) = getindex(t,find(b)) start(t::Tuple) = 1 done(t::Tuple, i::Int) = (length(t) < i) next(t::Tuple, i::Int) = (t[i], i+1) +eachindex(t::Tuple) = 1:length(t) # this allows partial evaluation of bounded sequences of next() calls on tuples, # while reducing to plain next() for arbitrary iterables. @@ -75,7 +76,7 @@ function isequal(t1::Tuple, t2::Tuple) if length(t1) != length(t2) return false end - for i = 1:length(t1) + for i in eachindex(t1) if !isequal(t1[i], t2[i]) return false end @@ -87,7 +88,7 @@ function ==(t1::Tuple, t2::Tuple) if length(t1) != length(t2) return false end - for i = 1:length(t1) + for i in eachindex(t1) if !(t1[i] == t2[i]) return false end diff --git a/base/unicode/utf32.jl b/base/unicode/utf32.jl index e936520e22bc5..458c12ceffe29 100644 --- a/base/unicode/utf32.jl +++ b/base/unicode/utf32.jl @@ -210,7 +210,7 @@ function convert(T::Type{UTF32String}, bytes::AbstractArray{UInt8}) end function isvalid(::Type{UTF32String}, str::Union{Vector{UInt32}, Vector{Char}}) - for i=1:length(str) + for i in eachindex(str) @inbounds if !isvalid(Char, UInt32(str[i])) ; return false ; end end return true diff --git a/base/unicode/utf8.jl b/base/unicode/utf8.jl index f3196b8e5679b..96b83b15d0df1 100644 --- a/base/unicode/utf8.jl +++ b/base/unicode/utf8.jl @@ -41,7 +41,7 @@ end function length(s::UTF8String) d = s.data cnum = 0 - for i = 1:length(d) + for i in eachindex(d) @inbounds cnum += !is_valid_continuation(d[i]) end cnum