{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveFoldable #-}
{-# LANGUAGE DeriveTraversable #-}
module NonEmpty (
  NonEmpty(..)
, nonEmpty
, reverse
#ifdef TEST
, fromList
#endif
) where

import           Prelude ()
import           Test.Hspec.Core.Compat hiding (reverse)

import qualified Data.List as List
import qualified Data.Foldable as Foldable

data NonEmpty a = a :| [a]
  deriving (NonEmpty a -> NonEmpty a -> Bool
(NonEmpty a -> NonEmpty a -> Bool)
-> (NonEmpty a -> NonEmpty a -> Bool) -> Eq (NonEmpty a)
forall a. Eq a => NonEmpty a -> NonEmpty a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: NonEmpty a -> NonEmpty a -> Bool
$c/= :: forall a. Eq a => NonEmpty a -> NonEmpty a -> Bool
== :: NonEmpty a -> NonEmpty a -> Bool
$c== :: forall a. Eq a => NonEmpty a -> NonEmpty a -> Bool
Eq, Int -> NonEmpty a -> ShowS
[NonEmpty a] -> ShowS
NonEmpty a -> String
(Int -> NonEmpty a -> ShowS)
-> (NonEmpty a -> String)
-> ([NonEmpty a] -> ShowS)
-> Show (NonEmpty a)
forall a. Show a => Int -> NonEmpty a -> ShowS
forall a. Show a => [NonEmpty a] -> ShowS
forall a. Show a => NonEmpty a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [NonEmpty a] -> ShowS
$cshowList :: forall a. Show a => [NonEmpty a] -> ShowS
show :: NonEmpty a -> String
$cshow :: forall a. Show a => NonEmpty a -> String
showsPrec :: Int -> NonEmpty a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> NonEmpty a -> ShowS
Show, a -> NonEmpty b -> NonEmpty a
(a -> b) -> NonEmpty a -> NonEmpty b
(forall a b. (a -> b) -> NonEmpty a -> NonEmpty b)
-> (forall a b. a -> NonEmpty b -> NonEmpty a) -> Functor NonEmpty
forall a b. a -> NonEmpty b -> NonEmpty a
forall a b. (a -> b) -> NonEmpty a -> NonEmpty b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> NonEmpty b -> NonEmpty a
$c<$ :: forall a b. a -> NonEmpty b -> NonEmpty a
fmap :: (a -> b) -> NonEmpty a -> NonEmpty b
$cfmap :: forall a b. (a -> b) -> NonEmpty a -> NonEmpty b
Functor, NonEmpty a -> Bool
(a -> m) -> NonEmpty a -> m
(a -> b -> b) -> b -> NonEmpty a -> b
(forall m. Monoid m => NonEmpty m -> m)
-> (forall m a. Monoid m => (a -> m) -> NonEmpty a -> m)
-> (forall m a. Monoid m => (a -> m) -> NonEmpty a -> m)
-> (forall a b. (a -> b -> b) -> b -> NonEmpty a -> b)
-> (forall a b. (a -> b -> b) -> b -> NonEmpty a -> b)
-> (forall b a. (b -> a -> b) -> b -> NonEmpty a -> b)
-> (forall b a. (b -> a -> b) -> b -> NonEmpty a -> b)
-> (forall a. (a -> a -> a) -> NonEmpty a -> a)
-> (forall a. (a -> a -> a) -> NonEmpty a -> a)
-> (forall a. NonEmpty a -> [a])
-> (forall a. NonEmpty a -> Bool)
-> (forall a. NonEmpty a -> Int)
-> (forall a. Eq a => a -> NonEmpty a -> Bool)
-> (forall a. Ord a => NonEmpty a -> a)
-> (forall a. Ord a => NonEmpty a -> a)
-> (forall a. Num a => NonEmpty a -> a)
-> (forall a. Num a => NonEmpty a -> a)
-> Foldable NonEmpty
forall a. Eq a => a -> NonEmpty a -> Bool
forall a. Num a => NonEmpty a -> a
forall a. Ord a => NonEmpty a -> a
forall m. Monoid m => NonEmpty m -> m
forall a. NonEmpty a -> Bool
forall a. NonEmpty a -> Int
forall a. NonEmpty a -> [a]
forall a. (a -> a -> a) -> NonEmpty a -> a
forall m a. Monoid m => (a -> m) -> NonEmpty a -> m
forall b a. (b -> a -> b) -> b -> NonEmpty a -> b
forall a b. (a -> b -> b) -> b -> NonEmpty a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: NonEmpty a -> a
$cproduct :: forall a. Num a => NonEmpty a -> a
sum :: NonEmpty a -> a
$csum :: forall a. Num a => NonEmpty a -> a
minimum :: NonEmpty a -> a
$cminimum :: forall a. Ord a => NonEmpty a -> a
maximum :: NonEmpty a -> a
$cmaximum :: forall a. Ord a => NonEmpty a -> a
elem :: a -> NonEmpty a -> Bool
$celem :: forall a. Eq a => a -> NonEmpty a -> Bool
length :: NonEmpty a -> Int
$clength :: forall a. NonEmpty a -> Int
null :: NonEmpty a -> Bool
$cnull :: forall a. NonEmpty a -> Bool
toList :: NonEmpty a -> [a]
$ctoList :: forall a. NonEmpty a -> [a]
foldl1 :: (a -> a -> a) -> NonEmpty a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> NonEmpty a -> a
foldr1 :: (a -> a -> a) -> NonEmpty a -> a
$cfoldr1 :: forall a. (a -> a -> a) -> NonEmpty a -> a
foldl' :: (b -> a -> b) -> b -> NonEmpty a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> NonEmpty a -> b
foldl :: (b -> a -> b) -> b -> NonEmpty a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> NonEmpty a -> b
foldr' :: (a -> b -> b) -> b -> NonEmpty a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> NonEmpty a -> b
foldr :: (a -> b -> b) -> b -> NonEmpty a -> b
$cfoldr :: forall a b. (a -> b -> b) -> b -> NonEmpty a -> b
foldMap' :: (a -> m) -> NonEmpty a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> NonEmpty a -> m
foldMap :: (a -> m) -> NonEmpty a -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> NonEmpty a -> m
fold :: NonEmpty m -> m
$cfold :: forall m. Monoid m => NonEmpty m -> m
Foldable, Functor NonEmpty
Foldable NonEmpty
Functor NonEmpty
-> Foldable NonEmpty
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> NonEmpty a -> f (NonEmpty b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    NonEmpty (f a) -> f (NonEmpty a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> NonEmpty a -> m (NonEmpty b))
-> (forall (m :: * -> *) a.
    Monad m =>
    NonEmpty (m a) -> m (NonEmpty a))
-> Traversable NonEmpty
(a -> f b) -> NonEmpty a -> f (NonEmpty b)
forall (t :: * -> *).
Functor t
-> Foldable t
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a. Monad m => NonEmpty (m a) -> m (NonEmpty a)
forall (f :: * -> *) a.
Applicative f =>
NonEmpty (f a) -> f (NonEmpty a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> NonEmpty a -> m (NonEmpty b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> NonEmpty a -> f (NonEmpty b)
sequence :: NonEmpty (m a) -> m (NonEmpty a)
$csequence :: forall (m :: * -> *) a. Monad m => NonEmpty (m a) -> m (NonEmpty a)
mapM :: (a -> m b) -> NonEmpty a -> m (NonEmpty b)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> NonEmpty a -> m (NonEmpty b)
sequenceA :: NonEmpty (f a) -> f (NonEmpty a)
$csequenceA :: forall (f :: * -> *) a.
Applicative f =>
NonEmpty (f a) -> f (NonEmpty a)
traverse :: (a -> f b) -> NonEmpty a -> f (NonEmpty b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> NonEmpty a -> f (NonEmpty b)
$cp2Traversable :: Foldable NonEmpty
$cp1Traversable :: Functor NonEmpty
Traversable)

infixr 5 :|

nonEmpty :: [a] -> Maybe (NonEmpty a)
nonEmpty :: [a] -> Maybe (NonEmpty a)
nonEmpty []     = Maybe (NonEmpty a)
forall a. Maybe a
Nothing
nonEmpty (a
a:[a]
as) = NonEmpty a -> Maybe (NonEmpty a)
forall a. a -> Maybe a
Just (a
a a -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:| [a]
as)

reverse :: NonEmpty a -> NonEmpty a
reverse :: NonEmpty a -> NonEmpty a
reverse = ([a] -> [a]) -> NonEmpty a -> NonEmpty a
forall (f :: * -> *) a b.
Foldable f =>
([a] -> [b]) -> f a -> NonEmpty b
lift [a] -> [a]
forall a. [a] -> [a]
List.reverse

lift :: Foldable f => ([a] -> [b]) -> f a -> NonEmpty b
lift :: ([a] -> [b]) -> f a -> NonEmpty b
lift [a] -> [b]
f = [b] -> NonEmpty b
forall a. [a] -> NonEmpty a
fromList ([b] -> NonEmpty b) -> (f a -> [b]) -> f a -> NonEmpty b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> [b]
f ([a] -> [b]) -> (f a -> [a]) -> f a -> [b]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
Foldable.toList

fromList :: [a] -> NonEmpty a
fromList :: [a] -> NonEmpty a
fromList (a
a:[a]
as) = a
a a -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:| [a]
as
fromList [] = String -> NonEmpty a
forall a. HasCallStack => String -> a
error String
"NonEmpty.fromList: empty list"