Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c784d89

Browse files
committedJan 8, 2014
deprecate concatenating [a,b,...] and T[a,b,...]
closes #2488, closes #3737
1 parent 8a8a382 commit c784d89

File tree

5 files changed

+101
-60
lines changed

5 files changed

+101
-60
lines changed
 

‎base/abstractarray.jl

+55-8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,45 @@ typealias AbstractVecOrMat{T} Union(AbstractVector{T}, AbstractMatrix{T})
66

77
## Basic functions ##
88

9+
vect() = Array(None, 0)
10+
vect{T}(X::T...) = T[ X[i] for i=1:length(X) ]
11+
12+
const _oldstyle_array_vcat_ = true
13+
14+
if _oldstyle_array_vcat_
15+
function oldstyle_vcat_warning(n::Int)
16+
if n == 1
17+
before = "a"
18+
after = "a;"
19+
elseif n == 2
20+
before = "a,b"
21+
after = "a;b"
22+
else
23+
before = "a,b,..."
24+
after = "a;b;..."
25+
end
26+
warn_once("[$before] concatenation is deprecated; use [$after] instead")
27+
end
28+
function vect(A::AbstractArray...)
29+
oldstyle_vcat_warning(length(A))
30+
vcat(A...)
31+
end
32+
function vect(X...)
33+
for a in X
34+
if typeof(a) <: AbstractArray
35+
oldstyle_vcat_warning(length(X))
36+
break
37+
end
38+
end
39+
vcat(X...)
40+
end
41+
else
42+
function vect(X...)
43+
T = promote_type(map(typeof, X)...)
44+
T[ X[i] for i=1:length(X) ]
45+
end
46+
end
47+
948
size{T,n}(t::AbstractArray{T,n}, d) = (d>n ? 1 : size(t)[d])
1049
eltype(x) = Any
1150
eltype{T,n}(::AbstractArray{T,n}) = T
@@ -791,6 +830,11 @@ end
791830
## cat: general case
792831

793832
function cat(catdim::Integer, X...)
833+
T = promote_type(map(x->isa(x,AbstractArray) ? eltype(x) : typeof(x), X)...)
834+
cat_t(catdim, T, X...)
835+
end
836+
837+
function cat_t(catdim::Integer, typeC::Type, X...)
794838
nargs = length(X)
795839
dimsX = map((a->isa(a,AbstractArray) ? size(a) : (1,)), X)
796840
ndimsX = map((a->isa(a,AbstractArray) ? ndims(a) : 1), X)
@@ -834,7 +878,6 @@ function cat(catdim::Integer, X...)
834878

835879
ndimsC = max(catdim, d_max)
836880
dimsC = ntuple(ndimsC, compute_dims)::(Int...)
837-
typeC = promote_type(map(x->isa(x,AbstractArray) ? eltype(x) : typeof(x), X)...)
838881
C = similar(isa(X[1],AbstractArray) ? full(X[1]) : [X[1]], typeC, dimsC)
839882

840883
range = 1
@@ -850,12 +893,15 @@ end
850893
vcat(X...) = cat(1, X...)
851894
hcat(X...) = cat(2, X...)
852895

896+
typed_vcat(T::Type, X...) = cat_t(1, T, X...)
897+
typed_hcat(T::Type, X...) = cat_t(2, T, X...)
898+
853899
cat{T}(catdim::Integer, A::AbstractArray{T}...) = cat_t(catdim, T, A...)
854900

855901
cat(catdim::Integer, A::AbstractArray...) =
856902
cat_t(catdim, promote_type(map(eltype, A)...), A...)
857903

858-
function cat_t(catdim::Integer, typeC, A::AbstractArray...)
904+
function cat_t(catdim::Integer, typeC::Type, A::AbstractArray...)
859905
# ndims of all input arrays should be in [d-1, d]
860906

861907
nargs = length(A)
@@ -916,6 +962,9 @@ end
916962
vcat(A::AbstractArray...) = cat(1, A...)
917963
hcat(A::AbstractArray...) = cat(2, A...)
918964

965+
typed_vcat(T::Type, A::AbstractArray...) = cat_t(1, T, A...)
966+
typed_hcat(T::Type, A::AbstractArray...) = cat_t(2, T, A...)
967+
919968
# 2d horizontal and vertical concatenation
920969

921970
function hvcat(nbc::Integer, as...)
@@ -1003,22 +1052,20 @@ function hvcat_fill(a, xs)
10031052
a
10041053
end
10051054

1006-
function hvcat(rows::(Int...), xs::Number...)
1055+
function typed_hvcat(T::Type, rows::(Int...), xs...)
10071056
nr = length(rows)
10081057
nc = rows[1]
1009-
#error check
10101058
for i = 2:nr
10111059
if nc != rows[i]
10121060
error("row ", i, " has mismatched number of columns")
10131061
end
10141062
end
1015-
T = typeof(xs[1])
1016-
for i=2:length(xs)
1017-
T = promote_type(T,typeof(xs[i]))
1018-
end
10191063
hvcat_fill(Array(T, nr, nc), xs)
10201064
end
10211065

