module Data.Vector.Generic.Optics
( toVectorOf
, forced
, vector
, asStream
, asStreamR
, cloned
, converted
, sliced
, ordinals
, vectorIx
, vectorTraverse
) where
import Data.Vector.Fusion.Bundle (Bundle)
import Data.Vector.Generic as V hiding (zip, filter, indexed)
import Data.Vector.Generic.New (New)
import Prelude hiding ((++), length, null, head, tail, init, last, map, reverse)
import Optics.Core
import Optics.Extra.Internal.Vector
import Optics.Internal.Fold
import Optics.Internal.IxFold
import Optics.Internal.Profunctor
import Optics.Internal.Optic
sliced
:: Vector v a
=> Int
-> Int
-> Lens' (v a) (v a)
sliced i n = lensVL $ \f v ->
(\v0 -> v // zip [i..i+n-1] (V.toList v0)) <$> f (slice i n v)
{-# INLINE sliced #-}
toVectorOf
:: (Is k A_Fold, Vector v a)
=> Optic' k is s a
-> s
-> v a
toVectorOf l s = fromList (toListOf l s)
{-# INLINE toVectorOf #-}
vector
:: (Vector v a, Vector v b)
=> Iso [a] [b] (v a) (v b)
vector = iso fromList V.toList
{-# INLINE vector #-}
asStream
:: (Vector v a, Vector v b)
=> Iso (v a) (v b) (Bundle v a) (Bundle v b)
asStream = iso stream unstream
{-# INLINE asStream #-}
asStreamR
:: (Vector v a, Vector v b)
=> Iso (v a) (v b) (Bundle v a) (Bundle v b)
asStreamR = iso streamR unstreamR
{-# INLINE asStreamR #-}
cloned :: Vector v a => Iso' (v a) (New v a)
cloned = iso clone new
{-# INLINE cloned #-}
forced :: (Vector v a, Vector v b) => Iso (v a) (v b) (v a) (v b)
forced = iso force force
{-# INLINE forced #-}
ordinals :: forall v a. Vector v a => [Int] -> IxTraversal' Int (v a) a
ordinals is = itraversalVL $ \f v ->
(v //) <$> traverse (\i -> (,) i <$> f i (v ! i)) (ordinalNub (length v) is)
{-# INLINE ordinals #-}
vectorIx :: V.Vector v a => Int -> Traversal' (v a) a
vectorIx i = traversalVL $ \f v ->
if 0 <= i && i < V.length v
then (\a -> v V.// [(i, a)]) <$> f (v V.! i)
else pure v
{-# INLINE vectorIx #-}
vectorTraverse
:: forall v w a b. (V.Vector v a, V.Vector w b)
=> IxTraversal Int (v a) (w b) a b
vectorTraverse = Optic vectorTraverse__
{-# INLINE vectorTraverse #-}
converted
:: (Vector v a, Vector w a, Vector v b, Vector w b)
=> Iso (v a) (v b) (w a) (w b)
converted = iso convert convert
{-# INLINE converted #-}
vectorTraverse__
:: (Traversing p, V.Vector v a, V.Vector w b)
=> Optic__ p j (Int -> j) (v a) (w b) a b
vectorTraverse__ = conjoined__ vectorTraverseNoIx__ vectorTraverseIx__
{-# INLINE [0] vectorTraverse__ #-}
vectorTraverseNoIx__
:: (Traversing p, V.Vector v a, V.Vector w b)
=> Optic__ p j j (v a) (w b) a b
vectorTraverseNoIx__ = wander $ \f v ->
let !n = V.length v in V.fromListN n <$> traverse f (V.toList v)
{-# INLINE vectorTraverseNoIx__ #-}
vectorTraverseIx__
:: (Traversing p, V.Vector v a, V.Vector w b)
=> Optic__ p j (Int -> j) (v a) (w b) a b
vectorTraverseIx__ = iwander $ \f v ->
let !n = V.length v in V.fromListN n <$> itraverse f (V.toList v)
{-# INLINE vectorTraverseIx__ #-}
{-# RULES
"vectorTraverse__ -> traversed"
forall (o :: Star g j a b). vectorTraverse__ o = vectorTraverseNoIx__ (reStar o)
:: (V.Vector v a, V.Vector w b) => Star g (Int -> j) (v a) (w b)
"vectorTraverse__ -> folded"
forall (o :: Forget r j a b). vectorTraverse__ o = foldring__ V.foldr (reForget o)
:: (V.Vector v a, V.Vector v b) => Forget r (Int -> j) (v a) (v b)
"vectorTraverse__ -> mapped"
forall (o :: FunArrow j a b). vectorTraverse__ o = roam V.map (reFunArrow o)
:: (V.Vector v a, V.Vector v b) => FunArrow (Int -> j) (v a) (v b)
"vectorTraverse__ -> itraversed"
forall (o :: IxStar g j a b). vectorTraverse__ o = vectorTraverseIx__ o
:: (V.Vector v a, V.Vector w b) => IxStar g (Int -> j) (v a) (w b)
"vectorTraverse__ -> ifolded"
forall (o :: IxForget r j a b). vectorTraverse__ o = ifoldring__ V.ifoldr o
:: (V.Vector v a, V.Vector v b) => IxForget r (Int -> j) (v a) (v b)
"vectorTraverse__ -> imapped"
forall (o :: IxFunArrow j a b). vectorTraverse__ o = iroam V.imap o
:: (V.Vector v a, V.Vector v b) => IxFunArrow (Int -> j) (v a) (v b)
#-}