packages feed

papa-0.7.0: src/Papa/Base/Data/Either.hs

{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wall #-}

-- | Sums, with optics for the two sides: the names 'left' and 'right'
-- ("Papa.Base.Data.Left", "Papa.Base.Data.Right"), and functions over
-- them.
module Papa.Base.Data.Either (
  module P,
  LeftLike (left),
  RightLike (right),
  lefts,
  rights,
  fromLeft,
  fromRight,
  partitionEithers,
) where

import Control.Category ((.))
import Control.Lens (Traversal, preview)
import Data.Either as P (
  Either (Left, Right),
  either,
  isLeft,
  isRight,
 )
import Data.Foldable (Foldable (foldr))
import Data.Functor.Const (Const)
import Data.Maybe (fromMaybe)
import Data.Monoid (First)
import Data.Traversable (Traversable (traverse))
import Papa.Base.Data.Left (LeftLike (left))
import Papa.Base.Data.Right (RightLike (right))

-- $setup
-- >>> :set -XNoImplicitPrelude
-- >>> import Papa
-- >>> import Hedgehog
-- >>> import qualified Hedgehog.Gen as Gen
-- >>> import qualified Hedgehog.Range as Range
-- >>> let genEither = Gen.either (Gen.int (Range.linear 0 9)) Gen.alpha
-- >>> let genEithers = Gen.list (Range.linear 0 20) genEither

-- | The 'Left' values of a 'Traversable' of 'Either's.
--
-- ==== __Examples__
--
-- >>> toListOf lefts [Left 1, Right 'x', Left (2 :: Int)]
-- [1,2]
--
-- >>> over lefts (+ 1) [Left 1, Right 'x', Left (2 :: Int)]
-- [Left 2,Right 'x',Left 3]
--
-- >>> check $ withTests 100 $ property $ do xs <- forAll genEithers; toListOf lefts xs === [a | Left a <- xs]
-- ...passed 100 tests.
-- True
--
-- @since 0.4.0
lefts :: (Traversable f) => Traversal (f (Either a b)) (f (Either a' b)) a a'
lefts =
  traverse . left
{-# INLINE lefts #-}

-- | The 'Right' values of a 'Traversable' of 'Either's.
--
-- ==== __Examples__
--
-- >>> toListOf rights [Left 1, Right 'x', Left (2 :: Int)]
-- "x"
--
-- >>> check $ withTests 100 $ property $ do xs <- forAll genEithers; toListOf rights xs === [b | Right b <- xs]
-- ...passed 100 tests.
-- True
--
-- @since 0.4.0
rights :: (Traversable f) => Traversal (f (Either a b)) (f (Either a b')) b b'
rights =
  traverse . right
{-# INLINE rights #-}

-- | The value of the name 'left', or the given default, for any data type
-- that has the name.
--
-- ==== __Examples__
--
-- >>> fromLeft 0 (Left 3 :: Either Int Char)
-- 3
--
-- >>> fromLeft 0 (Right 'x' :: Either Int Char)
-- 0
--
-- >>> check $ withTests 100 $ property $ do d <- forAll (Gen.int (Range.linear 0 9)); e <- forAll genEither; fromLeft d e === either id (\_ -> d) e
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
fromLeft :: (LeftLike (->) (Const (First a)) s s a a) => a -> s -> a
fromLeft a =
  fromMaybe a . preview left
{-# INLINE fromLeft #-}

-- | The value of the name 'right', or the given default, for any data type
-- that has the name.
--
-- ==== __Examples__
--
-- >>> fromRight 'z' (Right 'x' :: Either Int Char)
-- 'x'
--
-- >>> fromRight 'z' (Left 3 :: Either Int Char)
-- 'z'
--
-- >>> check $ withTests 100 $ property $ do d <- forAll Gen.alpha; e <- forAll genEither; fromRight d e === either (\_ -> d) id e
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
fromRight :: (RightLike (->) (Const (First b)) s s b b) => b -> s -> b
fromRight b =
  fromMaybe b . preview right
{-# INLINE fromRight #-}

-- | Partition the 'Either's of any 'Foldable' into the 'Left' values and
-- the 'Right' values, in order, in one lazy pass (as "Data.Either"'s
-- @partitionEithers@ does for a list).
--
-- ==== __Examples__
--
-- >>> partitionEithers [Left 1, Right 'x', Left (2 :: Int)]
-- ([1,2],"x")
--
-- >>> partitionEithers (Just (Right 'x' :: Either Int Char))
-- ([],"x")
--
-- >>> check $ withTests 100 $ property $ do xs <- forAll genEithers; partitionEithers xs === (toListOf lefts xs, toListOf rights xs)
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
partitionEithers :: (Foldable f) => f (Either a b) -> ([a], [b])
partitionEithers =
  foldr (\e ~(ls, rs) -> either (\a -> (a : ls, rs)) (\b -> (ls, b : rs)) e) ([], [])
{-# INLINE partitionEithers #-}