]> Git — Sourcephile - comptalang.git/blob - lib/Hcompta/Lib/NonEmpty.hs
Ajout : Calculus.Lambda.Omega.Explicit.
[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.Maybe (Maybe(..))
7 import Data.Monoid (Monoid(..))
8 import Prelude ((.))
9
10
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 #-}
16
17 -- | Return all the prefixes of the given 'NonEmpty' (including itself).
18 prefixes :: NonEmpty x -> [NonEmpty x]
19 prefixes (y:|ys) = go y [] ys []
20 where
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)