Permalink
Browse files

switch to new comprehension syntax in base/ and examples/

closes #744
  • Loading branch information...
1 parent f5a2190 commit b5283f5b5ad6e3258409fd57772b70d6b0eefddd @JeffBezanson JeffBezanson committed May 2, 2012
View
@@ -197,8 +197,8 @@ end
function gen_cartesian_map(cache, genbodies, ranges, exargnames, exargs...)
N = length(ranges)
if !has(cache,N)
- dimargnames = { gensym() | i=1:N }
- ivars = { gensym() | i=1:N }
+ dimargnames = { gensym() for i=1:N }
+ ivars = { gensym() for i=1:N }
bodies = genbodies(ivars)
## creating a 2d array, to pass as bodies
@@ -318,15 +318,15 @@ vcat() = Array(None, 0)
hcat() = Array(None, 0)
## cat: special cases
-hcat{T}(X::T...) = [ X[j] | i=1, j=1:length(X) ]
-vcat{T}(X::T...) = [ X[i] | i=1:length(X) ]
+hcat{T}(X::T...) = [ X[j] for i=1, j=1:length(X) ]
+vcat{T}(X::T...) = [ X[i] for i=1:length(X) ]
function hcat{T}(V::AbstractVector{T}...)
height = length(V[1])
for j = 2:length(V)
if length(V[j]) != height; error("hcat: mismatched dimensions"); end
end
- [ V[j][i]::T | i=1:length(V[1]), j=1:length(V) ]
+ [ V[j][i]::T for i=1:length(V[1]), j=1:length(V) ]
end
function vcat{T}(V::AbstractVector{T}...)
@@ -718,7 +718,7 @@ function sub2ind(dims, I::Integer...)
end
sub2ind(dims, I::AbstractVector...) =
- [ sub2ind(dims, map(X->X[i], I)...) | i=1:length(I[1]) ]
+ [ sub2ind(dims, map(X->X[i], I)...) for i=1:length(I[1]) ]
ind2sub(dims::(Integer...), ind::Integer) = ind2sub(dims, int(ind))
ind2sub(dims::(), ind::Integer) = throw(BoundsError())
@@ -829,9 +829,9 @@ function bsxfun(f, a::AbstractArray, b::AbstractArray)
end
c = Array(promote_type(eltype(a),eltype(b)), shp...)
- aidxs = { 1:size(a,i) | i=1:nd }
- bidxs = { 1:size(b,i) | i=1:nd }
- cidxs = { 1:size(c,i) | i=1:nd }
+ aidxs = { 1:size(a,i) for i=1:nd }
+ bidxs = { 1:size(b,i) for i=1:nd }
+ cidxs = { 1:size(c,i) for i=1:nd }
sliceop = function (idxs::Int...)
j = 1
View
@@ -187,12 +187,12 @@ function ref(A::Array, I::Integer...)
end
# note: this is also useful for Ranges
-ref{T<:Integer}(A::Vector, I::AbstractVector{T}) = [ A[i] | i=I ]
-ref{T<:Integer}(A::AbstractVector, I::AbstractVector{T}) = [ A[i] | i=I ]
+ref{T<:Integer}(A::Vector, I::AbstractVector{T}) = [ A[i] for i=I ]
+ref{T<:Integer}(A::AbstractVector, I::AbstractVector{T}) = [ A[i] for i=I ]
-ref{T<:Integer}(A::Matrix, I::AbstractVector{T}, j::Integer) = [ A[i,j] | i=I ]
-ref{T<:Integer}(A::Matrix, I::Integer, J::AbstractVector{T}) = [ A[i,j] | i=I, j=J ]
-ref{T<:Integer}(A::Matrix, I::AbstractVector{T}, J::AbstractVector{T}) = [ A[i,j] | i=I, j=J ]
+ref{T<:Integer}(A::Matrix, I::AbstractVector{T}, j::Integer) = [ A[i,j] for i=I ]
+ref{T<:Integer}(A::Matrix, I::Integer, J::AbstractVector{T}) = [ A[i,j] for i=I, j=J ]
+ref{T<:Integer}(A::Matrix, I::AbstractVector{T}, J::AbstractVector{T}) = [ A[i,j] for i=I, j=J ]
let ref_cache = nothing
global ref
@@ -597,13 +597,13 @@ end
# ^ is difficult, since negative exponents give a different type
-./(x::Array, y::Array ) = reshape( [ x[i] ./ y[i] | i=1:numel(x) ], size(x) )
-./(x::Number,y::Array ) = reshape( [ x ./ y[i] | i=1:numel(y) ], size(y) )
-./(x::Array, y::Number) = reshape( [ x[i] ./ y | i=1:numel(x) ], size(x) )
+./(x::Array, y::Array ) = reshape( [ x[i] ./ y[i] for i=1:numel(x) ], size(x) )
+./(x::Number,y::Array ) = reshape( [ x ./ y[i] for i=1:numel(y) ], size(y) )
+./(x::Array, y::Number) = reshape( [ x[i] ./ y for i=1:numel(x) ], size(x) )
-.^(x::Array, y::Array ) = reshape( [ x[i] ^ y[i] | i=1:numel(x) ], size(x) )
-.^(x::Number,y::Array ) = reshape( [ x ^ y[i] | i=1:numel(y) ], size(y) )
-.^(x::Array, y::Number) = reshape( [ x[i] ^ y | i=1:numel(x) ], size(x) )
+.^(x::Array, y::Array ) = reshape( [ x[i] ^ y[i] for i=1:numel(x) ], size(x) )
+.^(x::Number,y::Array ) = reshape( [ x ^ y[i] for i=1:numel(y) ], size(y) )
+.^(x::Array, y::Number) = reshape( [ x[i] ^ y for i=1:numel(x) ], size(x) )
function .^{S<:Integer,T<:Integer}(A::Array{S}, B::Array{T})
F = Array(Float64, promote_shape(size(A), size(B)))
@@ -849,7 +849,7 @@ rotr90(A::AbstractMatrix, k::Integer) = rotl90(A,-k)
rot180(A::AbstractMatrix, k::Integer) = k % 2 == 1 ? rot180(A) : copy(A)
const rot90 = rotl90
-reverse(v::StridedVector) = (n=length(v); [ v[n-i+1] | i=1:n ])
+reverse(v::StridedVector) = (n=length(v); [ v[n-i+1] for i=1:n ])
function reverse!(v::StridedVector)
n = length(v)
r = n
@@ -904,7 +904,7 @@ end
let findn_cache = nothing
function findn_one(ivars)
- s = { quote I[$i][count] = $ivars[i] end | i = 1:length(ivars)}
+ s = { quote I[$i][count] = $ivars[i] end for i = 1:length(ivars)}
quote
Aind = A[$(ivars...)]
if Aind != z
@@ -977,11 +977,11 @@ areduce{T}(f::Function, A::StridedArray{T}, region::Region, v0) =
let areduce_cache = nothing
# generate the body of the N-d loop to compute a reduction
function gen_areduce_func(n, f)
- ivars = { gensym() | i=1:n }
+ ivars = { gensym() for i=1:n }
# limits and vars for reduction loop
- lo = { gensym() | i=1:n }
- hi = { gensym() | i=1:n }
- rvars = { gensym() | i=1:n }
+ lo = { gensym() for i=1:n }
+ hi = { gensym() for i=1:n }
+ rvars = { gensym() for i=1:n }
setlims = { quote
# each dim of reduction is either 1:sizeA or ivar:ivar
if contains(region,$i)
@@ -990,8 +990,8 @@ function gen_areduce_func(n, f)
else
$lo[i] = $hi[i] = $ivars[i]
end
- end | i=1:n }
- rranges = { :( ($lo[i]):($hi[i]) ) | i=1:n } # lo:hi for all dims
+ end for i=1:n }
+ rranges = { :( ($lo[i]):($hi[i]) ) for i=1:n } # lo:hi for all dims
body =
quote
_tot = v0
@@ -1005,7 +1005,7 @@ function gen_areduce_func(n, f)
local _F_
function _F_(f, A, region, R, v0)
_ind = 1
- $make_loop_nest(ivars, { :(1:size(R,$i)) | i=1:n }, body)
+ $make_loop_nest(ivars, { :(1:size(R,$i)) for i=1:n }, body)
end
_F_
end
@@ -1267,19 +1267,19 @@ function transpose{T<:Union(Float64,Float32,Complex128,Complex64)}(A::Matrix{T})
if numel(A) > 50000
return _jl_fftw_transpose(reshape(A, size(A, 2), size(A, 1)))
else
- return [ A[j,i] | i=1:size(A,2), j=1:size(A,1) ]
+ return [ A[j,i] for i=1:size(A,2), j=1:size(A,1) ]
end
end
ctranspose{T<:Real}(A::StridedVecOrMat{T}) = transpose(A)
ctranspose(x::StridedVecOrMat) = transpose(x)
-transpose(x::StridedVector) = [ x[j] | i=1, j=1:size(x,1) ]
-transpose(x::StridedMatrix) = [ x[j,i] | i=1:size(x,2), j=1:size(x,1) ]
+transpose(x::StridedVector) = [ x[j] for i=1, j=1:size(x,1) ]
+transpose(x::StridedMatrix) = [ x[j,i] for i=1:size(x,2), j=1:size(x,1) ]
-ctranspose{T<:Number}(x::StridedVector{T}) = [ conj(x[j]) | i=1, j=1:size(x,1) ]
-ctranspose{T<:Number}(x::StridedMatrix{T}) = [ conj(x[j,i]) | i=1:size(x,2), j=1:size(x,1) ]
+ctranspose{T<:Number}(x::StridedVector{T}) = [ conj(x[j]) for i=1, j=1:size(x,1) ]
+ctranspose{T<:Number}(x::StridedMatrix{T}) = [ conj(x[j,i]) for i=1:size(x,2), j=1:size(x,1) ]
## Permute ##
@@ -1296,7 +1296,7 @@ function permute(A::StridedArray, perm)
end
#calculates all the strides
- strides = [ stride(A, perm[dim]) | dim = 1:length(perm) ]
+ strides = [ stride(A, perm[dim]) for dim = 1:length(perm) ]
#Creates offset, because indexing starts at 1
offset = 0
@@ -1307,7 +1307,7 @@ function permute(A::StridedArray, perm)
function permute_one(ivars)
len = length(ivars)
- counts = { gensym() | i=1:len}
+ counts = { gensym() for i=1:len}
toReturn = cell(len+1,2)
for i = 1:numel(toReturn)
toReturn[i] = nothing
View
@@ -36,12 +36,12 @@ function cell_2d(nr, nc, xs::ANY...)
end
# map cell array
-map(f, a::Array{Any,1}) = { f(a[i]) | i=1:length(a) }
+map(f, a::Array{Any,1}) = { f(a[i]) for i=1:length(a) }
map(f, a::Array{Any,1}, b::Array{Any,1}) =
- { f(a[i],b[i]) | i=1:length_checked_equal(a, b) }
+ { f(a[i],b[i]) for i=1:length_checked_equal(a, b) }
function map(f, as::Array{Any,1}...)
n = length_checked_equal(as...)
- { f(map(a->a[i],as)...) | i=1:n }
+ { f(map(a->a[i],as)...) for i=1:n }
end
cell(dims::(Integer...)) = Array(Any, map(int, dims))
@@ -60,7 +60,7 @@ end
const nCr = binomial
-pascal(n) = [binomial(i+j-2,i-1) | i=1:n,j=1:n]
+pascal(n) = [binomial(i+j-2,i-1) for i=1:n,j=1:n]
## other ordering related functions ##
@@ -137,7 +137,7 @@ function combinations(a::AbstractVector, t::Integer)
else
while true
# T2
- produce([ a[c[i]+1] | i=1:t ])
+ produce([ a[c[i]+1] for i=1:t ])
if j > 0
x = j
@@ -236,7 +236,7 @@ function partitions{T}(s::AbstractVector{T})
while true
# H2
# convert from restricted growth string a[1:n] to set of sets
- temp = [ Array(T,0) | k = 1:n ]
+ temp = [ Array(T,0) for k = 1:n ]
for k = 1:n
push(temp[a[k]+1], s[k])
end
View
@@ -189,7 +189,7 @@ procs(s::SubDArray) = s.parent.pmap[_jl_sub_da_pieces(s)]
function dist(s::SubDArray)
pcs = _jl_sub_da_pieces(s)
- sizes = [ length(pieceindex(s, p)) | p = pcs ]
+ sizes = [ length(pieceindex(s, p)) for p = pcs ]
cumsum([1, sizes])
end
@@ -356,7 +356,7 @@ function _jl_distribute_one(T, lsz, da, distdim, owner, orig_array)
i1 = da.dist[p] # my first index in distdim
iend = i1+lsz[distdim]-1 # my last "
# indexes of original array I will take
- idxs = { 1:lsz[i] | i=1:length(da.dims) }
+ idxs = { 1:lsz[i] for i=1:length(da.dims) }
idxs[distdim] = (i1:iend)
remote_call_fetch(owner, ref, orig_array, idxs...)
end
@@ -365,7 +365,7 @@ convert{T,N}(::Type{Array}, d::DArray{T,N}) = convert(Array{T,N}, d)
function convert{S,T,N}(::Type{Array{S,N}}, d::DArray{T,N})
a = Array(S, size(d))
- idxs = { 1:size(a,i) | i=1:N }
+ idxs = { 1:size(a,i) for i=1:N }
for p = 1:length(d.dist)-1
idxs[d.distdim] = d.dist[p]:(d.dist[p+1]-1)
a[idxs...] = remote_call_fetch(d.pmap[p], localize, d)
@@ -878,7 +878,7 @@ end # for
function reduce(f, v::DArray)
mapreduce(f, fetch,
- { @spawnat p reduce(f,localize(v)) | p = procs(v) })
+ { @spawnat p reduce(f,localize(v)) for p = procs(v) })
end
sum(d::DArray) = reduce(+, d)
View
@@ -925,7 +925,7 @@ function typeinf(linfo::LambdaStaticData,atypes::Tuple,sparams::Tuple, def, cop)
rec = false
- s = { () | i=1:n }
+ s = { () for i=1:n }
recpts = IntSet(n+1) # statements that depend recursively on our value
W = IntSet(n+1)
# initial set of pc
@@ -972,7 +972,7 @@ function typeinf(linfo::LambdaStaticData,atypes::Tuple,sparams::Tuple, def, cop)
# exception handlers
cur_hand = ()
- handler_at = { () | i=1:n }
+ handler_at = { () for i=1:n }
while !isempty(W)
pc = choose(W)
@@ -1399,7 +1399,7 @@ function inlineable(f, e::Expr, vars)
return NF
end
# ok, substitute argument expressions for argument names in the body
- spnames = { sp[i].name | i=1:2:length(sp) }
+ spnames = { sp[i].name for i=1:2:length(sp) }
return sym_replace(copy(expr), append(args,spnames),
append(argexprs,spvals))
end
@@ -1487,7 +1487,7 @@ function inlining_pass(e::Expr, vars)
newargs[i-2] = aarg.args[2:]
elseif isa(t,Tuple) && isleaftype(t)
# apply(f,t::(x,y)) => f(t[1],t[2])
- newargs[i-2] = { _jl_mk_tupleref(aarg,j) | j=1:length(t) }
+ newargs[i-2] = { _jl_mk_tupleref(aarg,j) for j=1:length(t) }
else
# not all args expandable
return e
@@ -1547,7 +1547,7 @@ function tuple_elim_pass(ast::Expr)
if nv > 0
del(body, i) # remove (multiple_value)
del(body, i) # remove tuple allocation
- vals = { unique_name(ast) | j=1:nv }
+ vals = { unique_name(ast) for j=1:nv }
# convert tuple allocation to a series of assignments
# to local variables
for j=1:nv
View
@@ -22,7 +22,7 @@ end
zip(itrs...) = Zip(itrs...)
length(z::Zip) = min(length, z.itrs)
-start(z::Zip) = { start(itr) | itr in z.itrs }
+start(z::Zip) = { start(itr) for itr in z.itrs }
function next(z::Zip, state)
v = Array(Any, length(z.itrs))
s = Array(Any, length(z.itrs))
View
@@ -148,18 +148,18 @@ function matmul3x3{T,S}(tA, tB, A::Matrix{T}, B::Matrix{S})
end
-triu{T}(M::Matrix{T}, k::Integer) = [ j-i >= k ? M[i,j] : zero(T) |
- i=1:size(M,1), j=1:size(M,2) ]
-tril{T}(M::Matrix{T}, k::Integer) = [ j-i <= k ? M[i,j] : zero(T) |
- i=1:size(M,1), j=1:size(M,2) ]
+triu{T}(M::Matrix{T}, k::Integer) = [ j-i >= k ? M[i,j] : zero(T) for
+ i=1:size(M,1), j=1:size(M,2) ]
+tril{T}(M::Matrix{T}, k::Integer) = [ j-i <= k ? M[i,j] : zero(T) for
+ i=1:size(M,1), j=1:size(M,2) ]
-diff(a::Vector) = [ a[i+1] - a[i] | i=1:length(a)-1 ]
+diff(a::Vector) = [ a[i+1] - a[i] for i=1:length(a)-1 ]
function diff(a::Matrix, dim::Integer)
if dim == 1
- [ a[i+1,j] - a[i,j] | i=1:size(a,1)-1, j=1:size(a,2) ]
+ [ a[i+1,j] - a[i,j] for i=1:size(a,1)-1, j=1:size(a,2) ]
else
- [ a[i,j+1] - a[i,j] | i=1:size(a,1), j=1:size(a,2)-1 ]
+ [ a[i,j+1] - a[i,j] for i=1:size(a,1), j=1:size(a,2)-1 ]
end
end
@@ -180,7 +180,7 @@ function gradient(F::Vector, h::Vector)
return g
end
-diag(A::Matrix) = [ A[i,i] | i=1:min(size(A,1),size(A,2)) ]
+diag(A::Matrix) = [ A[i,i] for i=1:min(size(A,1),size(A,2)) ]
function diagm{T}(v::Union(Vector{T},Matrix{T}))
if isa(v, Matrix)
@@ -206,7 +206,7 @@ function trace{T}(A::Matrix{T})
return t
end
-kron(a::Vector, b::Vector) = [ a[i]*b[j] | i=1:length(a), j=1:length(b) ]
+kron(a::Vector, b::Vector) = [ a[i]*b[j] for i=1:length(a), j=1:length(b) ]
function kron{T,S}(a::Matrix{T}, b::Matrix{S})
R = Array(promote_type(T,S), size(a,1)*size(b,1), size(a,2)*size(b,2))
@@ -612,7 +612,7 @@ function (\){T<:Union(Float64,Float32,Complex128,Complex64)}(A::StridedMatrix{T}
ipiv = Array(Int32, n)
# Check for SPD matrix
- if ishermitian(Acopy) && all([ Acopy[i,i] > 0 | i=1:n ])
+ if ishermitian(Acopy) && all([ Acopy[i,i] > 0 for i=1:n ])
case = :spd
end
Oops, something went wrong.

1 comment on commit b5283f5

@StefanKarpinski
Member

Awesomeness!

Please sign in to comment.