antelude-0.1.0: src/Antelude/List/NonEmpty.hs
{- |
Module : Antelude.List.NonEmpty
Description : Contains some functions for NonEmptys, and reexports the Data.List.NonEmpty module.
Maintainer : dneavesdev@pm.me
-}
module Antelude.List.NonEmpty
( -- * Rexports
module NEExport
-- * New and Reconstructed for safety
, append
, appendList
, atIndex
, fromList
, prepend
) where
import safe Antelude.Function ( flip, (|>) )
import safe Antelude.Internal.TypesClasses
( Int
, List
, Maybe (..)
, Ordering (..)
)
import safe Antelude.List as AL ( head )
import safe Data.List.NonEmpty as NEExport hiding
( append
, appendList
, fromList
)
import safe qualified Data.List.NonEmpty as NE
import safe Prelude ( Ord (compare) )
-- | Obtain the element at the given index, starting at 0. 'Nothing' if the index is not valid.
atIndex :: Int -> NonEmpty a -> Maybe a
atIndex index lst = case compare index 0 of
LT ->
Nothing
_ ->
lst
|> NE.drop
index
|> AL.head
-- | Convert a `List a` to a `NonEmpty a`
fromList :: List a -> Maybe (NonEmpty a)
fromList = NE.nonEmpty
-- | Add an item to the beginning of a 'NonEmpty'.
prepend :: NonEmpty a -> NonEmpty a -> NonEmpty a
prepend = flip NE.append
-- | Add an item to the end of a 'NonEmpty'.
append :: NonEmpty a -> NonEmpty a -> NonEmpty a
append = NE.append
-- | Add a 'List' to the end of a 'NonEmpty'.
appendList :: List a -> NonEmpty a -> NonEmpty a
appendList = flip NE.appendList