Sorry, I couldn’t find where this has been asked before.
Sometimes I need to split a vector into two vectors using some boolean test.
Here’s my implementation:
function Base.split(xs::T, isfirst::Function) where {T <: AbstractVector}
first = T()
second = T()
for x in xs
if isfirst(x)
push!(first, x)
else
push!(second, x)
end
end
first, second
end
It works:
julia> x = [1,2,3,4,5]
5-element Array{Int64,1}:
1
2
3
4
5
julia> odd, even = split(x, isodd)
([1, 3, 5], [2, 4])
Does a better version of this already exist somewhere? In your minds perhaps? One alternative would be to filter!
the original vector and retain the elements that failed the filter somehow, but a) not sure it’s more performant, and b) it would behave differently by changing the input vector.