graphql-engine/server/src-lib/Data/Sequence/NonEmpty.hs

38 lines
707 B
Haskell
Raw Normal View History

module Data.Sequence.NonEmpty
2018-06-27 16:11:32 +03:00
( NESeq
, (<|)
, (|>)
, init
, head
, toSeq
) where
import qualified Data.Foldable as F
2018-06-27 16:11:32 +03:00
import qualified Data.Sequence as Seq
import Prelude (Eq, Show, fst, (.))
2018-06-27 16:11:32 +03:00
infixr 5 <|
infixl 5 |>
newtype NESeq a
= NESeq { unNESeq :: (a, Seq.Seq a)}
deriving (Show, Eq)
instance F.Foldable NESeq where
foldr f v = F.foldr f v . toSeq
2018-06-27 16:11:32 +03:00
init :: a -> NESeq a
init a = NESeq (a, Seq.empty)
head :: NESeq a -> a
head = fst . unNESeq
(|>) :: NESeq a -> a -> NESeq a
(NESeq (h, l)) |> v = NESeq (h, l Seq.|> v)
(<|) :: a -> NESeq a -> NESeq a
v <| (NESeq (h, l)) = NESeq (v, h Seq.<| l)
toSeq :: NESeq a -> Seq.Seq a
toSeq (NESeq (v, l)) = v Seq.<| l