1066+
hvcat(rows::(Int...), xs::Number...) =
1067+
typed_hvcat(promote_type(map(typeof, xs)...), rows, xs...)
1068+
10221069
## Reductions and scans ##
10231070

10241071
function isequal(A::AbstractArray, B::AbstractArray)

‎base/array.jl

+4
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,15 @@ end
157157

158158
getindex(T::(Type...)) = Array(T,0)
159159

160+
if _oldstyle_array_vcat_
160161
# T[a:b] and T[a:s:b] also contruct typed ranges
161162
function getindex{T<:Number}(::Type{T}, r::Ranges)
163+
warn_once("T[a:b] concatenation is deprecated; use T[a:b;] instead")
162164
copy!(Array(T,length(r)), r)
163165
end
164166

165167
function getindex{T<:Number}(::Type{T}, r1::Ranges, rs::Ranges...)
168+
warn_once("T[a:b,...] concatenation is deprecated; use T[a:b;...] instead")
166169
a = Array(T,length(r1)+sum(length,rs))
167170
o = 1
168171
copy!(a, o, r1)
@@ -173,6 +176,7 @@ function getindex{T<:Number}(::Type{T}, r1::Ranges, rs::Ranges...)
173176
end
174177
return a
175178
end
179+
end
176180

177181
function fill!{T<:Union(Int8,Uint8)}(a::Array{T}, x::Integer)
178182
ccall(:memset, Ptr{Void}, (Ptr{Void}, Int32, Csize_t), a, x, length(a))

‎src/julia-parser.scm

