module Data.Sequence.Optics
( viewL, viewR
, sliced, slicedTo, slicedFrom
, seqOf
) where
import Data.Sequence (Seq, ViewL (..), ViewR (..), (><))
import qualified Data.Sequence as Seq
import Optics.Internal.Indexed
import Optics.Fold
import Optics.Iso
import Optics.IxTraversal
import Optics.Optic
import Optics.Traversal
viewL :: Iso (Seq a) (Seq b) (ViewL a) (ViewL b)
viewL :: forall a b. Iso (Seq a) (Seq b) (ViewL a) (ViewL b)
viewL = forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso forall a. Seq a -> ViewL a
Seq.viewl forall a b. (a -> b) -> a -> b
$ \ViewL b
xs -> case ViewL b
xs of
ViewL b
EmptyL -> forall a. Monoid a => a
mempty
b
a Seq.:< Seq b
as -> b
a forall a. a -> Seq a -> Seq a
Seq.<| Seq b
as
{-# INLINE viewL #-}
viewR :: Iso (Seq a) (Seq b) (ViewR a) (ViewR b)
viewR :: forall a b. Iso (Seq a) (Seq b) (ViewR a) (ViewR b)
viewR = forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso forall a. Seq a -> ViewR a
Seq.viewr forall a b. (a -> b) -> a -> b
$ \ViewR b
xs -> case ViewR b
xs of
ViewR b
EmptyR -> forall a. Monoid a => a
mempty
Seq b
as Seq.:> b
a -> Seq b
as forall a. Seq a -> a -> Seq a
Seq.|> b
a
{-# INLINE viewR #-}
slicedTo :: Int -> IxTraversal' Int (Seq a) a
slicedTo :: forall a. Int -> IxTraversal' Int (Seq a) a
slicedTo Int
n = forall (is :: IxList) i k s t a b.
HasSingleIndex is i =>
Optic k '[] s t a b -> Optic k is s t a b -> Optic k is s t a b
conjoined Traversal (Seq a) (Seq a) a a
noix IxTraversal Int (Seq a) (Seq a) a a
ix
where
noix :: Traversal (Seq a) (Seq a) a a
noix = forall s t a b. TraversalVL s t a b -> Traversal s t a b
traversalVL forall a b. (a -> b) -> a -> b
$ \a -> f a
f Seq a
m -> case forall a. Int -> Seq a -> (Seq a, Seq a)
Seq.splitAt Int
n Seq a
m of
(Seq a
l, Seq a
r) -> (forall a. Seq a -> Seq a -> Seq a
>< Seq a
r) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse a -> f a
f Seq a
l
ix :: IxTraversal Int (Seq a) (Seq a) a a
ix = forall i s t a b. IxTraversalVL i s t a b -> IxTraversal i s t a b
itraversalVL forall a b. (a -> b) -> a -> b
$ \Int -> a -> f a
f Seq a
m -> case forall a. Int -> Seq a -> (Seq a, Seq a)
Seq.splitAt Int
n Seq a
m of
(Seq a
l, Seq a
r) -> (forall a. Seq a -> Seq a -> Seq a
>< Seq a
r) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall i (t :: * -> *) (f :: * -> *) a b.
(TraversableWithIndex i t, Applicative f) =>
(i -> a -> f b) -> t a -> f (t b)
itraverse Int -> a -> f a
f Seq a
l
{-# INLINE slicedTo #-}
slicedFrom :: Int -> IxTraversal' Int (Seq a) a
slicedFrom :: forall a. Int -> IxTraversal' Int (Seq a) a
slicedFrom Int
n = forall (is :: IxList) i k s t a b.
HasSingleIndex is i =>
Optic k '[] s t a b -> Optic k is s t a b -> Optic k is s t a b
conjoined Traversal (Seq a) (Seq a) a a
noix IxTraversal Int (Seq a) (Seq a) a a
ix
where
noix :: Traversal (Seq a) (Seq a) a a
noix = forall s t a b. TraversalVL s t a b -> Traversal s t a b
traversalVL forall a b. (a -> b) -> a -> b
$ \a -> f a
f Seq a
m -> case forall a. Int -> Seq a -> (Seq a, Seq a)
Seq.splitAt Int
n Seq a
m of
(Seq a
l, Seq a
r) -> (Seq a
l forall a. Seq a -> Seq a -> Seq a
><) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse a -> f a
f Seq a
r
ix :: IxTraversal Int (Seq a) (Seq a) a a
ix = forall i s t a b. IxTraversalVL i s t a b -> IxTraversal i s t a b
itraversalVL forall a b. (a -> b) -> a -> b
$ \Int -> a -> f a
f Seq a
m -> case forall a. Int -> Seq a -> (Seq a, Seq a)
Seq.splitAt Int
n Seq a
m of
(Seq a
l, Seq a
r) -> (Seq a
l forall a. Seq a -> Seq a -> Seq a
><) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall i (t :: * -> *) (f :: * -> *) a b.
(TraversableWithIndex i t, Applicative f) =>
(i -> a -> f b) -> t a -> f (t b)
itraverse (Int -> a -> f a
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. Num a => a -> a -> a
+Int
n)) Seq a
r
{-# INLINE slicedFrom #-}
sliced :: Int -> Int -> IxTraversal' Int (Seq a) a
sliced :: forall a. Int -> Int -> IxTraversal' Int (Seq a) a
sliced Int
i Int
j = forall (is :: IxList) i k s t a b.
HasSingleIndex is i =>
Optic k '[] s t a b -> Optic k is s t a b -> Optic k is s t a b
conjoined Traversal (Seq a) (Seq a) a a
noix IxTraversal Int (Seq a) (Seq a) a a
ix
where
noix :: Traversal (Seq a) (Seq a) a a
noix = forall s t a b. TraversalVL s t a b -> Traversal s t a b
traversalVL forall a b. (a -> b) -> a -> b
$ \a -> f a
f Seq a
s -> case forall a. Int -> Seq a -> (Seq a, Seq a)
Seq.splitAt Int
i Seq a
s of
(Seq a
l, Seq a
mr) -> case forall a. Int -> Seq a -> (Seq a, Seq a)
Seq.splitAt (Int
jforall a. Num a => a -> a -> a
-Int
i) Seq a
mr of
(Seq a
m, Seq a
r) -> forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse a -> f a
f Seq a
m forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \Seq a
n -> Seq a
l forall a. Seq a -> Seq a -> Seq a
>< Seq a
n forall a. Seq a -> Seq a -> Seq a
>< Seq a
r
ix :: IxTraversal Int (Seq a) (Seq a) a a
ix = forall i s t a b. IxTraversalVL i s t a b -> IxTraversal i s t a b
itraversalVL forall a b. (a -> b) -> a -> b
$ \Int -> a -> f a
f Seq a
s -> case forall a. Int -> Seq a -> (Seq a, Seq a)
Seq.splitAt Int
i Seq a
s of
(Seq a
l, Seq a
mr) -> case forall a. Int -> Seq a -> (Seq a, Seq a)
Seq.splitAt (Int
jforall a. Num a => a -> a -> a
-Int
i) Seq a
mr of
(Seq a
m, Seq a
r) -> forall i (t :: * -> *) (f :: * -> *) a b.
(TraversableWithIndex i t, Applicative f) =>
(i -> a -> f b) -> t a -> f (t b)
itraverse (Int -> a -> f a
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. Num a => a -> a -> a
+Int
i)) Seq a
m forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \Seq a
n -> Seq a
l forall a. Seq a -> Seq a -> Seq a
>< Seq a
n forall a. Seq a -> Seq a -> Seq a
>< Seq a
r
{-# INLINE sliced #-}
seqOf :: Is k A_Fold => Optic' k is s a -> s -> Seq a
seqOf :: forall k (is :: IxList) s a.
Is k A_Fold =>
Optic' k is s a -> s -> Seq a
seqOf Optic' k is s a
l = forall k m (is :: IxList) s a.
(Is k A_Fold, Monoid m) =>
Optic' k is s a -> (a -> m) -> s -> m
foldMapOf Optic' k is s a
l forall a. a -> Seq a
Seq.singleton
{-# INLINE seqOf #-}