Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: deprecate cartesianmap #12716

Merged
merged 1 commit into from
Aug 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,8 @@ Deprecated or removed

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

* `cartesianmap` is deprecated in favor of iterating over a `CartesianRange`

Julia v0.3.0 Release Notes
==========================

Expand Down
60 changes: 5 additions & 55 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1118,57 +1118,6 @@ end

## iteration utilities ##

# slow, but useful
function cartesianmap(body, t::Tuple{Vararg{Int}}, it...)
idx = length(t)-length(it)
if idx == 1
for i = 1:t[1]
body(i, it...)
end
elseif idx == 2
for j = 1:t[2]
for i = 1:t[1]
body(i, j, it...)
end
end
elseif idx > 1
for j = 1:t[idx]
for i = 1:t[idx-1]
cartesianmap(body, t, i, j, it...)
end
end
else
throw(ArgumentError("cartesianmap length(t) - length(it) ≤ 0"))
end
end

cartesianmap(body, t::Tuple{}) = (body(); nothing)

function cartesianmap(body, t::Tuple{Int,})
for i = 1:t[1]
body(i)
end
end

function cartesianmap(body, t::Tuple{Int,Int})
for j = 1:t[2]
for i = 1:t[1]
body(i,j)
end
end
end

function cartesianmap(body, t::Tuple{Int,Int,Int})
for k = 1:t[3]
for j = 1:t[2]
for i = 1:t[1]
body(i,j,k)
end
end
end
end

##
# generic map on any iterator
function map(f, iters...)
result = []
Expand Down Expand Up @@ -1242,13 +1191,14 @@ function mapslices(f, A::AbstractArray, dims::AbstractVector)
R[ridx...] = r1

first = true
cartesianmap(itershape) do idxs...
nidx = length(otherdims)
for I in CartesianRange(itershape)
if first
first = false
else
ia = [idxs...]
idx[otherdims] = ia
ridx[otherdims] = ia
for i in 1:nidx
idx[otherdims[i]] = ridx[otherdims[i]] = I.I[i]
end
R[ridx...] = f(reshape(A[idx...], Asliceshape))
end
end
Expand Down
13 changes: 0 additions & 13 deletions base/datafmt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -550,19 +550,6 @@ end

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

#=
function writedlm_ndarray(io::IO, a::AbstractArray, dlm; opts...)
tail = size(a)[3:end]
function print_slice(idxs...)
writedlm(io, sub(a, 1:size(a,1), 1:size(a,2), idxs...), dlm; opts...)
if idxs != tail
print(io, "\n")
end
end
cartesianmap(print_slice, tail)
end
=#

function writedlm(io::IO, itr, dlm; opts...)
optsd = val_opts(opts)
quotes = get(optsd, :quotes, true)
Expand Down
12 changes: 12 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ macro deprecate(old,new)
$(esc(new))(args...)
end))
elseif isa(old,Expr) && old.head == :call
remove_linenums!(new)
oldcall = sprint(io->show_unquoted(io,old))
newcall = sprint(io->show_unquoted(io,new))
oldsym = if isa(old.args[1],Symbol)
Expand All @@ -55,6 +56,15 @@ macro deprecate(old,new)
end
end

remove_linenums!(ex) = ex
function remove_linenums!(ex::Expr)
filter!(x->!((isa(x,Expr) && is(x.head,:line)) || isa(x,LineNumberNode)), ex.args)
for subex in ex.args
remove_linenums!(subex)
end
ex
end

function depwarn(msg, funcsym)
opts = JLOptions()
if opts.depwarn > 0
Expand Down Expand Up @@ -782,3 +792,5 @@ function Regex(pattern::AbstractString, options::Integer)
"use string flags instead: Regex(\"$pattern\", \"$flags\").", :Regex)
Regex(pattern, flags)
end

@deprecate cartesianmap(f, dims) for idx in CartesianRange(dims); f(idx.I...); end
20 changes: 0 additions & 20 deletions base/docs/helpdb.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5560,26 +5560,6 @@ doc"""
"""
svdfact!

doc"""
```rst
::

cartesianmap(f, dims)

Given a ``dims`` tuple of integers ``(m, n, ...)``, call ``f`` on all combinations of
integers in the ranges ``1:m``, ``1:n``, etc.

.. doctest::

julia> cartesianmap(println, (2,2))
11
21
12
22
```
"""
cartesianmap

