]> Git — Sourcephile - comptalang.git/blob - lib/Hcompta/Lib/NonEmpty.hs
Ajout : CLI.Command.{Journals,Stats,Tags}.
[comptalang.git] / lib / Hcompta / Lib / NonEmpty.hs
1 module Hcompta.Lib.NonEmpty where
2
3 import qualified Data.List
4 -- import qualified Data.List.NonEmpty as NonEmpty
5 import Data.List.NonEmpty (NonEmpty(..))
6 import Data.Monoid ((<>))
7
8
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 #-}
14
15 -- | Return all the prefixes of the given 'NonEmpty' (including itself).
16 prefixes :: NonEmpty x -> [NonEmpty x]
17 prefixes (y:|ys) = go y [] ys []
18 where
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)