1 module Hcompta.Lib.NonEmpty where
3 import qualified Data.List
4 -- import qualified Data.List.NonEmpty as NonEmpty
5 import Data.List.NonEmpty (NonEmpty(..))
6 import Data.Maybe (Maybe(..))
7 import Data.Monoid (Monoid(..))
11 -- | Return the given 'NonEmpty' without its last section if any.
12 ascending :: NonEmpty x -> Maybe (NonEmpty x)
13 ascending (_:|[]) = Nothing
14 ascending (x:|xs) = Just (x:|Data.List.init xs)
15 {-# INLINE ascending #-}
17 -- | Return all the prefixes of the given 'NonEmpty' (including itself).
18 prefixes :: NonEmpty x -> [NonEmpty x]
19 prefixes (y:|ys) = go y [] ys []
21 go :: x -> [x] -> [x] -> [NonEmpty x] -> [NonEmpty x]
22 go x0 s [] = (:) (x0:|s)
23 go x0 s (x:xs) = go x0 (s `mappend` [x]) xs . (:) (x0:|s)