{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -Wno-orphans #-}
module HGeometry.Sequence.NonEmpty
( ViewL1(..)
, ViewR1(..)
, viewl1, viewr1
, (|>>)
, (<>>)
, splitL1At
, splitR1At
) where
import Control.DeepSeq
import Control.Lens
import qualified Data.Foldable as F
import Data.Foldable1
import Data.Foldable1.WithIndex
import Data.Functor.Apply
import Data.List.NonEmpty (NonEmpty(..))
import qualified Data.List.NonEmpty as NonEmpty
import Data.Semigroup.Traversable
import Data.Sequence (Seq(..))
import qualified Data.Sequence as Sequence
import GHC.Exts (IsList(..))
import GHC.Generics
import HGeometry.Foldable.Util (HasFromFoldable1(..))
data ViewL1 a = a :<< Seq a
deriving stock (Int -> ViewL1 a -> ShowS
[ViewL1 a] -> ShowS
ViewL1 a -> String
(Int -> ViewL1 a -> ShowS)
-> (ViewL1 a -> String) -> ([ViewL1 a] -> ShowS) -> Show (ViewL1 a)
forall a. Show a => Int -> ViewL1 a -> ShowS
forall a. Show a => [ViewL1 a] -> ShowS
forall a. Show a => ViewL1 a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> ViewL1 a -> ShowS
showsPrec :: Int -> ViewL1 a -> ShowS
$cshow :: forall a. Show a => ViewL1 a -> String
show :: ViewL1 a -> String
$cshowList :: forall a. Show a => [ViewL1 a] -> ShowS
showList :: [ViewL1 a] -> ShowS
Show,ReadPrec [ViewL1 a]
ReadPrec (ViewL1 a)
Int -> ReadS (ViewL1 a)
ReadS [ViewL1 a]
(Int -> ReadS (ViewL1 a))
-> ReadS [ViewL1 a]
-> ReadPrec (ViewL1 a)
-> ReadPrec [ViewL1 a]
-> Read (ViewL1 a)
forall a. Read a => ReadPrec [ViewL1 a]
forall a. Read a => ReadPrec (ViewL1 a)
forall a. Read a => Int -> ReadS (ViewL1 a)
forall a. Read a => ReadS [ViewL1 a]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: forall a. Read a => Int -> ReadS (ViewL1 a)
readsPrec :: Int -> ReadS (ViewL1 a)
$creadList :: forall a. Read a => ReadS [ViewL1 a]
readList :: ReadS [ViewL1 a]
$creadPrec :: forall a. Read a => ReadPrec (ViewL1 a)
readPrec :: ReadPrec (ViewL1 a)
$creadListPrec :: forall a. Read a => ReadPrec [ViewL1 a]
readListPrec :: ReadPrec [ViewL1 a]
Read,ViewL1 a -> ViewL1 a -> Bool
(ViewL1 a -> ViewL1 a -> Bool)
-> (ViewL1 a -> ViewL1 a -> Bool) -> Eq (ViewL1 a)
forall a. Eq a => ViewL1 a -> ViewL1 a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Eq a => ViewL1 a -> ViewL1 a -> Bool
== :: ViewL1 a -> ViewL1 a -> Bool
$c/= :: forall a. Eq a => ViewL1 a -> ViewL1 a -> Bool
/= :: ViewL1 a -> ViewL1 a -> Bool
Eq,Eq (ViewL1 a)
Eq (ViewL1 a) =>
(ViewL1 a -> ViewL1 a -> Ordering)
-> (ViewL1 a -> ViewL1 a -> Bool)
-> (ViewL1 a -> ViewL1 a -> Bool)
-> (ViewL1 a -> ViewL1 a -> Bool)
-> (ViewL1 a -> ViewL1 a -> Bool)
-> (ViewL1 a -> ViewL1 a -> ViewL1 a)
-> (ViewL1 a -> ViewL1 a -> ViewL1 a)
-> Ord (ViewL1 a)
ViewL1 a -> ViewL1 a -> Bool
ViewL1 a -> ViewL1 a -> Ordering
ViewL1 a -> ViewL1 a -> ViewL1 a
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall a. Ord a => Eq (ViewL1 a)
forall a. Ord a => ViewL1 a -> ViewL1 a -> Bool
forall a. Ord a => ViewL1 a -> ViewL1 a -> Ordering
forall a. Ord a => ViewL1 a -> ViewL1 a -> ViewL1 a
$ccompare :: forall a. Ord a => ViewL1 a -> ViewL1 a -> Ordering
compare :: ViewL1 a -> ViewL1 a -> Ordering
$c< :: forall a. Ord a => ViewL1 a -> ViewL1 a -> Bool
< :: ViewL1 a -> ViewL1 a -> Bool
$c<= :: forall a. Ord a => ViewL1 a -> ViewL1 a -> Bool
<= :: ViewL1 a -> ViewL1 a -> Bool
$c> :: forall a. Ord a => ViewL1 a -> ViewL1 a -> Bool
> :: ViewL1 a -> ViewL1 a -> Bool
$c>= :: forall a. Ord a => ViewL1 a -> ViewL1 a -> Bool
>= :: ViewL1 a -> ViewL1 a -> Bool
$cmax :: forall a. Ord a => ViewL1 a -> ViewL1 a -> ViewL1 a
max :: ViewL1 a -> ViewL1 a -> ViewL1 a
$cmin :: forall a. Ord a => ViewL1 a -> ViewL1 a -> ViewL1 a
min :: ViewL1 a -> ViewL1 a -> ViewL1 a
Ord,(forall a b. (a -> b) -> ViewL1 a -> ViewL1 b)
-> (forall a b. a -> ViewL1 b -> ViewL1 a) -> Functor ViewL1
forall a b. a -> ViewL1 b -> ViewL1 a
forall a b. (a -> b) -> ViewL1 a -> ViewL1 b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> ViewL1 a -> ViewL1 b
fmap :: forall a b. (a -> b) -> ViewL1 a -> ViewL1 b
$c<$ :: forall a b. a -> ViewL1 b -> ViewL1 a
<$ :: forall a b. a -> ViewL1 b -> ViewL1 a
Functor,Functor ViewL1
Foldable ViewL1
(Functor ViewL1, Foldable ViewL1) =>
(forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> ViewL1 a -> f (ViewL1 b))
-> (forall (f :: * -> *) a.
Applicative f =>
ViewL1 (f a) -> f (ViewL1 a))
-> (forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> ViewL1 a -> m (ViewL1 b))
-> (forall (m :: * -> *) a.
Monad m =>
ViewL1 (m a) -> m (ViewL1 a))
-> Traversable ViewL1
forall (t :: * -> *).
(Functor t, Foldable t) =>
(forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a. Monad m => ViewL1 (m a) -> m (ViewL1 a)
forall (f :: * -> *) a.
Applicative f =>
ViewL1 (f a) -> f (ViewL1 a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> ViewL1 a -> m (ViewL1 b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> ViewL1 a -> f (ViewL1 b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> ViewL1 a -> f (ViewL1 b)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> ViewL1 a -> f (ViewL1 b)
$csequenceA :: forall (f :: * -> *) a.
Applicative f =>
ViewL1 (f a) -> f (ViewL1 a)
sequenceA :: forall (f :: * -> *) a.
Applicative f =>
ViewL1 (f a) -> f (ViewL1 a)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> ViewL1 a -> m (ViewL1 b)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> ViewL1 a -> m (ViewL1 b)
$csequence :: forall (m :: * -> *) a. Monad m => ViewL1 (m a) -> m (ViewL1 a)
sequence :: forall (m :: * -> *) a. Monad m => ViewL1 (m a) -> m (ViewL1 a)
Traversable,(forall x. ViewL1 a -> Rep (ViewL1 a) x)
-> (forall x. Rep (ViewL1 a) x -> ViewL1 a) -> Generic (ViewL1 a)
forall x. Rep (ViewL1 a) x -> ViewL1 a
forall x. ViewL1 a -> Rep (ViewL1 a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (ViewL1 a) x -> ViewL1 a
forall a x. ViewL1 a -> Rep (ViewL1 a) x
$cfrom :: forall a x. ViewL1 a -> Rep (ViewL1 a) x
from :: forall x. ViewL1 a -> Rep (ViewL1 a) x
$cto :: forall a x. Rep (ViewL1 a) x -> ViewL1 a
to :: forall x. Rep (ViewL1 a) x -> ViewL1 a
Generic)
instance NFData a => NFData (ViewL1 a)
type instance Index (ViewL1 a) = Int
type instance IxValue (ViewL1 a) = a
instance Ixed (ViewL1 a) where
ix :: Index (ViewL1 a) -> Traversal' (ViewL1 a) (IxValue (ViewL1 a))
ix Index (ViewL1 a)
i IxValue (ViewL1 a) -> f (IxValue (ViewL1 a))
f (a
x :<< Seq a
xs)
| Int
Index (ViewL1 a)
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 = (a -> Seq a -> ViewL1 a
forall a. a -> Seq a -> ViewL1 a
:<< Seq a
xs) (a -> ViewL1 a) -> f a -> f (ViewL1 a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IxValue (ViewL1 a) -> f (IxValue (ViewL1 a))
f a
IxValue (ViewL1 a)
x
| Bool
otherwise = (a
x a -> Seq a -> ViewL1 a
forall a. a -> Seq a -> ViewL1 a
:<<) (Seq a -> ViewL1 a) -> f (Seq a) -> f (ViewL1 a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Index (Seq a) -> Traversal' (Seq a) (IxValue (Seq a))
forall m. Ixed m => Index m -> Traversal' m (IxValue m)
ix (Int
Index (ViewL1 a)
iInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) IxValue (Seq a) -> f (IxValue (Seq a))
IxValue (ViewL1 a) -> f (IxValue (ViewL1 a))
f Seq a
xs
{-# INLINE ix #-}
instance Foldable ViewL1 where
foldMap :: forall m a. Monoid m => (a -> m) -> ViewL1 a -> m
foldMap a -> m
f (a
x :<< Seq a
xs) = a -> m
f a
x m -> m -> m
forall a. Semigroup a => a -> a -> a
<> (a -> m) -> Seq a -> m
forall m a. Monoid m => (a -> m) -> Seq a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap a -> m
f Seq a
xs
{-# INLINE foldMap #-}
length :: forall a. ViewL1 a -> Int
length (a
_ :<< Seq a
xs) = Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Seq a -> Int
forall a. Seq a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length Seq a
xs
{-# INLINE length #-}
null :: forall a. ViewL1 a -> Bool
null ViewL1 a
_ = Bool
False
{-# INLINE null #-}
instance Foldable1 ViewL1 where
foldMap1 :: forall m a. Semigroup m => (a -> m) -> ViewL1 a -> m
foldMap1 a -> m
f (a
a :<< Seq a
as) = case (a -> Maybe m) -> Seq a -> Maybe m
forall m a. Monoid m => (a -> m) -> Seq a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (m -> Maybe m
forall a. a -> Maybe a
Just (m -> Maybe m) -> (a -> m) -> a -> Maybe m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m
f) Seq a
as of
Maybe m
Nothing -> a -> m
f a
a
Just m
b -> a -> m
f a
a m -> m -> m
forall a. Semigroup a => a -> a -> a
<> m
b
{-# INLINE foldMap1 #-}
instance Traversable1 ViewL1 where
traverse1 :: forall (f :: * -> *) a b.
Apply f =>
(a -> f b) -> ViewL1 a -> f (ViewL1 b)
traverse1 a -> f b
f (a
a :<< Seq a
as) = b -> Seq b -> ViewL1 b
forall a. a -> Seq a -> ViewL1 a
(:<<) (b -> Seq b -> ViewL1 b) -> f b -> f (Seq b -> ViewL1 b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
a f (Seq b -> ViewL1 b) -> MaybeApply f (Seq b) -> f (ViewL1 b)
forall (f :: * -> *) a b.
Apply f =>
f (a -> b) -> MaybeApply f a -> f b
<.*> (a -> f b) -> Seq a -> MaybeApply f (Seq b)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Apply f) =>
(a -> f b) -> t a -> MaybeApply f (t b)
traverse1Maybe a -> f b
f Seq a
as
{-# INLINE traverse1 #-}
instance FunctorWithIndex Int ViewL1
instance FoldableWithIndex Int ViewL1
instance TraversableWithIndex Int ViewL1
instance Foldable1WithIndex Int ViewL1 where
ifoldMap1 :: forall m a. Semigroup m => (Int -> a -> m) -> ViewL1 a -> m
ifoldMap1 Int -> a -> m
f (a
a :<< Seq a
as) = case (Int -> a -> Maybe m) -> Seq a -> Maybe m
forall m a. Monoid m => (Int -> a -> m) -> Seq a -> m
forall i (f :: * -> *) m a.
(FoldableWithIndex i f, Monoid m) =>
(i -> a -> m) -> f a -> m
ifoldMap (\Int
i -> m -> Maybe m
forall a. a -> Maybe a
Just (m -> Maybe m) -> (a -> m) -> a -> Maybe m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> a -> m
f (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)) Seq a
as of
Maybe m
Nothing -> Int -> a -> m
f Int
0 a
a
Just m
b -> Int -> a -> m
f Int
0 a
a m -> m -> m
forall a. Semigroup a => a -> a -> a
<> m
b
{-# INLINE ifoldMap1 #-}
instance Semigroup (ViewL1 a) where
(a
a :<< Seq a
as) <> :: ViewL1 a -> ViewL1 a -> ViewL1 a
<> (a
b :<< Seq a
bs) = a
a a -> Seq a -> ViewL1 a
forall a. a -> Seq a -> ViewL1 a
:<< (Seq a
as Seq a -> Seq a -> Seq a
forall a. Semigroup a => a -> a -> a
<> (a
b a -> Seq a -> Seq a
forall a. a -> Seq a -> Seq a
:<| Seq a
bs))
{-# INLINE (<>) #-}
instance HasFromFoldable1 ViewL1 where
fromNonEmpty :: forall a. NonEmpty a -> ViewL1 a
fromNonEmpty (a
a :| [a]
as) = a
a a -> Seq a -> ViewL1 a
forall a. a -> Seq a -> ViewL1 a
:<< [Item (Seq a)] -> Seq a
forall l. IsList l => [Item l] -> l
fromList [a]
[Item (Seq a)]
as
{-# INLINE fromNonEmpty #-}
instance IsList (ViewL1 a) where
type Item (ViewL1 a) = a
toList :: ViewL1 a -> [Item (ViewL1 a)]
toList = ViewL1 a -> [a]
ViewL1 a -> [Item (ViewL1 a)]
forall a. ViewL1 a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
F.toList
{-# INLINE toList #-}
fromList :: [Item (ViewL1 a)] -> ViewL1 a
fromList = ViewL1 a
-> (NonEmpty a -> ViewL1 a) -> Maybe (NonEmpty a) -> ViewL1 a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (String -> ViewL1 a
forall a. HasCallStack => String -> a
error String
"ViewL1 fromList; empty List") NonEmpty a -> ViewL1 a
forall a. NonEmpty a -> ViewL1 a
forall (f :: * -> *) a. HasFromFoldable1 f => NonEmpty a -> f a
fromNonEmpty (Maybe (NonEmpty a) -> ViewL1 a)
-> ([a] -> Maybe (NonEmpty a)) -> [a] -> ViewL1 a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> Maybe (NonEmpty a)
forall a. [a] -> Maybe (NonEmpty a)
NonEmpty.nonEmpty
{-# INLINE fromList #-}
instance Reversing (ViewL1 a) where
reversing :: ViewL1 a -> ViewL1 a
reversing (a
x :<< Seq a
s) = ViewR1 a -> ViewL1 a
forall a. ViewR1 a -> ViewL1 a
viewl1 (ViewR1 a -> ViewL1 a) -> ViewR1 a -> ViewL1 a
forall a b. (a -> b) -> a -> b
$ Seq a -> Seq a
forall a. Seq a -> Seq a
Sequence.reverse Seq a
s Seq a -> a -> ViewR1 a
forall a. Seq a -> a -> ViewR1 a
:>> a
x
data ViewR1 a = Seq a :>> a
deriving stock (Int -> ViewR1 a -> ShowS
[ViewR1 a] -> ShowS
ViewR1 a -> String
(Int -> ViewR1 a -> ShowS)
-> (ViewR1 a -> String) -> ([ViewR1 a] -> ShowS) -> Show (ViewR1 a)
forall a. Show a => Int -> ViewR1 a -> ShowS
forall a. Show a => [ViewR1 a] -> ShowS
forall a. Show a => ViewR1 a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> ViewR1 a -> ShowS
showsPrec :: Int -> ViewR1 a -> ShowS
$cshow :: forall a. Show a => ViewR1 a -> String
show :: ViewR1 a -> String
$cshowList :: forall a. Show a => [ViewR1 a] -> ShowS
showList :: [ViewR1 a] -> ShowS
Show,ReadPrec [ViewR1 a]
ReadPrec (ViewR1 a)
Int -> ReadS (ViewR1 a)
ReadS [ViewR1 a]
(Int -> ReadS (ViewR1 a))
-> ReadS [ViewR1 a]
-> ReadPrec (ViewR1 a)
-> ReadPrec [ViewR1 a]
-> Read (ViewR1 a)
forall a. Read a => ReadPrec [ViewR1 a]
forall a. Read a => ReadPrec (ViewR1 a)
forall a. Read a => Int -> ReadS (ViewR1 a)
forall a. Read a => ReadS [ViewR1 a]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: forall a. Read a => Int -> ReadS (ViewR1 a)
readsPrec :: Int -> ReadS (ViewR1 a)
$creadList :: forall a. Read a => ReadS [ViewR1 a]
readList :: ReadS [ViewR1 a]
$creadPrec :: forall a. Read a => ReadPrec (ViewR1 a)
readPrec :: ReadPrec (ViewR1 a)
$creadListPrec :: forall a. Read a => ReadPrec [ViewR1 a]
readListPrec :: ReadPrec [ViewR1 a]
Read,ViewR1 a -> ViewR1 a -> Bool
(ViewR1 a -> ViewR1 a -> Bool)
-> (ViewR1 a -> ViewR1 a -> Bool) -> Eq (ViewR1 a)
forall a. Eq a => ViewR1 a -> ViewR1 a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Eq a => ViewR1 a -> ViewR1 a -> Bool
== :: ViewR1 a -> ViewR1 a -> Bool
$c/= :: forall a. Eq a => ViewR1 a -> ViewR1 a -> Bool
/= :: ViewR1 a -> ViewR1 a -> Bool
Eq,Eq (ViewR1 a)
Eq (ViewR1 a) =>
(ViewR1 a -> ViewR1 a -> Ordering)
-> (ViewR1 a -> ViewR1 a -> Bool)
-> (ViewR1 a -> ViewR1 a -> Bool)
-> (ViewR1 a -> ViewR1 a -> Bool)
-> (ViewR1 a -> ViewR1 a -> Bool)
-> (ViewR1 a -> ViewR1 a -> ViewR1 a)
-> (ViewR1 a -> ViewR1 a -> ViewR1 a)
-> Ord (ViewR1 a)
ViewR1 a -> ViewR1 a -> Bool
ViewR1 a -> ViewR1 a -> Ordering
ViewR1 a -> ViewR1 a -> ViewR1 a
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall a. Ord a => Eq (ViewR1 a)
forall a. Ord a => ViewR1 a -> ViewR1 a -> Bool
forall a. Ord a => ViewR1 a -> ViewR1 a -> Ordering
forall a. Ord a => ViewR1 a -> ViewR1 a -> ViewR1 a
$ccompare :: forall a. Ord a => ViewR1 a -> ViewR1 a -> Ordering
compare :: ViewR1 a -> ViewR1 a -> Ordering
$c< :: forall a. Ord a => ViewR1 a -> ViewR1 a -> Bool
< :: ViewR1 a -> ViewR1 a -> Bool
$c<= :: forall a. Ord a => ViewR1 a -> ViewR1 a -> Bool
<= :: ViewR1 a -> ViewR1 a -> Bool
$c> :: forall a. Ord a => ViewR1 a -> ViewR1 a -> Bool
> :: ViewR1 a -> ViewR1 a -> Bool
$c>= :: forall a. Ord a => ViewR1 a -> ViewR1 a -> Bool
>= :: ViewR1 a -> ViewR1 a -> Bool
$cmax :: forall a. Ord a => ViewR1 a -> ViewR1 a -> ViewR1 a
max :: ViewR1 a -> ViewR1 a -> ViewR1 a
$cmin :: forall a. Ord a => ViewR1 a -> ViewR1 a -> ViewR1 a
min :: ViewR1 a -> ViewR1 a -> ViewR1 a
Ord,(forall a b. (a -> b) -> ViewR1 a -> ViewR1 b)
-> (forall a b. a -> ViewR1 b -> ViewR1 a) -> Functor ViewR1
forall a b. a -> ViewR1 b -> ViewR1 a
forall a b. (a -> b) -> ViewR1 a -> ViewR1 b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> ViewR1 a -> ViewR1 b
fmap :: forall a b. (a -> b) -> ViewR1 a -> ViewR1 b
$c<$ :: forall a b. a -> ViewR1 b -> ViewR1 a
<$ :: forall a b. a -> ViewR1 b -> ViewR1 a
Functor,Functor ViewR1
Foldable ViewR1
(Functor ViewR1, Foldable ViewR1) =>
(forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> ViewR1 a -> f (ViewR1 b))
-> (forall (f :: * -> *) a.
Applicative f =>
ViewR1 (f a) -> f (ViewR1 a))
-> (forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> ViewR1 a -> m (ViewR1 b))
-> (forall (m :: * -> *) a.
Monad m =>
ViewR1 (m a) -> m (ViewR1 a))
-> Traversable ViewR1
forall (t :: * -> *).
(Functor t, Foldable t) =>
(forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a. Monad m => ViewR1 (m a) -> m (ViewR1 a)
forall (f :: * -> *) a.
Applicative f =>
ViewR1 (f a) -> f (ViewR1 a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> ViewR1 a -> m (ViewR1 b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> ViewR1 a -> f (ViewR1 b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> ViewR1 a -> f (ViewR1 b)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> ViewR1 a -> f (ViewR1 b)
$csequenceA :: forall (f :: * -> *) a.
Applicative f =>
ViewR1 (f a) -> f (ViewR1 a)
sequenceA :: forall (f :: * -> *) a.
Applicative f =>
ViewR1 (f a) -> f (ViewR1 a)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> ViewR1 a -> m (ViewR1 b)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> ViewR1 a -> m (ViewR1 b)
$csequence :: forall (m :: * -> *) a. Monad m => ViewR1 (m a) -> m (ViewR1 a)
sequence :: forall (m :: * -> *) a. Monad m => ViewR1 (m a) -> m (ViewR1 a)
Traversable,(forall x. ViewR1 a -> Rep (ViewR1 a) x)
-> (forall x. Rep (ViewR1 a) x -> ViewR1 a) -> Generic (ViewR1 a)
forall x. Rep (ViewR1 a) x -> ViewR1 a
forall x. ViewR1 a -> Rep (ViewR1 a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (ViewR1 a) x -> ViewR1 a
forall a x. ViewR1 a -> Rep (ViewR1 a) x
$cfrom :: forall a x. ViewR1 a -> Rep (ViewR1 a) x
from :: forall x. ViewR1 a -> Rep (ViewR1 a) x
$cto :: forall a x. Rep (ViewR1 a) x -> ViewR1 a
to :: forall x. Rep (ViewR1 a) x -> ViewR1 a
Generic)
instance NFData a => NFData (ViewR1 a)
type instance Index (ViewR1 a) = Int
type instance IxValue (ViewR1 a) = a
instance Ixed (ViewR1 a) where
ix :: Index (ViewR1 a) -> Traversal' (ViewR1 a) (IxValue (ViewR1 a))
ix Index (ViewR1 a)
i IxValue (ViewR1 a) -> f (IxValue (ViewR1 a))
f (Seq a
xs :>> a
x)
| Int
Index (ViewR1 a)
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Seq a -> Int
forall a. Seq a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length Seq a
xs = (Seq a
xs Seq a -> a -> ViewR1 a
forall a. Seq a -> a -> ViewR1 a
:>>) (a -> ViewR1 a) -> f a -> f (ViewR1 a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IxValue (ViewR1 a) -> f (IxValue (ViewR1 a))
f a
IxValue (ViewR1 a)
x
| Bool
otherwise = (Seq a -> a -> ViewR1 a
forall a. Seq a -> a -> ViewR1 a
:>> a
x) (Seq a -> ViewR1 a) -> f (Seq a) -> f (ViewR1 a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Index (Seq a) -> Traversal' (Seq a) (IxValue (Seq a))
forall m. Ixed m => Index m -> Traversal' m (IxValue m)
ix Index (Seq a)
Index (ViewR1 a)
i IxValue (Seq a) -> f (IxValue (Seq a))
IxValue (ViewR1 a) -> f (IxValue (ViewR1 a))
f Seq a
xs
{-# INLINE ix #-}
instance Foldable ViewR1 where
foldMap :: forall m a. Monoid m => (a -> m) -> ViewR1 a -> m
foldMap a -> m
f (Seq a
xs :>> a
x) = (a -> m) -> Seq a -> m
forall m a. Monoid m => (a -> m) -> Seq a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap a -> m
f Seq a
xs m -> m -> m
forall a. Semigroup a => a -> a -> a
<> a -> m
f a
x
{-# INLINE foldMap #-}
length :: forall a. ViewR1 a -> Int
length (Seq a
xs :>> a
_) = Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Seq a -> Int
forall a. Seq a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length Seq a
xs
{-# INLINE length #-}
null :: forall a. ViewR1 a -> Bool
null ViewR1 a
_ = Bool
False
{-# INLINE null #-}
instance Foldable1 ViewR1 where
foldMap1 :: forall m a. Semigroup m => (a -> m) -> ViewR1 a -> m
foldMap1 a -> m
f (Seq a
as :>> a
a) = case (a -> Maybe m) -> Seq a -> Maybe m
forall m a. Monoid m => (a -> m) -> Seq a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (m -> Maybe m
forall a. a -> Maybe a
Just (m -> Maybe m) -> (a -> m) -> a -> Maybe m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m
f) Seq a
as of
Maybe m
Nothing -> a -> m
f a
a
Just m
b -> m
b m -> m -> m
forall a. Semigroup a => a -> a -> a
<> a -> m
f a
a
{-# INLINE foldMap1 #-}
instance Traversable1 ViewR1 where
traverse1 :: forall (f :: * -> *) a b.
Apply f =>
(a -> f b) -> ViewR1 a -> f (ViewR1 b)
traverse1 a -> f b
f (Seq a
as :>> a
a) = Seq b -> b -> ViewR1 b
forall a. Seq a -> a -> ViewR1 a
(:>>) (Seq b -> b -> ViewR1 b)
-> MaybeApply f (Seq b) -> MaybeApply f (b -> ViewR1 b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (a -> f b) -> Seq a -> MaybeApply f (Seq b)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Apply f) =>
(a -> f b) -> t a -> MaybeApply f (t b)
traverse1Maybe a -> f b
f Seq a
as MaybeApply f (b -> ViewR1 b) -> f b -> f (ViewR1 b)
forall (f :: * -> *) a b.
Apply f =>
MaybeApply f (a -> b) -> f a -> f b
<*.> a -> f b
f a
a
{-# INLINE traverse1 #-}
instance FunctorWithIndex Int ViewR1
instance FoldableWithIndex Int ViewR1
instance TraversableWithIndex Int ViewR1
instance Foldable1WithIndex Int ViewR1 where
ifoldMap1 :: forall m a. Semigroup m => (Int -> a -> m) -> ViewR1 a -> m
ifoldMap1 Int -> a -> m
f (Seq a
as :>> a
a) = case (Int -> a -> Maybe m) -> Seq a -> Maybe m
forall m a. Monoid m => (Int -> a -> m) -> Seq a -> m
forall i (f :: * -> *) m a.
(FoldableWithIndex i f, Monoid m) =>
(i -> a -> m) -> f a -> m
ifoldMap (\Int
i -> m -> Maybe m
forall a. a -> Maybe a
Just (m -> Maybe m) -> (a -> m) -> a -> Maybe m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> a -> m
f Int
i) Seq a
as of
Maybe m
Nothing -> Int -> a -> m
f Int
0 a
a
Just m
b -> m
b m -> m -> m
forall a. Semigroup a => a -> a -> a
<> Int -> a -> m
f (Seq a -> Int
forall a. Seq a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length Seq a
as) a
a
{-# INLINE ifoldMap1 #-}
instance Semigroup (ViewR1 a) where
(Seq a
as :>> a
a) <> :: ViewR1 a -> ViewR1 a -> ViewR1 a
<> (Seq a
bs :>> a
b) = ((Seq a
as Seq a -> a -> Seq a
forall a. Seq a -> a -> Seq a
:|> a
a) Seq a -> Seq a -> Seq a
forall a. Semigroup a => a -> a -> a
<> Seq a
bs) Seq a -> a -> ViewR1 a
forall a. Seq a -> a -> ViewR1 a
:>> a
b
{-# INLINE (<>) #-}
instance HasFromFoldable1 ViewR1 where
fromNonEmpty :: forall a. NonEmpty a -> ViewR1 a
fromNonEmpty (a
a :| [a]
as) = case NonEmpty a -> ViewR1 a
forall a. NonEmpty a -> ViewR1 a
forall (f :: * -> *) a. HasFromFoldable1 f => NonEmpty a -> f a
fromNonEmpty (NonEmpty a -> ViewR1 a) -> Maybe (NonEmpty a) -> Maybe (ViewR1 a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [a] -> Maybe (NonEmpty a)
forall a. [a] -> Maybe (NonEmpty a)
NonEmpty.nonEmpty [a]
as of
Maybe (ViewR1 a)
Nothing -> Seq a
forall a. Seq a
Sequence.empty Seq a -> a -> ViewR1 a
forall a. Seq a -> a -> ViewR1 a
:>> a
a
Just (Seq a
as' :>> a
x) -> (a
a a -> Seq a -> Seq a
forall a. a -> Seq a -> Seq a
:<| Seq a
as') Seq a -> a -> ViewR1 a
forall a. Seq a -> a -> ViewR1 a
:>> a
x
{-# INLINE fromNonEmpty #-}
instance IsList (ViewR1 a) where
type Item (ViewR1 a) = a
toList :: ViewR1 a -> [Item (ViewR1 a)]
toList = ViewR1 a -> [a]
ViewR1 a -> [Item (ViewR1 a)]
forall a. ViewR1 a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
F.toList
{-# INLINE toList #-}
fromList :: [Item (ViewR1 a)] -> ViewR1 a
fromList = ViewR1 a
-> (NonEmpty a -> ViewR1 a) -> Maybe (NonEmpty a) -> ViewR1 a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (String -> ViewR1 a
forall a. HasCallStack => String -> a
error String
"ViewR1 fromList; empty List") NonEmpty a -> ViewR1 a
forall a. NonEmpty a -> ViewR1 a
forall (f :: * -> *) a. HasFromFoldable1 f => NonEmpty a -> f a
fromNonEmpty (Maybe (NonEmpty a) -> ViewR1 a)
-> ([a] -> Maybe (NonEmpty a)) -> [a] -> ViewR1 a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> Maybe (NonEmpty a)
forall a. [a] -> Maybe (NonEmpty a)
NonEmpty.nonEmpty
{-# INLINE fromList #-}
instance Reversing (ViewR1 a) where
reversing :: ViewR1 a -> ViewR1 a
reversing (Seq a
s :>> a
x) = ViewL1 a -> ViewR1 a
forall a. ViewL1 a -> ViewR1 a
viewr1 (ViewL1 a -> ViewR1 a) -> ViewL1 a -> ViewR1 a
forall a b. (a -> b) -> a -> b
$ a
x a -> Seq a -> ViewL1 a
forall a. a -> Seq a -> ViewL1 a
:<< Seq a -> Seq a
forall a. Seq a -> Seq a
Sequence.reverse Seq a
s
(|>>) :: ViewR1 a -> a -> ViewR1 a
(Seq a
ys :>> a
y) |>> :: forall a. ViewR1 a -> a -> ViewR1 a
|>> a
x = (Seq a
ys Seq a -> a -> Seq a
forall a. Seq a -> a -> Seq a
:|> a
y) Seq a -> a -> ViewR1 a
forall a. Seq a -> a -> ViewR1 a
:>> a
x
infixl 5 |>>
infixl 5 <>>
(<>>) :: ViewR1 a -> Seq a -> ViewR1 a
l :: ViewR1 a
l@(Seq a
ys :>> a
y) <>> :: forall a. ViewR1 a -> Seq a -> ViewR1 a
<>> Seq a
rs = case Seq a -> ViewR a
forall a. Seq a -> ViewR a
Sequence.viewr Seq a
rs of
ViewR a
Sequence.EmptyR -> ViewR1 a
l
Seq a
rs' Sequence.:> a
r -> (Seq a
ys Seq a -> Seq a -> Seq a
forall a. Semigroup a => a -> a -> a
<> (a
y a -> Seq a -> Seq a
forall a. a -> Seq a -> Seq a
:<| Seq a
rs')) Seq a -> a -> ViewR1 a
forall a. Seq a -> a -> ViewR1 a
:>> a
r
viewl1 :: ViewR1 a -> ViewL1 a
viewl1 :: forall a. ViewR1 a -> ViewL1 a
viewl1 (Seq a
xs :>> a
r) = case Seq a -> ViewL a
forall a. Seq a -> ViewL a
Sequence.viewl Seq a
xs of
ViewL a
Sequence.EmptyL -> a
r a -> Seq a -> ViewL1 a
forall a. a -> Seq a -> ViewL1 a
:<< Seq a
forall a. Seq a
Sequence.empty
a
l Sequence.:< Seq a
mid -> a
l a -> Seq a -> ViewL1 a
forall a. a -> Seq a -> ViewL1 a
:<< (Seq a
mid Seq a -> a -> Seq a
forall a. Seq a -> a -> Seq a
:|> a
r)
viewr1 :: ViewL1 a -> ViewR1 a
viewr1 :: forall a. ViewL1 a -> ViewR1 a
viewr1 (a
l :<< Seq a
ls) = case Seq a -> ViewR a
forall a. Seq a -> ViewR a
Sequence.viewr Seq a
ls of
ViewR a
Sequence.EmptyR -> Seq a
forall a. Seq a
Sequence.empty Seq a -> a -> ViewR1 a
forall a. Seq a -> a -> ViewR1 a
:>> a
l
Seq a
mid Sequence.:> a
r -> (a
l a -> Seq a -> Seq a
forall a. a -> Seq a -> Seq a
:<| Seq a
mid) Seq a -> a -> ViewR1 a
forall a. Seq a -> a -> ViewR1 a
:>> a
r
splitL1At :: Int -> ViewL1 a -> Maybe (Seq a, a, Seq a)
splitL1At :: forall a. Int -> ViewL1 a -> Maybe (Seq a, a, Seq a)
splitL1At Int
i (a
x :<< Seq a
s) = Int -> Seq a -> Maybe (Seq a)
forall (t :: * -> *) a. Foldable t => Int -> t a -> Maybe (t a)
clampRange Int
i (a
x a -> Seq a -> Seq a
forall s a. Cons s s a a => a -> s -> s
<| Seq a
s) Maybe (Seq a)
-> (Seq a -> (Seq a, a, Seq a)) -> Maybe (Seq a, a, Seq a)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \Seq a
s' -> case Int -> Seq a -> (Seq a, Seq a)
forall a. Int -> Seq a -> (Seq a, Seq a)
Sequence.splitAt Int
i Seq a
s' of
(Seq a
pref, a
y :<| Seq a
suff) -> (Seq a
pref, a
y, Seq a
suff)
(Seq a, Seq a)
_ -> String -> (Seq a, a, Seq a)
forall a. HasCallStack => String -> a
error String
"splitL1At: absurd"
splitR1At :: Int -> ViewR1 a -> Maybe (Seq a, a, Seq a)
splitR1At :: forall a. Int -> ViewR1 a -> Maybe (Seq a, a, Seq a)
splitR1At Int
i (Seq a
s :>> a
x) = Int -> Seq a -> Maybe (Seq a)
forall (t :: * -> *) a. Foldable t => Int -> t a -> Maybe (t a)
clampRange Int
i (Seq a
s Seq a -> a -> Seq a
forall s a. Snoc s s a a => s -> a -> s
|> a
x) Maybe (Seq a)
-> (Seq a -> (Seq a, a, Seq a)) -> Maybe (Seq a, a, Seq a)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \Seq a
s' -> case Int -> Seq a -> (Seq a, Seq a)
forall a. Int -> Seq a -> (Seq a, Seq a)
Sequence.splitAt Int
i Seq a
s' of
(Seq a
pref, a
y :<| Seq a
suff) -> (Seq a
pref, a
y, Seq a
suff)
(Seq a, Seq a)
_ -> String -> (Seq a, a, Seq a)
forall a. HasCallStack => String -> a
error String
"splitR1At: absurd"
clampRange :: Foldable t => Int -> t a -> Maybe (t a)
clampRange :: forall (t :: * -> *) a. Foldable t => Int -> t a -> Maybe (t a)
clampRange Int
i t a
s
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 = Maybe (t a)
forall a. Maybe a
Nothing
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= t a -> Int
forall a. t a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
F.length t a
s = Maybe (t a)
forall a. Maybe a
Nothing
| Bool
otherwise = t a -> Maybe (t a)
forall a. a -> Maybe a
Just t a
s