Skip to content

Commit 15ad7e9

Browse files
committed
deprecate cartesianmap
1 parent 9b30678 commit 15ad7e9

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
@@ -515,6 +515,8 @@ Deprecated or removed
515515

516516
* `require(::AbstractString)` and `reload` (see news about addition of `compile`)
517517

518+
* `cartesianmap` is deprecated in favor of iterating over a `CartesianRange`
519+
518520
Julia v0.3.0 Release Notes
519521
==========================
520522

base/abstractarray.jl

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

11191119
## iteration utilities ##
11201120

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

12441193
first = true
1245-
cartesianmap(itershape) do idxs...
1194+
nidx = length(otherdims)
1195+
for I in CartesianRange(itershape)
12461196
if first
12471197
first = false
12481198
else
1249-
ia = [idxs...]
1250-
idx[otherdims] = ia
1251-
ridx[otherdims] = ia
1199+
for i in 1:nidx
1200+
idx[otherdims[i]] = ridx[otherdims[i]] = I.I[i]
1201+
end
12521202
R[ridx...] = f(reshape(A[idx...], Asliceshape))
12531203
end
12541204
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
@@ -782,3 +792,5 @@ function Regex(pattern::AbstractString, options::Integer)
782792
"use string flags instead: Regex(\"$pattern\", \"$flags\").", :Regex)
783793
Regex(pattern, flags)
784794
end
795+
796+
@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
@@ -5560,26 +5560,6 @@ doc"""
55605560
"""
55615561
svdfact!
55625562

5563-
doc"""
5564-
```rst
5565-
::
5566-
5567-
cartesianmap(f, dims)
5568-
5569-
Given a ``dims`` tuple of integers ``(m, n, ...)``, call ``f`` on all combinations of
5570-
integers in the ranges ``1:m``, ``1:n``, etc.
5571-
5572-
.. doctest::
5573-
5574-
julia> cartesianmap(println, (2,2))
5575-
11
5576-
21
5577-
12
5578-
22
5579-
```
5580-
"""
5581-
cartesianmap
5582-
55835563
doc"""
55845564
hist2d(M, e1, e2) -> (edge1, edge2, counts)
55855565

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
function whos(m::Module, pattern::Regex)

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

@@ -371,17 +373,6 @@ function test_ind2sub(::Type{TestAbstractArray})
371373
end
372374
end
373375

374-
function test_cartesianmap(::Type{TestAbstractArray})
375-
f(x, y, z, B::Array) = push!(B, (x, y, z))
376-
B = NTuple{3, Int}[]
377-
cartesianmap(f, (3, 3, 3, 1), B)
378-
@test B == NTuple{3, Int}[(1,1,1), (2,1,1), (3,1,1), (1,2,1), (2,2,1), (3,2,1),
379-
(1,3,1), (2,3,1), (3,3,1), (1,1,2), (2,1,2), (3,1,2), (1,2,2), (2,2,2),
380-
(3,2,2), (1,3,2), (2,3,2), (3,3,2), (1,1,3), (2,1,3), (3,1,3), (1,2,3),
381-
(2,2,3), (3,2,3), (1,3,3), (2,3,3), (3,3,3)]
382-
@test_throws ArgumentError cartesianmap(f, (1,), B)
383-
end
384-
385376
type GenericIterator{N} end
386377
Base.start{N}(::GenericIterator{N}) = 1
387378
Base.next{N}(::GenericIterator{N}, i) = (i, i + 1)
@@ -479,7 +470,6 @@ test_setindex!_internals(TestAbstractArray)
479470
test_get(TestAbstractArray)
480471
test_cat(TestAbstractArray)
481472
test_ind2sub(TestAbstractArray)
482-
test_cartesianmap(TestAbstractArray)
483473
test_map(TestAbstractArray)
484474
test_map_promote(TestAbstractArray)
485475
test_UInt_indexing(TestAbstractArray)

0 commit comments

Comments
 (0)