summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorphadej <>2019-07-26 08:19:00 (GMT)
committerhdiff <hdiff@hdiff.luite.com>2019-07-26 08:19:00 (GMT)
commit19e78dbbf4d8c1995a2b8989beb15652e55a28fa (patch)
treeff16938063a2b5e01015f9da67dae05edad1141b
parentd47122eef153bdcf66a8243b395285faa70c9528 (diff)
version 1.0.1HEAD1.0.1master
-rwxr-xr-xCHANGELOG.md4
-rw-r--r--src/Data/These.hs29
-rw-r--r--these.cabal2
3 files changed, 34 insertions, 1 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e214df6..26c2294 100755
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,7 @@
+# 1.0.1
+
+- add `partitionEithersNE :: NonEmpty (Either a b) -> These (NonEmpty a) (NonEmpty b)`
+
# 1
This is major package reogranisation. Old `these` were split into
diff --git a/src/Data/These.hs b/src/Data/These.hs
index c5c7ee8..75dadd0 100644
--- a/src/Data/These.hs
+++ b/src/Data/These.hs
@@ -16,6 +16,7 @@ module Data.These (
-- * Partition
, partitionThese
, partitionHereThere
+ , partitionEithersNE
-- * Distributivity
--
@@ -35,7 +36,9 @@ import Data.Bifunctor (Bifunctor (..))
import Data.Binary (Binary (..))
import Data.Bitraversable (Bitraversable (..))
import Data.Data (Data, Typeable)
+import Data.Either (partitionEithers)
import Data.Hashable (Hashable (..))
+import Data.List.NonEmpty (NonEmpty (..))
import Data.Semigroup (Semigroup (..))
import GHC.Generics (Generic)
@@ -143,6 +146,32 @@ partitionHereThere (t:ts) = case t of
where
~(xs,ys) = partitionHereThere ts
+-- | Like 'partitionEithers' but for 'NonEmpty' types.
+--
+-- * either all are 'Left'
+-- * either all are 'Right'
+-- * or there is both 'Left' and 'Right' stuff
+--
+-- /Note:/ this is not online algorithm. In the worst case it will traverse
+-- the whole list before deciding the result constructor.
+--
+-- >>> partitionEithersNE $ Left 'x' :| [Right 'y']
+-- These ('x' :| "") ('y' :| "")
+--
+-- >>> partitionEithersNE $ Left 'x' :| map Left "yz"
+-- This ('x' :| "yz")
+--
+-- @since 1.0.1
+partitionEithersNE :: NonEmpty (Either a b) -> These (NonEmpty a) (NonEmpty b)
+partitionEithersNE (x :| xs) = case (x, ls, rs) of
+ (Left y, ys, []) -> This (y :| ys)
+ (Left y, ys, (z:zs)) -> These (y :| ys) (z :| zs)
+ (Right z, [], zs) -> That (z :| zs)
+ (Right z, (y:ys), zs) -> These (y :| ys) (z :| zs)
+ where
+ (ls, rs) = partitionEithers xs
+
+
-------------------------------------------------------------------------------
-- Distributivity
-------------------------------------------------------------------------------
diff --git a/these.cabal b/these.cabal
index 8a383ac..047b861 100644
--- a/these.cabal
+++ b/these.cabal
@@ -1,6 +1,6 @@
cabal-version: >=1.10
name: these
-version: 1
+version: 1.0.1
synopsis: An either-or-both data type.
homepage: https://github.com/isomorphism/these
license: BSD3