{-# LANGUAGE TupleSections #-}
module TOML.Utils.NonEmpty (
zipHistory,
) where
import Data.List (scanl')
import Data.List.NonEmpty (NonEmpty ((:|)))
import qualified Data.List.NonEmpty as NonEmpty
zipHistory :: NonEmpty a -> NonEmpty (NonEmpty a, a)
zipHistory :: forall a. NonEmpty a -> NonEmpty (NonEmpty a, a)
zipHistory (a
a :| [a]
as) =
forall a. [a] -> NonEmpty a
NonEmpty.fromList forall a b. (a -> b) -> a -> b
$
forall b a. (b -> a -> b) -> b -> [a] -> [b]
scanl'
(\(NonEmpty a
history, a
_) a
x -> (forall {a}. NonEmpty a -> a -> NonEmpty a
append NonEmpty a
history a
x, a
x))
(forall {a}. a -> NonEmpty a
singleton a
a, a
a)
[a]
as
where
append :: NonEmpty a -> a -> NonEmpty a
append NonEmpty a
xs a
x = NonEmpty a
xs forall a. Semigroup a => a -> a -> a
<> forall {a}. a -> NonEmpty a
singleton a
x
singleton :: a -> NonEmpty a
singleton a
x = a
x forall a. a -> [a] -> NonEmpty a
:| []