{-# LANGUAGE CPP #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiParamTypeClasses #-}
#ifdef TRUSTWORTHY
{-# LANGUAGE Trustworthy #-}
#endif
module Data.Vector.Lens
( toVectorOf
, vector
, forced
, sliced
, ordinals
) where
import Prelude ()
import Control.Lens
import Control.Lens.Internal.List (ordinalNub)
import Control.Lens.Internal.Prelude
import qualified Data.Vector as V
import Data.Vector (Vector)
sliced :: Int
-> Int
-> Lens' (Vector a) (Vector a)
sliced :: forall a. Int -> Int -> Lens' (Vector a) (Vector a)
sliced Int
i Int
n Vector a -> f (Vector a)
f Vector a
v = Vector a -> f (Vector a)
f (forall a. Int -> Int -> Vector a -> Vector a
V.slice Int
i Int
n Vector a
v) forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \ Vector a
v0 -> Vector a
v forall a. Vector a -> [(Int, a)] -> Vector a
V.// forall a b. [a] -> [b] -> [(a, b)]
zip [Int
i..Int
iforall a. Num a => a -> a -> a
+Int
nforall a. Num a => a -> a -> a
-Int
1] (forall a. Vector a -> [a]
V.toList Vector a
v0)
{-# INLINE sliced #-}
toVectorOf :: Getting (Endo [a]) s a -> s -> Vector a
toVectorOf :: forall a s. Getting (Endo [a]) s a -> s -> Vector a
toVectorOf Getting (Endo [a]) s a
l s
s = forall a. [a] -> Vector a
V.fromList (forall a s. Getting (Endo [a]) s a -> s -> [a]
toListOf Getting (Endo [a]) s a
l s
s)
{-# INLINE toVectorOf #-}
vector :: Iso [a] [b] (Vector a) (Vector b)
vector :: forall a b. Iso [a] [b] (Vector a) (Vector b)
vector = forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso forall a. [a] -> Vector a
V.fromList forall a. Vector a -> [a]
V.toList
{-# INLINE vector #-}
forced :: Iso (Vector a) (Vector b) (Vector a) (Vector b)
forced :: forall a b. Iso (Vector a) (Vector b) (Vector a) (Vector b)
forced = forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso forall a. Vector a -> Vector a
V.force forall a. Vector a -> Vector a
V.force
{-# INLINE forced #-}
ordinals :: [Int] -> IndexedTraversal' Int (Vector a) a
ordinals :: forall a. [Int] -> IndexedTraversal' Int (Vector a) a
ordinals [Int]
is p a (f a)
f Vector a
v = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Vector a
v forall a. Vector a -> [(Int, a)] -> Vector a
V.//) forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse (\Int
i -> (,) Int
i forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall i (p :: * -> * -> *) a b.
Indexable i p =>
p a b -> i -> a -> b
indexed p a (f a)
f Int
i (Vector a
v forall a. Vector a -> Int -> a
V.! Int
i)) forall a b. (a -> b) -> a -> b
$ Int -> [Int] -> [Int]
ordinalNub (forall (t :: * -> *) a. Foldable t => t a -> Int
length Vector a
v) [Int]
is
{-# INLINE ordinals #-}