# JuliaLang/julia

replace contains(x,y) with in(y,x). closes #4153

`also add contains(String,String) to test for substrings`
1 parent 3f73066 commit 7ee71738c1d6a3261585f4eaf335b25089470291 JeffBezanson committed Aug 29, 2013
 @@ -157,7 +157,7 @@ vec(a::AbstractVector) = a function squeeze(A::AbstractArray, dims) d = () for i in 1:ndims(A) - if contains(dims,i) + if in(i,dims) if size(A,i) != 1 error("squeezed dims must all be size 1") end @@ -1397,12 +1397,12 @@ function nnz{T}(a::AbstractArray{T}) end # for reductions that expand 0 dims to 1 -reduced_dims(A, region) = ntuple(ndims(A), i->(contains(region, i) ? 1 : +reduced_dims(A, region) = ntuple(ndims(A), i->(in(i, region) ? 1 : size(A,i))) # keep 0 dims in place reduced_dims0(A, region) = ntuple(ndims(A), i->(size(A,i)==0 ? 0 : - contains(region, i) ? 1 : + in(i, region) ? 1 : size(A,i))) reducedim(f::Function, A, region, v0) =
 @@ -1378,7 +1378,7 @@ function findin(a, b) ind = Array(Int, 0) bset = union!(Set(), b) for i = 1:length(a) - if contains(bset, a[i]) + if in(a[i], bset) push!(ind, i) end end @@ -1424,7 +1424,7 @@ function gen_reducedim_func(n, f) rvars = { symbol(string("r",i)) for i=1:n } setlims = { quote # each dim of reduction is either 1:sizeA or ivar:ivar - if contains(region,\$i) + if in(\$i,region) \$(lo[i]) = 1 \$(hi[i]) = size(A,\$i) else @@ -1573,7 +1573,7 @@ function intersect(vs...) for v_elem in vs[1] inall = true for i = 2:length(vs) - if !contains(vs[i], v_elem) + if !in(v_elem, vs[i]) inall=false; break end end @@ -1589,7 +1589,7 @@ function union(vs...) seen = Set() for v in vs for v_elem in v - if !contains(seen, v_elem) + if !in(v_elem, seen) push!(ret, v_elem) push!(seen, v_elem) end @@ -1604,7 +1604,7 @@ function setdiff(a, b) ret = Array(args_type,0) seen = Set() for a_elem in a - if !contains(seen, a_elem) && !contains(bset, a_elem) + if !in(a_elem, seen) && !in(a_elem, bset) push!(ret, a_elem) push!(seen, a_elem) end
 @@ -256,10 +256,10 @@ function process_options(args::Array{Any,1}) global have_color = true elseif args[i][8] == '=' val = args[i][9:] - if contains(("no","0","false"), val) + if in(val, ("no","0","false")) color_set = true global have_color = false - elseif contains(("yes","1","true"), val) + elseif in(val, ("yes","1","true")) color_set = true global have_color = true end
 @@ -86,7 +86,7 @@ const valid_opts = [:has_header, :ignore_invalid_chars, :use_mmap] function val_opts(opts) d = Dict{Symbol,Bool}() for opt in opts - !contains(valid_opts, opt[1]) && error("unknown option \$(opt[1])") + !in(opt[1], valid_opts) && error("unknown option \$(opt[1])") !isa(opt[2], Bool) && error("\$(opt[1]) can only be boolean") d[opt[1]] = opt[2] end @@ -148,7 +148,7 @@ function dlm_offsets(sbuff::UTF8String, dlm, eol, offsets::Array{Int,2}) idx = 1 while(idx <= length(sbuff.data)) val,idx = next(sbuff, idx) - (val != eol) && ((dlm == invalid_dlm) ? !contains(_default_delims, val) : (val != dlm)) && continue + (val != eol) && ((dlm == invalid_dlm) ? !in(val, _default_delims) : (val != dlm)) && continue col += 1 offsets[row,col] = idx-2 (row >= maxrow) && (col == maxcol) && break @@ -164,7 +164,7 @@ function dlm_offsets(dbuff::Vector{Uint8}, dlm::Uint8, eol::Uint8, offsets::Arra offsets[maxrow,maxcol] = length(dbuff) for idx in 1:length(dbuff) val = dbuff[idx] - (val != eol) && ((dlm == invalid_dlm) ? !contains(_default_delims, val) : (val != dlm)) && continue + (val != eol) && ((dlm == invalid_dlm) ? !in(val, _default_delims) : (val != dlm)) && continue col += 1 offsets[row,col] = idx-1 (row >= maxrow) && (col == maxcol) && break @@ -178,7 +178,7 @@ function dlm_dims{T,D}(dbuff::T, eol::D, dlm::D) ncols = nrows = col = 0 try for val in dbuff - (val != eol) && ((dlm == invalid_dlm) ? !contains(_default_delims, val) : (val != dlm)) && continue + (val != eol) && ((dlm == invalid_dlm) ? !in(val, _default_delims) : (val != dlm)) && continue col += 1 (val == eol) && (nrows += 1; ncols = max(ncols, col); col = 0) end
 @@ -4,9 +4,9 @@ abstract Associative{K,V} const secret_table_token = :__c782dbf1cf4d6a2e5e3865d7e95634f2e09b5902__ -haskey(d::Associative, k) = contains(keys(d),k) +haskey(d::Associative, k) = in(k,keys(d)) -function contains(a::Associative, p::(Any,Any)) +function in(p::(Any,Any), a::Associative) v = get(a,p[1],secret_table_token) !is(v, secret_table_token) && isequal(v, p[2]) end @@ -58,8 +58,8 @@ function next(v::ValueIterator, state) n[1][2], n[2] end -contains(v::KeyIterator, k) = !is(get(v.dict, k, secret_table_token), - secret_table_token) +in(k, v::KeyIterator) = !is(get(v.dict, k, secret_table_token), + secret_table_token) keys(a::Associative) = KeyIterator(a) values(a::Associative) = ValueIterator(a) @@ -495,7 +495,7 @@ function get{K,V}(h::Dict{K,V}, key, deflt) end haskey(h::Dict, key) = (ht_keyindex(h, key) >= 0) -contains{T<:Dict}(v::KeyIterator{T}, key) = (ht_keyindex(v.dict, key) >= 0) +in{T<:Dict}(key, v::KeyIterator{T}) = (ht_keyindex(v.dict, key) >= 0) function getkey{K,V}(h::Dict{K,V}, key, deflt) index = ht_keyindex(h, key)
 @@ -72,7 +72,7 @@ const ENV = EnvHash() getindex(::EnvHash, k::String) = @accessEnv k throw(KeyError(k)) get(::EnvHash, k::String, def) = @accessEnv k (return def) -contains(::KeyIterator{EnvHash}, k::String) = _hasenv(k) +in(k::String, ::KeyIterator{EnvHash}) = _hasenv(k) pop!(::EnvHash, k::String) = (v = ENV[k]; _unsetenv(k); v) pop!(::EnvHash, k::String, def) = haskey(ENV,k) ? pop!(ENV,k) : def function delete!(::EnvHash, k::String)
 @@ -693,6 +693,7 @@ export getindex, getkey, haskey, + in, intersect!, intersect, isempty,
 @@ -67,7 +67,7 @@ function init_help() if !haskey(MODULE_DICT, func) MODULE_DICT[func] = {} end - if !contains(MODULE_DICT[func], mod) + if !in(mod, MODULE_DICT[func]) push!(MODULE_DICT[func], mod) end end
 @@ -117,7 +117,7 @@ function copy!(to::IntSet, from::IntSet) union!(to, from) end -function contains(s::IntSet, n::Integer) +function in(n::Integer, s::IntSet) if n >= s.limit # max IntSet length is typemax(Int), so highest possible element is # typemax(Int)-1