Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Function to partition a set according to a predicate.
"""
partition(f, xs)
Partitions a collection according to a predicate `f`. Similar to `filter`, but returns
the both the elements for which `f` is `true` and for which it is `false`, in two different
collections.
Returns a tuple of vectors, where the first vector contains the elements for which `f` is true
and the second the elements for which `f` is false.
"""
function partition(f, xs)
T = eltype(xs)
xs_true, xs_false = T[], T[]
for x in xs
f(x) ? push!(xs_true, x) : push!(xs_false, x)
end
return xs_true, xs_false
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.