Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add partitionWith from vector #11

Merged
merged 1 commit into from Jan 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,9 @@
# Revision history for nonempty-vector

## Unreleased

* Added `partitionWith` from `vector`. [(#11)](https://github.com/emilypi/nonempty-vector/pull/11) - Thanks @AlistairB!

## 0.2.1.0

* Added `consV` and `snocV` primitives for consing a vector to create a nonempty one. [(#8)](https://github.com/emilypi/nonempty-vector/pull/8) - Thanks @AlistairB!
Expand Down
17 changes: 16 additions & 1 deletion src/Data/Vector/NonEmpty.hs
Expand Up @@ -153,7 +153,7 @@ module Data.Vector.NonEmpty
, takeWhile, dropWhile

-- * Partitioning
, partition, unstablePartition, span, break
, partition, partitionWith, unstablePartition, span, break

-- * Searching
, elem, notElem, find, findIndex, findIndices, elemIndex
Expand Down Expand Up @@ -191,6 +191,7 @@ import Control.Monad (Monad)
import Control.Monad.ST

import qualified Data.Foldable as Foldable
import Data.Either (Either(..))
import Data.Functor
import Data.Int
import Data.List.NonEmpty (NonEmpty(..))
Expand Down Expand Up @@ -2111,6 +2112,20 @@ partition :: (a -> Bool) -> NonEmptyVector a -> (Vector a, Vector a)
partition f = V.partition f . _neVec
{-# INLINE partition #-}

-- | /O(n)/ Split the non-empty vector in two parts, the first one
-- containing the Left elements and the second containing the
-- Right elements. The relative order of the elements is preserved.
--
-- If all elements produce a Left (or Right), one of the
-- resulting vectors may be empty.
--
-- >>> partitionWith (\a -> if a < 3 then Left a else Right (P.show a)) (unsafeFromList [1..5])
-- ([1,2],["3","4","5"])
--
partitionWith :: (a -> Either b c) -> NonEmptyVector a -> (Vector b, Vector c)
partitionWith f = V.partitionWith f . _neVec
{-# INLINE partitionWith #-}

-- | /O(n)/ Split the non-empty vector in two parts, the first one
-- containing those elements that satisfy the predicate and the second
-- one those that don't. The order of the elements is not preserved but
Expand Down