{-# 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
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
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, 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
<$ :: forall a b. a -> NonEmpty b -> NonEmpty a
$c<$ :: forall a b. a -> NonEmpty b -> NonEmpty a
fmap :: forall a b. (a -> b) -> NonEmpty a -> NonEmpty b
$cfmap :: forall a b. (a -> b) -> NonEmpty a -> NonEmpty b
Functor, 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 :: forall a. Num a => NonEmpty a -> a
$cproduct :: forall a. Num a => NonEmpty a -> a
sum :: forall a. Num a => NonEmpty a -> a
$csum :: forall a. Num a => NonEmpty a -> a
minimum :: forall a. Ord a => NonEmpty a -> a
$cminimum :: forall a. Ord a => NonEmpty a -> a
maximum :: forall a. Ord a => NonEmpty a -> a
$cmaximum :: forall a. Ord a => NonEmpty a -> a
elem :: forall a. Eq a => a -> NonEmpty a -> Bool
$celem :: forall a. Eq a => a -> NonEmpty a -> Bool
length :: forall a. NonEmpty a -> Int
$clength :: forall a. NonEmpty a -> Int
null :: forall a. NonEmpty a -> Bool
$cnull :: forall a. NonEmpty a -> Bool
toList :: forall a. NonEmpty a -> [a]
$ctoList :: forall a. NonEmpty a -> [a]
foldl1 :: forall a. (a -> a -> a) -> NonEmpty a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> NonEmpty a -> a
foldr1 :: forall a. (a -> a -> a) -> NonEmpty a -> a
$cfoldr1 :: forall a. (a -> a -> a) -> NonEmpty a -> a
foldl' :: forall b a. (b -> a -> b) -> b -> NonEmpty a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> NonEmpty a -> b
foldl :: forall b a. (b -> a -> b) -> b -> NonEmpty a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> NonEmpty a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> NonEmpty a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> NonEmpty a -> b
foldr :: forall a b. (a -> b -> b) -> b -> NonEmpty a -> b
$cfoldr :: forall a b. (a -> b -> b) -> b -> NonEmpty a -> b
foldMap' :: forall m a. Monoid m => (a -> m) -> NonEmpty a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> NonEmpty a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> NonEmpty a -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> NonEmpty a -> m
fold :: forall m. Monoid m => NonEmpty m -> m
$cfold :: forall m. Monoid m => NonEmpty m -> m
Foldable, Functor NonEmpty
Foldable NonEmpty
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 :: forall (m :: * -> *) a. Monad m => NonEmpty (m a) -> m (NonEmpty a)
$csequence :: forall (m :: * -> *) a. Monad m => NonEmpty (m a) -> m (NonEmpty a)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> NonEmpty a -> m (NonEmpty b)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> NonEmpty a -> m (NonEmpty b)
sequenceA :: forall (f :: * -> *) a.
Applicative f =>
NonEmpty (f a) -> f (NonEmpty a)
$csequenceA :: forall (f :: * -> *) a.
Applicative f =>
NonEmpty (f a) -> f (NonEmpty a)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> NonEmpty a -> f (NonEmpty b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> NonEmpty a -> f (NonEmpty b)
Traversable)

infixr 5 :|

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

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

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

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