module Data.NonEmpty.Mixed where
import qualified Data.NonEmpty.Class as C
import qualified Data.NonEmpty as NonEmpty
import Data.Foldable (Foldable, foldr, )
import Prelude hiding (foldr, scanl, scanr, )
groupBy ::
(Foldable f) =>
(a -> a -> Bool) -> f a -> [NonEmpty.T [] a]
groupBy p =
foldr
(\x0 yt ->
let (xr,yr) =
case yt of
NonEmpty.Cons x1 xs : ys ->
if p x0 x1
then (x1:xs,ys)
else ([],yt)
[] -> ([],yt)
in NonEmpty.Cons x0 xr : yr)
[]
segmentBefore ::
(Foldable f) =>
(a -> Bool) -> f a -> ([a], [NonEmpty.T [] a])
segmentBefore p =
foldr
(\ x ys ->
if p x
then ([], NonEmpty.Cons x (fst ys) : snd ys)
else (x : fst ys, snd ys))
([],[])
mapAdjacent ::
(C.Cons f, C.Zip f) => (a -> a -> b) -> NonEmpty.T f a -> f b
mapAdjacent f xs =
C.zipWith f (NonEmpty.flatten xs) (NonEmpty.tail xs)
viewR :: (C.ViewR f, C.Empty f, C.Cons f) => NonEmpty.T f a -> (f a, a)
viewR (NonEmpty.Cons x xs) =
case C.viewR xs of
Nothing -> (C.empty, x)
Just (ys, y) -> (C.cons x ys, y)
init :: (C.ViewR f, C.Empty f, C.Cons f) => NonEmpty.T f a -> f a
init = fst . viewR
last :: (C.ViewR f) => NonEmpty.T f a -> a
last (NonEmpty.Cons x xs) =
case C.viewR xs of
Nothing -> x
Just (_, y) -> y
tails ::
(C.ViewL f, C.Empty f) =>
f a -> NonEmpty.T [] (f a)
tails xt =
NonEmpty.force $
case C.viewL xt of
Nothing -> NonEmpty.Cons C.empty []
Just (_, xs) -> C.cons xt $ tails xs
inits ::
(C.ViewL f, C.Cons f, C.Empty f) =>
f a -> NonEmpty.T [] (f a)
inits xt =
NonEmpty.Cons C.empty $
case C.viewL xt of
Nothing -> []
Just (x,xs) -> map (C.cons x) $ NonEmpty.flatten $ inits xs
appendLeft :: (C.Cons f) => [a] -> f a -> f a
appendLeft = flip $ foldr C.cons