Permalink
Browse files

bang: consistently use ! for mutating functions [#907].

This deprecates the non-! versions and prints a warning when they
are used. It doesn't quite close out #907 but comes close. There
are still a few lingering mutators like del_each that we may want
to get rid of or rename somehow.
  • Loading branch information...
1 parent 20d6b6f commit d34d330fc7e3323e0b2d1e2d69f3bc291031d61f @StefanKarpinski StefanKarpinski committed Jan 8, 2013
View
@@ -613,7 +613,7 @@ assign{T<:Real}(A::Array, x, I::AbstractVector{Bool}, J::AbstractVector{T}) = as
## Dequeue functionality ##
-function push{T}(a::Array{T,1}, item)
+function push!{T}(a::Array{T,1}, item)
if is(T,None)
error("[] cannot grow. Instead, initialize the array with \"T[]\".")
end
@@ -624,7 +624,7 @@ function push{T}(a::Array{T,1}, item)
return a
end
-function push(a::Array{Any,1}, item::ANY)
+function push!(a::Array{Any,1}, item::ANY)
ccall(:jl_array_grow_end, Void, (Any, Uint), a, 1)
arrayset(a, item, length(a))
return a
@@ -640,7 +640,7 @@ function append!{T}(a::Array{T,1}, items::Array{T,1})
return a
end
-function grow(a::Vector, n::Integer)
+function grow!(a::Vector, n::Integer)
if n > 0
ccall(:jl_array_grow_end, Void, (Any, Uint), a, n)
else
@@ -652,16 +652,16 @@ function grow(a::Vector, n::Integer)
return a
end
-function pop(a::Vector)
+function pop!(a::Vector)
if isempty(a)
- error("pop: array is empty")
+ error("pop!: array is empty")
end
item = a[end]
ccall(:jl_array_del_end, Void, (Any, Uint), a, 1)
return item
end
-function enqueue{T}(a::Array{T,1}, item)
+function unshift!{T}(a::Array{T,1}, item)
if is(T,None)
error("[] cannot grow. Instead, initialize the array with \"T[]\".")
end
@@ -670,18 +670,17 @@ function enqueue{T}(a::Array{T,1}, item)
a[1] = item
return a
end
-const unshift = enqueue
-function shift(a::Vector)
+function shift!(a::Vector)
if isempty(a)
- error("shift: array is empty")
+ error("shift!: array is empty")
end
item = a[1]
ccall(:jl_array_del_beg, Void, (Any, Uint), a, 1)
return item
end
-function insert{T}(a::Array{T,1}, i::Integer, item)
+function insert!{T}(a::Array{T,1}, i::Integer, item)
if i < 1
throw(BoundsError())
end
@@ -703,7 +702,7 @@ function insert{T}(a::Array{T,1}, i::Integer, item)
a[i] = item
end
-function del(a::Vector, i::Integer)
+function delete!(a::Vector, i::Integer)
n = length(a)
if !(1 <= i <= n)
throw(BoundsError())
@@ -722,7 +721,7 @@ function del(a::Vector, i::Integer)
return a
end
-function del{T<:Integer}(a::Vector, r::Range1{T})
+function delete!{T<:Integer}(a::Vector, r::Range1{T})
n = length(a)
f = first(r)
l = last(r)
@@ -747,7 +746,7 @@ function del{T<:Integer}(a::Vector, r::Range1{T})
return a
end
-function del_all(a::Vector)
+function empty!(a::Vector)
ccall(:jl_array_del_end, Void, (Any, Uint), a, length(a))
return a
end
@@ -1136,7 +1135,7 @@ function find(testf::Function, A::StridedArray)
tmpI = Array(Int, 0)
for i = 1:length(A)
if testf(A[i])
- push(tmpI, i)
+ push!(tmpI, i)
end
end
I = Array(Int, length(tmpI))
@@ -1635,7 +1634,7 @@ function permute(A::StridedArray, perm)
P = similar(A, dimsP)
ranges = ntuple(ndimsA, i->(1:dimsP[i]))
while length(stridenames) < ndimsA
- push(stridenames, gensym())
+ push!(stridenames, gensym())
end
#calculates all the strides
View
@@ -657,7 +657,7 @@ assign{T<:Real}(A::BitMatrix, x, I::AbstractVector{Bool}, J::AbstractVector{T})
## Dequeue functionality ##
-function push(B::BitVector, item)
+function push!(B::BitVector, item)
# convert first so we don't grow the bitarray if the assignment won't work
item = convert(Bool, item)
@@ -693,13 +693,13 @@ end
append!(B::BitVector, items::AbstractVector{Bool}) = append!(B, bitpack(items))
append!(A::Vector{Bool}, items::BitVector) = append!(A, bitunpack(items))
-function grow(B::BitVector, n::Integer)
+function grow!(B::BitVector, n::Integer)
n0 = length(B)
if n < -n0
throw(BoundsError())
end
if n < 0
- return del(B, n0+n+1:n0)
+ return delete!(B, n0+n+1:n0)
end
k0 = length(B.chunks)
k1 = num_bit_chunks(n0 + int(n))
@@ -711,9 +711,9 @@ function grow(B::BitVector, n::Integer)
return B
end
-function pop(B::BitVector)
+function pop!(B::BitVector)
if isempty(B)
- error("pop: BitArray is empty")
+ error("pop!: BitArray is empty")
end
item = B[end]
B[end] = false
@@ -727,7 +727,7 @@ function pop(B::BitVector)
return item
end
-function enqueue(B::BitVector, item)
+function unshift!(B::BitVector, item)
item = convert(Bool, item)
l = @_mod64 length(B)
@@ -747,9 +747,9 @@ function enqueue(B::BitVector, item)
return B
end
-function shift(B::BitVector)
+function shift!(B::BitVector)
if isempty(B)
- error("shift: BitArray is empty")
+ error("shift!: BitArray is empty")
end
item = B[1]
@@ -768,7 +768,7 @@ function shift(B::BitVector)
return item
end
-function insert(B::BitVector, i::Integer, item)
+function insert!(B::BitVector, i::Integer, item)
if i < 1
throw(BoundsError())
end
@@ -799,7 +799,7 @@ function insert(B::BitVector, i::Integer, item)
B[i] = item
end
-function del(B::BitVector, i::Integer)
+function delete!(B::BitVector, i::Integer)
n = length(B)
if !(1 <= i <= n)
throw(BoundsError())
@@ -833,7 +833,7 @@ function del(B::BitVector, i::Integer)
return B
end
-function del(B::BitVector, r::Range1{Int})
+function delete!(B::BitVector, r::Range1{Int})
n = length(B)
i_f = first(r)
i_l = last(r)
@@ -863,7 +863,7 @@ function del(B::BitVector, r::Range1{Int})
return B
end
-function del_all(B::BitVector)
+function empty!(B::BitVector)
ccall(:jl_array_del_end, Void, (Any, Uint), B.chunks, length(B.chunks))
B.dims[1] = 0
return B
@@ -1585,7 +1585,7 @@ function permute(B::BitArray, perm)
P = similar(B, dimsP)
ranges = ntuple(ndimsB, i->(colon(1,dimsP[i])))
while length(stridenames) < ndimsB
- push(stridenames, gensym())
+ push!(stridenames, gensym())
end
#calculates all the strides
View
@@ -1,6 +1,8 @@
## client.jl - frontend handling command line options, environment setup,
## and REPL
+have_color = false # default can be altered
+
const color_normal = "\033[0m"
text_colors = {:black => "\033[1m\033[30m",
@@ -282,8 +284,8 @@ function _start()
global PGRP;
PGRP.myid = 1
assert(PGRP.np == 0)
- push(PGRP.workers,LocalProcess())
- push(PGRP.locs,("",0))
+ push!(PGRP.workers,LocalProcess())
+ push!(PGRP.locs,("",0))
PGRP.np = 1
# make scheduler aware of current (root) task
enq_work(roottask_wi)
@@ -325,7 +327,7 @@ end
const atexit_hooks = {}
-atexit(f::Function) = (enqueue(atexit_hooks, f); nothing)
+atexit(f::Function) = (unshift!(atexit_hooks, f); nothing)
function _atexit()
for f in atexit_hooks
View
@@ -259,12 +259,12 @@ function partitions{T}(s::AbstractVector{T})
# convert from restricted growth string a[1:n] to set of sets
temp = [ Array(T,0) for k = 1:n ]
for k = 1:n
- push(temp[a[k]+1], s[k])
+ push!(temp[a[k]+1], s[k])
end
result = Array(Array{T,1},0)
for arr in temp
if !isempty(arr)
- push(result, arr)
+ push!(result, arr)
end
end
#produce(a[1:n]) # this is the string representing set assignment
View
@@ -221,9 +221,9 @@ function locate(d::DArray, I::Range1{Int})
if i >= d.dist[j+1]
j += 1
else
- push(pmap,j)
+ push!(pmap,j)
i = min(imax+1,d.dist[j+1])
- push(dist,i)
+ push!(dist,i)
j += 1
end
end
@@ -246,9 +246,9 @@ function locate(d::DArray, I::AbstractVector{Int})
if i >= d.dist[j+1]
j += 1
else
- push(pmap,j)
+ push!(pmap,j)
i = min(imax+1,d.dist[j+1])
- push(dist,i)
+ push!(dist,i)
j += 1
end
end
View
@@ -0,0 +1,20 @@
+macro deprecate(oldf,newf)
+ oldname = expr(:quote,oldf)
+ newname = expr(:quote,newf)
+ expr(:toplevel,
+ expr(:export,esc(oldf)),
+ :(function $(esc(oldf))(args...)
+ warn(strcat($oldname," is deprecated, use ",$newname," instead."))
+ $(esc(newf))(args...)
+ end))
+end
+
+@deprecate push push!
+@deprecate pop pop!
+@deprecate grow grow!
+@deprecate enqueue unshift!
+@deprecate unshift unshift!
+@deprecate shift shift!
+@deprecate insert insert!
+@deprecate del delete!
+@deprecate del_all empty!
View
@@ -80,7 +80,7 @@ merge(d::Associative, others::Associative...) = merge!(copy(d), others...)
function filter!(f::Function, d::Associative)
for (k,v) in d
if !f(k,v)
- del(d,k)
+ delete!(d,k)
end
end
return d
@@ -131,10 +131,10 @@ end
get(t::ObjectIdDict, key::ANY, default::ANY) =
ccall(:jl_eqtable_get, Any, (Any, Any, Any), t.ht, key, default)
-del(t::ObjectIdDict, key::ANY) =
+delete!(t::ObjectIdDict, key::ANY) =
(ccall(:jl_eqtable_del, Int32, (Any, Any), t.ht, key); t)
-del_all(t::ObjectIdDict) = (t.ht = cell(length(t.ht)); t)
+empty!(t::ObjectIdDict) = (t.ht = cell(length(t.ht)); t)
start(t::ObjectIdDict) = 0
done(t::ObjectIdDict, i) = is(next(t,i),())
@@ -329,7 +329,7 @@ function resize(d::Dict, newsz)
rehash(d, newsz)
end
-function del_all{K,V}(h::Dict{K,V})
+function empty!{K,V}(h::Dict{K,V})
fill!(h.slots, 0x0)
sz = length(h.slots)
h.keys = Array(K, sz)
@@ -444,7 +444,7 @@ function key{K,V}(h::Dict{K,V}, key, deflt)
return (index<0) ? deflt : h.keys[index]::K
end
-function del(h::Dict, key)
+function delete!(h::Dict, key)
index = ht_keyindex(h, key)
if index > 0
h.slots[index] = 0x2
@@ -490,7 +490,7 @@ end
function add_weak_key(t::Dict, k, v)
if is(t.deleter, identity)
- t.deleter = x->del(t, x)
+ t.deleter = x->delete!(t, x)
end
t[WeakRef(k)] = v
# TODO: it might be better to avoid the finalizer, allow
@@ -502,7 +502,7 @@ end
function add_weak_value(t::Dict, k, v)
t[k] = WeakRef(v)
- finalizer(v, x->del(t, k))
+ finalizer(v, x->delete!(t, k))
return t
end
@@ -524,8 +524,8 @@ function key{K}(wkh::WeakKeyDict{K}, kk, deflt)
end
get{K}(wkh::WeakKeyDict{K}, key, deflt) = get(wkh.ht, key, deflt)
-del{K}(wkh::WeakKeyDict{K}, key) = del(wkh.ht, key)
-del_all(wkh::WeakKeyDict) = (del_all(wkh.ht); wkh)
+delete!{K}(wkh::WeakKeyDict{K}, key) = delete!(wkh.ht, key)
+empty!(wkh::WeakKeyDict) = (empty!(wkh.ht); wkh)
has{K}(wkh::WeakKeyDict{K}, key) = has(wkh.ht, key)
ref{K}(wkh::WeakKeyDict{K}, key) = ref(wkh.ht, key)
isempty(wkh::WeakKeyDict) = isempty(wkh.ht)
View
@@ -75,7 +75,7 @@ const ENV = EnvHash()
ref(::EnvHash, k::String) = @accessEnv k throw(KeyError(k))
get(::EnvHash, k::String, deflt) = @accessEnv k (return deflt)
has(::EnvHash, k::String) = hasenv(k)
-del(::EnvHash, k::String) = unsetenv(k)
+delete!(::EnvHash, k::String) = unsetenv(k)
assign(::EnvHash, v, k::String) = setenv(k,string(v))
@unix_only begin
Oops, something went wrong.

0 comments on commit d34d330

Please sign in to comment.