-- | Functions for NonEmpty lists.
module Util.NEs where
import qualified Data.List as List
import           Data.List.NonEmpty (NonEmpty(..))
import qualified Data.Maybe as Maybe

import qualified Util.Lists as Lists


unsnoc :: NonEmpty a -> ([a], a)
unsnoc :: forall a. NonEmpty a -> ([a], a)
unsnoc (a
x :| [a]
xs) =
    forall a. a -> Maybe a -> a
Maybe.fromMaybe (forall a. HasCallStack => [Char] -> a
error [Char]
"unsnoc: not reached") (forall a. [a] -> Maybe ([a], a)
Lists.unsnoc (a
x forall a. a -> [a] -> [a]
: [a]
xs))

minimum :: Ord a => NonEmpty a -> a
minimum :: forall a. Ord a => NonEmpty a -> a
minimum (a
x :| [a]
xs) = forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
List.minimum (a
x forall a. a -> [a] -> [a]
: [a]
xs)

maximum :: Ord a => NonEmpty a -> a
maximum :: forall a. Ord a => NonEmpty a -> a
maximum (a
x :| [a]
xs) = forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
List.maximum (a
x forall a. a -> [a] -> [a]
: [a]
xs)