Skip to content

Commit 6cd351f

Browse files
committed
Merge pull request #12716 from JuliaLang/jb/deprecatecartesianmap
RFC: deprecate `cartesianmap`
2 parents c53f8bd + 15ad7e9 commit 6cd351f

File tree

9 files changed

+31
-109
lines changed

9 files changed

+31
-109
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,8 @@ Deprecated or removed
526526

527527
* `require(::AbstractString)` and `reload` (see news about addition of `compile`).
528528

529+
* `cartesianmap` is deprecated in favor of iterating over a `CartesianRange`
530+
529531
Julia v0.3.0 Release Notes
530532
==========================
531533

base/abstractarray.jl

Lines changed: 5 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,57 +1124,6 @@ end
11241124

11251125
## iteration utilities ##
11261126

1127-
# slow, but useful
1128-
function cartesianmap(body, t::Tuple{Vararg{Int}}, it...)
1129-
idx = length(t)-length(it)
1130-
if idx == 1
1131-
for i = 1:t[1]
1132-
body(i, it...)
1133-
end
1134-
elseif idx == 2
1135-
for j = 1:t[2]
1136-
for i = 1:t[1]
1137-
body(i, j, it...)
1138-
end
1139-
end
1140-
elseif idx > 1
1141-
for j = 1:t[idx]
1142-
for i = 1:t[idx-1]
1143-
cartesianmap(body, t, i, j, it...)
1144-
end
1145-
end
1146-
else
1147-
throw(ArgumentError("cartesianmap length(t) - length(it) ≤ 0"))
1148-
end
1149-
end
1150-
1151-
cartesianmap(body, t::Tuple{}) = (body(); nothing)
1152-
1153-
function cartesianmap(body, t::Tuple{Int,})
1154-
for i = 1:t[1]
1155-
body(i)
1156-
end
1157-
end
1158-
1159-
function cartesianmap(body, t::Tuple{Int,Int})
1160-
for j = 1:t[2]
1161-
for i = 1:t[1]
1162-
body(i,j)
1163-
end
1164-
end
1165-
end
1166-
1167-
function cartesianmap(body, t::Tuple{Int,Int,Int})
1168-
for k = 1:t[3]
1169-
for j = 1:t[2]
1170-
for i = 1:t[1]
1171-
body(i,j,k)
1172-
end
1173-
end
1174-
end
1175-
end
1176-
1177-
##
11781127
# generic map on any iterator
11791128
function map(f, iters...)
11801129
result = []
@@ -1248,13 +1197,14 @@ function mapslices(f, A::AbstractArray, dims::AbstractVector)
12481197
R[ridx...] = r1
12491198

12501199
first = true
1251-
cartesianmap(itershape) do idxs...
1200+
nidx = length(otherdims)
1201+
for I in CartesianRange(itershape)
12521202
if first
12531203
first = false
12541204
else
1255-
ia = [idxs...]
1256-
idx[otherdims] = ia
1257-
ridx[otherdims] = ia
1205+
for i in 1:nidx
1206+
idx[otherdims[i]] = ridx[otherdims[i]] = I.I[i]
1207+
end
12581208
R[ridx...] = f(reshape(A[idx...], Asliceshape))
12591209
end
12601210
end

base/datafmt.jl

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -550,19 +550,6 @@ end
550550

551551
writedlm{T}(io::IO, a::AbstractArray{T,0}, dlm; opts...) = writedlm(io, reshape(a,1), dlm; opts...)
552552

553-
#=
554-
function writedlm_ndarray(io::IO, a::AbstractArray, dlm; opts...)
555-
tail = size(a)[3:end]
556-
function print_slice(idxs...)
557-
writedlm(io, sub(a, 1:size(a,1), 1:size(a,2), idxs...), dlm; opts...)
558-
if idxs != tail
559-
print(io, "\n")
560-
end
561-
end
562-
cartesianmap(print_slice, tail)
563-
end
564-
=#
565-
566553
function writedlm(io::IO, itr, dlm; opts...)
567554
optsd = val_opts(opts)
568555
quotes = get(optsd, :quotes, true)

base/deprecated.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ macro deprecate(old,new)
3232
$(esc(new))(args...)
3333
end))
3434
elseif isa(old,Expr) && old.head == :call
35+
remove_linenums!(new)
3536
oldcall = sprint(io->show_unquoted(io,old))
3637
newcall = sprint(io->show_unquoted(io,new))
3738
oldsym = if isa(old.args[1],Symbol)
@@ -55,6 +56,15 @@ macro deprecate(old,new)
5556
end
5657
end
5758

59+
remove_linenums!(ex) = ex
60+
function remove_linenums!(ex::Expr)
61+
filter!(x->!((isa(x,Expr) && is(x.head,:line)) || isa(x,LineNumberNode)), ex.args)
62+
for subex in ex.args
63+
remove_linenums!(subex)
64+
end
65+
ex
66+
end
67+
5868
function depwarn(msg, funcsym)
5969
opts = JLOptions()
6070
if opts.depwarn > 0
@@ -783,3 +793,5 @@ export FloatingPoint
783793
"use string flags instead: Regex(\"$pattern\", \"$flags\").", :Regex)
784794
Regex(pattern, flags)
785795
end
796+
797+
@deprecate cartesianmap(f, dims) for idx in CartesianRange(dims); f(idx.I...); end

