I want something like
splitBy pred list = ( filter pred list, filter (not . pred) list )
but in one pass.
You are looking for the partition
function from Data.List
:
partition :: (a -> Bool) -> [a] -> ([a], [a])
It can be implemented nicely using a fold:
splitBy pred = foldr f ([], [])
where f x ~(yes, no) = if pred x then (x : yes, no)
else (yes, x : no)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With