{-# LANGUAGE CPP #-}
module Data.List.NonEmpty.Extra(
module Data.List.NonEmpty,
(|:), (|>), snoc,
appendl, appendr,
sortOn, union, unionBy,
maximum1, minimum1, maximumBy1, minimumBy1, maximumOn1, minimumOn1
) where
import Data.Function
import qualified Data.List as List
import Data.List.NonEmpty
#if __GLASGOW_HASKELL__ <= 802
import Data.Semigroup ((<>))
#endif
infixl 5 |>, |:
(|>) :: NonEmpty a -> a -> NonEmpty a
(|>) xs x = xs <> pure x
snoc :: NonEmpty a -> a -> NonEmpty a
snoc = (|>)
(|:) :: [a] -> a -> NonEmpty a
(|:) xs x = foldr cons (pure x) xs
appendl :: NonEmpty a -> [a] -> NonEmpty a
appendl (x :| xs) l = x :| (xs ++ l)
appendr :: [a] -> NonEmpty a -> NonEmpty a
appendr l nel = foldr cons nel l
sortOn :: Ord b => (a -> b) -> NonEmpty a -> NonEmpty a
sortOn f = fromList . List.sortOn f . toList
union :: Eq a => NonEmpty a -> NonEmpty a -> NonEmpty a
union = unionBy (==)
unionBy :: (a -> a -> Bool) -> NonEmpty a -> NonEmpty a -> NonEmpty a
unionBy eq xs ys = fromList $ List.unionBy eq (toList xs) (toList ys)
maximum1 :: Ord a => NonEmpty a -> a
maximum1 = List.maximum
minimum1 :: Ord a => NonEmpty a -> a
minimum1 = List.minimum
maximumBy1 :: (a -> a -> Ordering) -> NonEmpty a -> a
maximumBy1 = List.maximumBy
minimumBy1 :: (a -> a -> Ordering) -> NonEmpty a -> a
minimumBy1 = List.minimumBy
maximumOn1 :: Ord b => (a -> b) -> NonEmpty a -> a
maximumOn1 f = maximumBy1 (compare `on` f)
minimumOn1 :: Ord b => (a -> b) -> NonEmpty a -> a
minimumOn1 f = minimumBy1 (compare `on` f)