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

infixr 5 :|

nonEmpty :: [a] -> Maybe (NonEmpty a)
nonEmpty :: forall a. [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 :: forall a. 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 :: forall (f :: * -> *) a b.
Foldable f =>
([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 a. f a -> [a]
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 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"