Yep, it may be redundant here. I’m use to parameterizing functions when in structs to enforce specialization.
In any case, I think the splice!
version is very clean conceptually. So given, your needs, I would opt for it (perhaps using a different name, so as to not intrude on Base module).
It would be a nice addition to Base.
Here is another version which uses filter!
and may be faster for some implementations of AbstractDict:
function Base.splice!(d::AbstractDict, p)
r = [k=>v for (k,v) in d if p(k)]
filter!(!in(r), d)
r
end
or
function Base.splice!(d::AbstractDict, p)
r = Dict(k=>v for (k,v) in d if p(k))
filter!(p->!haskey(r,first(p)), d)
r
end
Another perhaps useful utility function for Dicts:
function groupby(d::AbstractDict, p)
T = promote_type(Base.return_types(p,(keytype(d),))...)
D = Dict{T,Dict{keytype(d), valtype(d)}}()
for (k,v) in d
dd = get!(()->(typeof(d)()), D, p(k))
dd[k] = v
end
D
end
with it:
julia> D = groupby(mydict, isodd)
Dict{Bool, Dict{Int64, Int64}} with 2 entries:
0 => Dict(4=>12, 6=>25, 2=>45, 10=>21, 8=>1)
1 => Dict(5=>45, 7=>93, 9=>18, 3=>5, 1=>28)
julia> filter!(in(D[false]), mydict)
Dict{Int64, Int64} with 5 entries:
4 => 12
6 => 25
2 => 45
10 => 21
8 => 1
julia> foreach(D[true]) do (k,v)
println("$k\t$v")
end
5 45
7 93
9 18
3 5
1 28