+11-15
Original file line numberDiff line numberDiff line change
@@ -845,14 +845,10 @@
845845
(loop (list 'typed_dict ex))
846846
(loop (list 'ref ex)))
847847
(case (car al)
848+
((vect) (loop (list* 'ref ex (cdr al))))
848849
((dict) (loop (list* 'typed_dict ex (cdr al))))
849850
((hcat) (loop (list* 'typed_hcat ex (cdr al))))
850-
((vcat)
851-
(if (any (lambda (x)
852-
(and (pair? x) (eq? (car x) 'row)))
853-
(cdr al))
854-
(loop (list* 'typed_vcat ex (cdr al)))
855-
(loop (list* 'ref ex (cdr al)))))
851+
((vcat) (loop (list* 'typed_vcat ex (cdr al))))
856852
((comprehension)
857853
(loop (list* 'typed_comprehension ex (cdr al))))
858854
((dict_comprehension)
@@ -1289,21 +1285,20 @@
12891285
(error (string "missing comma or " closer
12901286
" in argument list"))))))))))
12911287

1292-
; parse [] concatenation expressions and {} cell expressions
1293-
(define (parse-vcat s first closer)
1288+
(define (parse-vect s first closer)
12941289
(let loop ((lst '())
12951290
(nxt first))
12961291
(let ((t (require-token s)))
12971292
(if (eqv? t closer)
12981293
(begin (take-token s)
1299-
(cons 'vcat (reverse (cons nxt lst))))
1294+
(cons 'vect (reverse (cons nxt lst))))
13001295
(case t
13011296
((#\,)
13021297
(take-token s)
13031298
(if (eqv? (require-token s) closer)
13041299
;; allow ending with ,
13051300
(begin (take-token s)
1306-
(cons 'vcat (reverse (cons nxt lst))))
1301+
(cons 'vect (reverse (cons nxt lst))))
13071302
(loop (cons nxt lst) (parse-eq* s))))
13081303
((#\;)
13091304
(error "unexpected semicolon in array expression"))
@@ -1313,7 +1308,7 @@
13131308
(error "missing separator in array expression")))))))
13141309

13151310
(define (parse-dict s first closer)
1316-
(let ((v (parse-vcat s first closer)))
1311+
(let ((v (parse-vect s first closer)))
13171312
(if (any dict-literal? (cdr v))
13181313
(if (every dict-literal? (cdr v))
13191314
`(dict ,@(cdr v))
@@ -1349,7 +1344,7 @@
13491344
(if (pair? outer)
13501345
(fix 'vcat (update-outer vec outer))
13511346
(if (or (null? vec) (null? (cdr vec)))
1352-
(fix 'vcat vec) ; [x] => (vcat x)
1347+
(fix 'vect vec) ; [x] => (vect x)
13531348
(fix 'hcat vec)))) ; [x y] => (hcat x y)
13541349
(case t
13551350
((#\; #\newline)
@@ -1390,8 +1385,8 @@
13901385
(else
13911386
(parse-dict s first closer)))
13921387
(case (peek-token s)
1393-
((#\,)
1394-
(parse-vcat s first closer))
1388+
((#\, closer)
1389+
(parse-vect s first closer))
13951390
((for)
13961391
(take-token s)
13971392
(parse-comprehension s first closer))
@@ -1663,6 +1658,7 @@
16631658
(if (null? vex)
16641659
'(cell1d)
16651660
(case (car vex)
1661+
((vect) `(cell1d ,@(cdr vex)))
16661662
((comprehension)
16671663
`(typed_comprehension (top Any) ,@(cdr vex)))
16681664
((dict_comprehension)
@@ -1698,7 +1694,7 @@
16981694
((eqv? t #\[ )
16991695
(take-token s)
17001696
(let ((vex (parse-cat s #\])))
1701-
(if (null? vex) '(vcat) vex)))
1697+
(if (null? vex) '(vect) vex)))
17021698

17031699
;; string literal
17041700
((eqv? t #\")

‎src/julia-syntax.scm

+19-37
Original file line numberDiff line numberDiff line change
@@ -1796,6 +1796,9 @@
17961796
(error "invalid \":\" outside indexing"))
17971797
`(call colon ,.(map expand-forms (cdr e))))
17981798

1799+
'vect
1800+
(lambda (e) (expand-forms `(call (top vect) ,@(cdr e))))
1801+
17991802
'hcat
18001803
(lambda (e) `(call hcat ,.(map expand-forms (cdr e))))
18011804

@@ -1818,47 +1821,26 @@
18181821
`(call vcat ,@a)))))
18191822

18201823
'typed_hcat
1821-
(lambda (e)
1822-
(let ((t (cadr e))
1823-
(a (cddr e)))
1824-
(let ((result (gensy))
1825-
(ncols (length a)))
1826-
`(block
1827-
#;(if (call (top !) (call (top isa) ,t Type))
1828-
(call (top error) "invalid array index"))
1829-
(= ,result (call (top Array) ,(expand-forms t) 1 ,ncols))
1830-
,.(map (lambda (x i) `(call (top setindex!) ,result
1831-
,(expand-forms x) ,i))
1832-
a (cdr (iota (+ ncols 1))))
1833-
,result))))
1824+
(lambda (e) `(call (top typed_hcat) ,(expand-forms (cadr e)) ,.(map expand-forms (cddr e))))
18341825

18351826
'typed_vcat
18361827
(lambda (e)
18371828
(let ((t (cadr e))
1838-
(rows (cddr e)))
1839-
(if (any (lambda (x) (not (and (pair? x) (eq? 'row (car x))))) rows)
1840-
(error "invalid array literal")
1841-
(let ((result (gensy))
1842-
(nrows (length rows))
1843-
(ncols (length (cdar rows))))
1844-
(if (any (lambda (x) (not (= (length (cdr x)) ncols))) rows)
1845-
(error "invalid array literal")
1846-
`(block
1847-
#;(if (call (top !) (call (top isa) ,t Type))
1848-
(call (top error) "invalid array index"))
1849-
(= ,result (call (top Array) ,(expand-forms t) ,nrows ,ncols))
1850-
,.(apply nconc
1851-
(map
1852-
(lambda (row i)
1853-
(map
1854-
(lambda (x j)
1855-
`(call (top setindex!) ,result
1856-
,(expand-forms x) ,i ,j))
1857-
(cdr row)
1858-
(cdr (iota (+ ncols 1)))))
1859-
rows
1860-
(cdr (iota (+ nrows 1)))))
1861-
,result))))))
1829+
(a (cddr e)))
1830+
(expand-forms
1831+
(if (any (lambda (x)
1832+
(and (pair? x) (eq? (car x) 'row)))
1833+
a)
1834+
;; convert nested hcat inside vcat to hvcat
1835+
(let ((rows (map (lambda (x)
1836+
(if (and (pair? x) (eq? (car x) 'row))
1837+
(cdr x)
1838+
(list x)))
1839+
a)))
1840+
`(call (top typed_hvcat) ,t
1841+
(tuple ,.(map length rows))
1842+
,.(apply nconc rows)))
1843+
`(call (top typed_vcat) ,t ,@a)))))
18621844

18631845
'|'| (lambda (e) `(call ctranspose ,(expand-forms (cadr e))))
18641846
'|.'| (lambda (e) `(call transpose ,(expand-forms (cadr e))))

‎test/arrayops.jl

+12
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,18 @@ Y = [1. 2. 3.; 4. 5. 6.]
236236
@test size(X) == size(Y)
237237
for i = 1:length(X) @test X[i] === Y[i] end
238238

239+
_array_equiv(a,b) = eltype(a) == eltype(b) && a == b
240+
@test _array_equiv(Uint8[1:3;4], [0x1,0x2,0x3,0x4])
241+
if !Base._oldstyle_array_vcat_
242+
@test_throws Uint8[1:3]
243+
@test_throws Uint8[1:3,]
244+
@test_throws Uint8[1:3,4:6]
245+
a = Array(Range1{Int},1); a[1] = 1:3
246+
@test _array_equiv([1:3,], a)
247+
a = Array(Range1{Int},2); a[1] = 1:3; a[2] = 4:6
248+
@test _array_equiv([1:3,4:6], a)
249+
end
250+
239251
# "end"
240252
X = [ i+2j for i=1:5, j=1:5 ]
241253
@test X[end,end] == 15

0 commit comments

Comments
 (0)
Please sign in to comment.