{-# LANGUAGE DeriveAnyClass #-}
module Data.List.NonEmptySep
( NonEmptySep (..)
, head
, fromList
) where
import Control.DeepSeq
import GHC.Generics
import Prelude hiding (head)
data NonEmptySep sep a
= Cons a sep (NonEmptySep sep a)
| Singleton a
deriving (Int -> NonEmptySep sep a -> ShowS
[NonEmptySep sep a] -> ShowS
NonEmptySep sep a -> String
(Int -> NonEmptySep sep a -> ShowS)
-> (NonEmptySep sep a -> String)
-> ([NonEmptySep sep a] -> ShowS)
-> Show (NonEmptySep sep a)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall sep a.
(Show sep, Show a) =>
Int -> NonEmptySep sep a -> ShowS
forall sep a. (Show sep, Show a) => [NonEmptySep sep a] -> ShowS
forall sep a. (Show sep, Show a) => NonEmptySep sep a -> String
$cshowsPrec :: forall sep a.
(Show sep, Show a) =>
Int -> NonEmptySep sep a -> ShowS
showsPrec :: Int -> NonEmptySep sep a -> ShowS
$cshow :: forall sep a. (Show sep, Show a) => NonEmptySep sep a -> String
show :: NonEmptySep sep a -> String
$cshowList :: forall sep a. (Show sep, Show a) => [NonEmptySep sep a] -> ShowS
showList :: [NonEmptySep sep a] -> ShowS
Show, (forall x. NonEmptySep sep a -> Rep (NonEmptySep sep a) x)
-> (forall x. Rep (NonEmptySep sep a) x -> NonEmptySep sep a)
-> Generic (NonEmptySep sep a)
forall x. Rep (NonEmptySep sep a) x -> NonEmptySep sep a
forall x. NonEmptySep sep a -> Rep (NonEmptySep sep a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall sep a x. Rep (NonEmptySep sep a) x -> NonEmptySep sep a
forall sep a x. NonEmptySep sep a -> Rep (NonEmptySep sep a) x
$cfrom :: forall sep a x. NonEmptySep sep a -> Rep (NonEmptySep sep a) x
from :: forall x. NonEmptySep sep a -> Rep (NonEmptySep sep a) x
$cto :: forall sep a x. Rep (NonEmptySep sep a) x -> NonEmptySep sep a
to :: forall x. Rep (NonEmptySep sep a) x -> NonEmptySep sep a
Generic)
deriving anyclass (NonEmptySep sep a -> ()
(NonEmptySep sep a -> ()) -> NFData (NonEmptySep sep a)
forall a. (a -> ()) -> NFData a
forall sep a. (NFData a, NFData sep) => NonEmptySep sep a -> ()
$crnf :: forall sep a. (NFData a, NFData sep) => NonEmptySep sep a -> ()
rnf :: NonEmptySep sep a -> ()
NFData)
instance Functor (NonEmptySep sep) where
fmap :: forall a b. (a -> b) -> NonEmptySep sep a -> NonEmptySep sep b
fmap a -> b
f (Singleton a
x) = b -> NonEmptySep sep b
forall sep a. a -> NonEmptySep sep a
Singleton (a -> b
f a
x)
fmap a -> b
f (Cons a
x sep
y NonEmptySep sep a
xs) = b -> sep -> NonEmptySep sep b -> NonEmptySep sep b
forall sep a. a -> sep -> NonEmptySep sep a -> NonEmptySep sep a
Cons (a -> b
f a
x) sep
y ((a -> b) -> NonEmptySep sep a -> NonEmptySep sep b
forall a b. (a -> b) -> NonEmptySep sep a -> NonEmptySep sep b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f NonEmptySep sep a
xs)
instance Foldable (NonEmptySep sep) where
foldr :: forall a b. (a -> b -> b) -> b -> NonEmptySep sep a -> b
foldr a -> b -> b
f b
e (Singleton a
a) = a -> b -> b
f a
a b
e
foldr a -> b -> b
f b
e (Cons a
x sep
_ NonEmptySep sep a
xs) = a -> b -> b
f a
x ((a -> b -> b) -> b -> NonEmptySep sep a -> b
forall a b. (a -> b -> b) -> b -> NonEmptySep sep a -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr a -> b -> b
f b
e NonEmptySep sep a
xs)
fromList :: ([(a, sep)], a) -> NonEmptySep sep a
fromList :: forall a sep. ([(a, sep)], a) -> NonEmptySep sep a
fromList ([], a
x) = a -> NonEmptySep sep a
forall sep a. a -> NonEmptySep sep a
Singleton a
x
fromList ((a
x, sep
y) : [(a, sep)]
ys, a
z) = a -> sep -> NonEmptySep sep a -> NonEmptySep sep a
forall sep a. a -> sep -> NonEmptySep sep a -> NonEmptySep sep a
Cons a
x sep
y (([(a, sep)], a) -> NonEmptySep sep a
forall a sep. ([(a, sep)], a) -> NonEmptySep sep a
fromList ([(a, sep)]
ys, a
z))
head :: NonEmptySep sep a -> a
head :: forall sep a. NonEmptySep sep a -> a
head (Singleton a
x) = a
x
head (Cons a
x sep
_ NonEmptySep sep a
_) = a
x