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.Monoid ((<>))
9 -- | Return the given 'NonEmpty' without its last section if any.
10 ascending :: NonEmpty x -> Maybe (NonEmpty x)
11 ascending (_:|[]) = Nothing
12 ascending (x:|xs) = Just (x:|Data.List.init xs)
13 {-# INLINE ascending #-}
15 -- | Return all the prefixes of the given 'NonEmpty' (including itself).
16 prefixes :: NonEmpty x -> [NonEmpty x]
17 prefixes (y:|ys) = go y [] ys []
19 go :: x -> [x] -> [x] -> [NonEmpty x] -> [NonEmpty x]
20 go x0 s [] = (:) (x0:|s)
21 go x0 s (x:xs) = go x0 (s <> [x]) xs . (:) (x0:|s)