{-# LANGUAGE CPP #-}
module Data.RAList.NonEmpty (
NERAList (..),
NERAList' (..),
explicitShow,
explicitShowsPrec,
singleton,
cons,
(!),
(!?),
head,
last,
uncons,
tail,
length,
null,
toNonEmpty,
fromNonEmpty,
foldMap1,
foldr1Map,
ifoldMap,
ifoldMap1,
ifoldr1Map,
adjust,
map,
imap,
itraverse,
#ifdef MIN_VERSION_semigroupoids
itraverse1,
#endif
) where
import Prelude (snd)
import Data.RAList.NonEmpty.Internal
import Data.RAList.Tree (Leaf (..), Node (..))
import Data.RAList.Internal (RAList (..))
tail :: NERAList a -> RAList a
tail r = snd (uncons r)
uncons :: NERAList a -> (a, RAList a)
uncons (NE (Last (Lf x))) = (x, Empty)
uncons (NE (Cons1 (Lf x) r)) = (x, NonEmpty (NE (Cons0 r)))
uncons (NE (Cons0 r)) =
let (Lf x, r') = unconsTree r in (x, NonEmpty (NE r'))
unconsTree :: NERAList' (Node t) a -> (t a, NERAList' t a)
unconsTree (Last (Nd x y)) = (x, Last y)
unconsTree (Cons1 (Nd x y) r) = (x, Cons1 y (Cons0 r))
unconsTree (Cons0 r) =
let (Nd x y, r') = unconsTree r in (x, Cons1 y r')