{-# LANGUAGE CPP #-}
{-# LANGUAGE Trustworthy #-}
module Universum.List.Safe
( list
, uncons
#if ( __GLASGOW_HASKELL__ >= 800 )
, whenNotNull
, whenNotNullM
#endif
) where
#if ( __GLASGOW_HASKELL__ >= 800 )
import Universum.Applicative (Applicative, pass)
import Universum.Monad (Monad (..))
import Universum.Monoid (NonEmpty (..))
#endif
import Universum.Functor (fmap)
import Universum.Monad (Maybe (..))
list :: [b] -> (a -> b) -> [a] -> [b]
list def f xs = case xs of
[] -> def
_ -> fmap f xs
uncons :: [a] -> Maybe (a, [a])
uncons [] = Nothing
uncons (x:xs) = Just (x, xs)
#if ( __GLASGOW_HASKELL__ >= 800 )
whenNotNull :: Applicative f => [a] -> (NonEmpty a -> f ()) -> f ()
whenNotNull [] _ = pass
whenNotNull (x:xs) f = f (x :| xs)
{-# INLINE whenNotNull #-}
whenNotNullM :: Monad m => m [a] -> (NonEmpty a -> m ()) -> m ()
whenNotNullM ml f = ml >>= \l -> whenNotNull l f
{-# INLINE whenNotNullM #-}
#endif