base/docs/helpdb.jl

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5574,26 +5574,6 @@ doc"""
55745574
"""
55755575
svdfact!
55765576

5577-
doc"""
5578-
```rst
5579-
::
5580-
5581-
cartesianmap(f, dims)
5582-
5583-
Given a ``dims`` tuple of integers ``(m, n, ...)``, call ``f`` on all combinations of
5584-
integers in the ranges ``1:m``, ``1:n``, etc.
5585-
5586-
.. doctest::
5587-
5588-
julia> cartesianmap(println, (2,2))
5589-
11
5590-
21
5591-
12
5592-
22
5593-
```
5594-
"""
5595-
cartesianmap
5596-
55975577
doc"""
55985578
hist2d(M, e1, e2) -> (edge1, edge2, counts)
55995579

base/exports.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,6 @@ export
499499
broadcast_function,
500500
broadcast_getindex,
501501
broadcast_setindex!,
502-
cartesianmap,
503502
cat,
504503
cell,
505504
checkbounds,

base/iostream.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,9 @@ function write{T,N,A<:Array}(s::IOStream, a::SubArray{T,N,A})
148148
if N<=1
149149
return write(s, pointer(a, 1), colsz)
150150
else
151-
cartesianmap((idxs...)->write(s, pointer(a, idxs), colsz),
152-
tuple(1, size(a)[2:end]...))
151+
for idxs in CartesianRange((1, size(a)[2:end]...))
152+
write(s, pointer(a, idxs.I), colsz)
153+
end
153154
return colsz*trailingsize(a,2)
154155
end
155156
end

base/show.jl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,7 +1132,8 @@ function show_nd(io::IO, a::AbstractArray, limit, print_matrix, label_slices)
11321132
end
11331133
tail = size(a)[3:end]
11341134
nd = ndims(a)-2
1135-
function print_slice(idxs...)
1135+
for I in CartesianRange(tail)
1136+
idxs = I.I
11361137
if limit
11371138
for i = 1:nd
11381139
ii = idxs[i]
@@ -1141,15 +1142,15 @@ function show_nd(io::IO, a::AbstractArray, limit, print_matrix, label_slices)
11411142
for j=i+1:nd
11421143
szj = size(a,j+2)
11431144
if szj>10 && 3 < idxs[j] <= szj-3
1144-
return
1145+
@goto skip
11451146
end
11461147
end
11471148
#println(io, idxs)
11481149
print(io, "...\n\n")
1149-
return
1150+
@goto skip
11501151
end
11511152
if 3 < ii <= size(a,i+2)-3
1152-
return
1153+
@goto skip
11531154
end
11541155
end
11551156
end
@@ -1162,8 +1163,8 @@ function show_nd(io::IO, a::AbstractArray, limit, print_matrix, label_slices)
11621163
slice = sub(a, 1:size(a,1), 1:size(a,2), idxs...)
11631164
print_matrix(io, slice)
11641165
print(io, idxs == tail ? "" : "\n\n")
1166+
@label skip
11651167
end
1166-
cartesianmap(print_slice, tail)
11671168
end
11681169

11691170
# global flag for limiting output

test/abstractarray.jl

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ Base.convert{T,N }(::Type{TSlow }, X::AbstractArray{T,N}) = convert(TSlow{T
4343
Base.convert{T,N,_}(::Type{TSlow{T }}, X::AbstractArray{_,N}) = convert(TSlow{T,N}, X)
4444
Base.convert{T,N }(::Type{TSlow{T,N}}, X::AbstractArray ) = begin
4545
A = TSlow(T, size(X))
46-
cartesianmap((I...)->(A[I...] = X[I...]), size(X))
46+
for I in CartesianRange(size(X))
47+
A[I.I...] = X[I.I...]
48+
end
4749
A
4850
end
4951

@@ -368,17 +370,6 @@ function test_ind2sub(::Type{TestAbstractArray})
368370
end
369371
end
370372

371-
function test_cartesianmap(::Type{TestAbstractArray})
372-
f(x, y, z, B::Array) = push!(B, (x, y, z))
373-
B = NTuple{3, Int}[]
374-
cartesianmap(f, (3, 3, 3, 1), B)
375-
@test B == NTuple{3, Int}[(1,1,1), (2,1,1), (3,1,1), (1,2,1), (2,2,1), (3,2,1),
376-
(1,3,1), (2,3,1), (3,3,1), (1,1,2), (2,1,2), (3,1,2), (1,2,2), (2,2,2),
377-
(3,2,2), (1,3,2), (2,3,2), (3,3,2), (1,1,3), (2,1,3), (3,1,3), (1,2,3),
378-
(2,2,3), (3,2,3), (1,3,3), (2,3,3), (3,3,3)]
379-
@test_throws ArgumentError cartesianmap(f, (1,), B)
380-
end
381-
382373
type GenericIterator{N} end
383374
Base.start{N}(::GenericIterator{N}) = 1
384375
Base.next{N}(::GenericIterator{N}, i) = (i, i + 1)
@@ -476,7 +467,6 @@ test_setindex!_internals(TestAbstractArray)
476467
test_get(TestAbstractArray)
477468
test_cat(TestAbstractArray)
478469
test_ind2sub(TestAbstractArray)
479-
test_cartesianmap(TestAbstractArray)
480470
test_map(TestAbstractArray)
481471
test_map_promote(TestAbstractArray)
482472
test_UInt_indexing(TestAbstractArray)

0 commit comments

Comments
 (0)