module Data.Text.PgpWordlist.Internal.AltList (
AltList(..)
, fromList
, toList
, first
, second
, bimap
, bisequence
, bitraverse
) where
import Control.Applicative
#if MIN_VERSION_base(4,8,0)
import qualified Data.Bifunctor as Bi
#endif
data AltList a b = Nil | a :<> AltList b a
deriving (Eq, Ord, Show)
infixr 5 :<>
fromList :: [a] -> AltList a a
fromList = foldr (:<>) Nil
toList :: AltList a a -> [a]
toList Nil = []
toList (x :<> xs) = x : toList xs
first :: (a -> a') -> AltList a b -> AltList a' b
first _ Nil = Nil
first f (x :<> Nil) = f x :<> Nil
first f (x :<> y :<> ys) = f x :<> y :<> first f ys
second :: (b -> b') -> AltList a b -> AltList a b'
second _ Nil = Nil
second g (x :<> xs) = x :<> first g xs
bimap :: (a -> a') -> (b -> b') -> AltList a b -> AltList a' b'
bimap _ _ Nil = Nil
bimap f g (x :<> xs) = f x :<> bimap g f xs
#if MIN_VERSION_base(4,8,0)
instance Bi.Bifunctor AltList where
first = first
second = second
bimap = bimap
#endif
bisequence :: Applicative f => AltList (f a) (f b) -> f (AltList a b)
bisequence Nil = pure Nil
bisequence (x :<> xs) = liftA2 (:<>) x (bisequence xs)
bitraverse :: Applicative f => (a -> f c) -> (b -> f d) -> AltList a b -> f (AltList c d)
bitraverse _ _ Nil = pure Nil
bitraverse f g (x :<> xs) = liftA2 (:<>) (f x) (bitraverse g f xs)