]> Git — Sourcephile - comptalang.git/blob - lib/Hcompta/Lib/NonEmpty.hs
Sync with symantic.
[comptalang.git] / lib / Hcompta / Lib / NonEmpty.hs
1 module Hcompta.Lib.NonEmpty where
2
3 import Data.Function ((.))
4 import qualified Data.List
5 import Data.List.NonEmpty (NonEmpty(..))
6 import Data.Maybe (Maybe(..))
7 import Data.Monoid (Monoid(..))
8
9 -- | Return the given 'NonEmpty' without its last section if any.
10 parent :: NonEmpty x -> Maybe (NonEmpty x)
11 parent (_:|[]) = Nothing
12 parent (x:|xs) = Just (x:|Data.List.init xs)
13
14 -- | Return all the ancestors (i.e. prefixes)
15 -- of the given 'NonEmpty' (including itself).
16 ancestors_or_self :: NonEmpty x -> [NonEmpty x]
17 ancestors_or_self (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 `mappend` [x]) xs . (:) (x0:|s)