module Hcompta.Lib.NonEmpty where import qualified Data.List -- import qualified Data.List.NonEmpty as NonEmpty import Data.List.NonEmpty (NonEmpty(..)) import Data.Monoid ((<>)) -- | Return the given 'NonEmpty' without its last section if any. ascending :: NonEmpty x -> Maybe (NonEmpty x) ascending (_:|[]) = Nothing ascending (x:|xs) = Just (x:|Data.List.init xs) {-# INLINE ascending #-} -- | Return all the prefixes of the given 'NonEmpty' (including itself). prefixes :: NonEmpty x -> [NonEmpty x] prefixes (y:|ys) = go y [] ys [] where go :: x -> [x] -> [x] -> [NonEmpty x] -> [NonEmpty x] go x0 s [] = (:) (x0:|s) go x0 s (x:xs) = go x0 (s <> [x]) xs . (:) (x0:|s)