{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE FlexibleContexts #-}
module Data.Sequence.Lens
( viewL, viewR
, sliced, slicedTo, slicedFrom
, seqOf
) where
import Control.Applicative
import Control.Lens
import Data.Monoid
import Data.Sequence as Seq
viewL :: Iso (Seq a) (Seq b) (ViewL a) (ViewL b)
viewL = iso viewl $ \ xs -> case xs of
EmptyL -> mempty
a :< as -> a Seq.<| as
{-# INLINE viewL #-}
viewR :: Iso (Seq a) (Seq b) (ViewR a) (ViewR b)
viewR = iso viewr $ \xs -> case xs of
EmptyR -> mempty
as :> a -> as Seq.|> a
{-# INLINE viewR #-}
slicedTo :: Int -> IndexedTraversal' Int (Seq a) a
slicedTo n f m = case Seq.splitAt n m of
(l,r) -> (>< r) <$> itraverse (indexed f) l
{-# INLINE slicedTo #-}
slicedFrom :: Int -> IndexedTraversal' Int (Seq a) a
slicedFrom n f m = case Seq.splitAt n m of
(l,r) -> (l ><) <$> itraverse (indexed f . (+n)) r
{-# INLINE slicedFrom #-}
sliced :: Int -> Int -> IndexedTraversal' Int (Seq a) a
sliced i j f s = case Seq.splitAt i s of
(l,mr) -> case Seq.splitAt (j-i) mr of
(m, r) -> itraverse (indexed f . (+i)) m <&> \n -> l >< n >< r
{-# INLINE sliced #-}
seqOf :: Getting (Seq a) s a -> s -> Seq a
seqOf l = views l Seq.singleton
{-# INLINE seqOf #-}