doc"""
hist2d(M, e1, e2) -> (edge1, edge2, counts)

Expand Down
1 change: 0 additions & 1 deletion base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,6 @@ export
broadcast_function,
broadcast_getindex,
broadcast_setindex!,
cartesianmap,
cat,
cell,
checkbounds,
Expand Down
5 changes: 3 additions & 2 deletions base/iostream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,9 @@ function write{T,N,A<:Array}(s::IOStream, a::SubArray{T,N,A})
if N<=1
return write(s, pointer(a, 1), colsz)
else
cartesianmap((idxs...)->write(s, pointer(a, idxs), colsz),
tuple(1, size(a)[2:end]...))
for idxs in CartesianRange((1, size(a)[2:end]...))
write(s, pointer(a, idxs.I), colsz)
end
return colsz*trailingsize(a,2)
end
end
Expand Down
11 changes: 6 additions & 5 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1132,7 +1132,8 @@ function show_nd(io::IO, a::AbstractArray, limit, print_matrix, label_slices)
end
tail = size(a)[3:end]
nd = ndims(a)-2
function print_slice(idxs...)
for I in CartesianRange(tail)
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll want to change the returns in this function to continue since it's now a for loop.

Copy link
Sponsor Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right you are, thanks.

idxs = I.I
if limit
for i = 1:nd
ii = idxs[i]
Expand All @@ -1141,15 +1142,15 @@ function show_nd(io::IO, a::AbstractArray, limit, print_matrix, label_slices)
for j=i+1:nd
szj = size(a,j+2)
if szj>10 && 3 < idxs[j] <= szj-3
return
@goto skip
end
end
#println(io, idxs)
print(io, "...\n\n")
return
@goto skip
end
if 3 < ii <= size(a,i+2)-3
return
@goto skip
end
end
end
Expand All @@ -1162,8 +1163,8 @@ function show_nd(io::IO, a::AbstractArray, limit, print_matrix, label_slices)
slice = sub(a, 1:size(a,1), 1:size(a,2), idxs...)
print_matrix(io, slice)
print(io, idxs == tail ? "" : "\n\n")
@label skip
end
cartesianmap(print_slice, tail)
end

function whos(m::Module, pattern::Regex)
Expand Down
16 changes: 3 additions & 13 deletions test/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ Base.convert{T,N }(::Type{TSlow }, X::AbstractArray{T,N}) = convert(TSlow{T
Base.convert{T,N,_}(::Type{TSlow{T }}, X::AbstractArray{_,N}) = convert(TSlow{T,N}, X)
Base.convert{T,N }(::Type{TSlow{T,N}}, X::AbstractArray ) = begin
A = TSlow(T, size(X))
cartesianmap((I...)->(A[I...] = X[I...]), size(X))
for I in CartesianRange(size(X))
A[I.I...] = X[I.I...]
end
A
end

Expand Down Expand Up @@ -371,17 +373,6 @@ function test_ind2sub(::Type{TestAbstractArray})
end
end

function test_cartesianmap(::Type{TestAbstractArray})
f(x, y, z, B::Array) = push!(B, (x, y, z))
B = NTuple{3, Int}[]
cartesianmap(f, (3, 3, 3, 1), B)
@test B == NTuple{3, Int}[(1,1,1), (2,1,1), (3,1,1), (1,2,1), (2,2,1), (3,2,1),
(1,3,1), (2,3,1), (3,3,1), (1,1,2), (2,1,2), (3,1,2), (1,2,2), (2,2,2),
(3,2,2), (1,3,2), (2,3,2), (3,3,2), (1,1,3), (2,1,3), (3,1,3), (1,2,3),
(2,2,3), (3,2,3), (1,3,3), (2,3,3), (3,3,3)]
@test_throws ArgumentError cartesianmap(f, (1,), B)
end

type GenericIterator{N} end
Base.start{N}(::GenericIterator{N}) = 1
Base.next{N}(::GenericIterator{N}, i) = (i, i + 1)
Expand Down Expand Up @@ -479,7 +470,6 @@ test_setindex!_internals(TestAbstractArray)
test_get(TestAbstractArray)
test_cat(TestAbstractArray)
test_ind2sub(TestAbstractArray)
test_cartesianmap(TestAbstractArray)
test_map(TestAbstractArray)
test_map_promote(TestAbstractArray)
test_UInt_indexing(TestAbstractArray)
Expand Down