# Split vector into two using a boolean test

**URL:** <https://discourse.julialang.org/t/split-vector-into-two-using-a-boolean-test/15600>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 28, 2018, 9:34am UTC](https://discourse.julialang.org/t/split-vector-into-two-using-a-boolean-test/15600 "2018-09-28T09:34:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [September 28, 2018, 9:34am UTC](https://discourse.julialang.org/t/split-vector-into-two-using-a-boolean-test/15600/1 "2018-09-28T09:34:33Z")

</div>

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:

```julia
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
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.

---

<div class="post-metadata">

**Author:** ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)\
**Post date:** [September 28, 2018, 1:02pm UTC](https://discourse.julialang.org/t/split-vector-into-two-using-a-boolean-test/15600/2 "2018-09-28T13:02:52Z")

</div>

Maybe a `filterperm!()` along the lines of `x[fp() = isodd.(x)]`
