Safe Haskell | None |
---|---|
Language | Haskell2010 |
This module re-exports the functionality in
Sized
specialized to
Primitive
.
Functions returning a vector determine the size from the type context unless
they have a
'
suffix in which case they take an explicit
Proxy
argument.
Functions where the resulting vector size is not known until runtime are not exported.
Synopsis
- type Vector = Vector Vector
- pattern SomeSized :: Prim a => KnownNat n => Vector n a -> Vector a
- type MVector = MVector MVector
- length :: forall n a. KnownNat n => Vector n a -> Int
- length' :: forall n a. Vector n a -> Proxy n
- knownLength :: forall n a r. Prim a => Vector n a -> ( KnownNat n => r) -> r
- knownLength' :: forall n a r. Prim a => Vector n a -> ( KnownNat n => Proxy n -> r) -> r
- index :: forall n a. Prim a => Vector n a -> Finite n -> a
- index' :: forall n m a p. ( KnownNat n, Prim a) => Vector ((n + m) + 1) a -> p n -> a
- unsafeIndex :: forall n a. Prim a => Vector n a -> Int -> a
- head :: forall n a. Prim a => Vector (1 + n) a -> a
- last :: forall n a. Prim a => Vector (n + 1) a -> a
- indexM :: forall n a m. ( Prim a, Monad m) => Vector n a -> Finite n -> m a
- indexM' :: forall n k a m p. ( KnownNat n, Prim a, Monad m) => Vector (n + k) a -> p n -> m a
- unsafeIndexM :: forall n a m. ( Prim a, Monad m) => Vector n a -> Int -> m a
- headM :: forall n a m. ( Prim a, Monad m) => Vector (1 + n) a -> m a
- lastM :: forall n a m. ( Prim a, Monad m) => Vector (n + 1) a -> m a
- slice :: forall i n m a p. ( KnownNat i, KnownNat n, Prim a) => p i -> Vector ((i + n) + m) a -> Vector n a
- slice' :: forall i n m a p. ( KnownNat i, KnownNat n, Prim a) => p i -> p n -> Vector ((i + n) + m) a -> Vector n a
- init :: forall n a. Prim a => Vector (n + 1) a -> Vector n a
- tail :: forall n a. Prim a => Vector (1 + n) a -> Vector n a
- take :: forall n m a. ( KnownNat n, Prim a) => Vector (n + m) a -> Vector n a
- take' :: forall n m a p. ( KnownNat n, Prim a) => p n -> Vector (n + m) a -> Vector n a
- drop :: forall n m a. ( KnownNat n, Prim a) => Vector (n + m) a -> Vector m a
- drop' :: forall n m a p. ( KnownNat n, Prim a) => p n -> Vector (n + m) a -> Vector m a
- splitAt :: forall n m a. ( KnownNat n, Prim a) => Vector (n + m) a -> ( Vector n a, Vector m a)
- splitAt' :: forall n m a p. ( KnownNat n, Prim a) => p n -> Vector (n + m) a -> ( Vector n a, Vector m a)
- empty :: forall a. Prim a => Vector 0 a
- singleton :: forall a. Prim a => a -> Vector 1 a
- fromTuple :: forall a input length. ( Prim a, IndexedListLiterals input length a, KnownNat length) => input -> Vector length a
- replicate :: forall n a. ( KnownNat n, Prim a) => a -> Vector n a
- replicate' :: forall n a p. ( KnownNat n, Prim a) => p n -> a -> Vector n a
- generate :: forall n a. ( KnownNat n, Prim a) => ( Finite n -> a) -> Vector n a
- generate' :: forall n a p. ( KnownNat n, Prim a) => p n -> ( Finite n -> a) -> Vector n a
- iterateN :: forall n a. ( KnownNat n, Prim a) => (a -> a) -> a -> Vector n a
- iterateN' :: forall n a p. ( KnownNat n, Prim a) => p n -> (a -> a) -> a -> Vector n a
- replicateM :: forall n m a. ( KnownNat n, Prim a, Monad m) => m a -> m ( Vector n a)
- replicateM' :: forall n m a p. ( KnownNat n, Prim a, Monad m) => p n -> m a -> m ( Vector n a)
- generateM :: forall n m a. ( KnownNat n, Prim a, Monad m) => ( Finite n -> m a) -> m ( Vector n a)
- generateM' :: forall n m a p. ( KnownNat n, Prim a, Monad m) => p n -> ( Finite n -> m a) -> m ( Vector n a)
- unfoldrN :: forall n a b. ( KnownNat n, Prim a) => (b -> (a, b)) -> b -> Vector n a
- unfoldrN' :: forall n a b p. ( KnownNat n, Prim a) => p n -> (b -> (a, b)) -> b -> Vector n a
- enumFromN :: forall n a. ( KnownNat n, Prim a, Num a) => a -> Vector n a
- enumFromN' :: forall n a p. ( KnownNat n, Prim a, Num a) => a -> p n -> Vector n a
- enumFromStepN :: forall n a. ( KnownNat n, Prim a, Num a) => a -> a -> Vector n a
- enumFromStepN' :: forall n a p. ( KnownNat n, Prim a, Num a) => a -> a -> p n -> Vector n a
- cons :: forall n a. Prim a => a -> Vector n a -> Vector (1 + n) a
- snoc :: forall n a. Prim a => Vector n a -> a -> Vector (n + 1) a
- (++) :: forall n m a. Prim a => Vector n a -> Vector m a -> Vector (n + m) a
- force :: Prim a => Vector n a -> Vector n a
- (//) :: Prim a => Vector m a -> [( Finite m, a)] -> Vector m a
- update_ :: Prim a => Vector m a -> Vector n Int -> Vector n a -> Vector m a
- unsafeUpd :: Prim a => Vector m a -> [( Int , a)] -> Vector m a
- unsafeUpdate_ :: Prim a => Vector m a -> Vector n Int -> Vector n a -> Vector m a
- accum :: Prim a => (a -> b -> a) -> Vector m a -> [( Finite m, b)] -> Vector m a
- accumulate_ :: ( Prim a, Prim b) => (a -> b -> a) -> Vector m a -> Vector n Int -> Vector n b -> Vector m a
- unsafeAccum :: Prim a => (a -> b -> a) -> Vector m a -> [( Int , b)] -> Vector m a
- unsafeAccumulate_ :: ( Prim a, Prim b) => (a -> b -> a) -> Vector m a -> Vector n Int -> Vector n b -> Vector m a
- reverse :: Prim a => Vector n a -> Vector n a
- backpermute :: Prim a => Vector m a -> Vector n Int -> Vector n a
- unsafeBackpermute :: Prim a => Vector m a -> Vector n Int -> Vector n a
- ix :: forall n a f. ( Prim a, Functor f) => Finite n -> (a -> f a) -> Vector n a -> f ( Vector n a)
- _head :: forall n a f. ( Prim a, Functor f) => (a -> f a) -> Vector (1 + n) a -> f ( Vector (1 + n) a)
- _last :: forall n a f. ( Prim a, Functor f) => (a -> f a) -> Vector (n + 1) a -> f ( Vector (n + 1) a)
- map :: ( Prim a, Prim b) => (a -> b) -> Vector n a -> Vector n b
- imap :: ( Prim a, Prim b) => ( Finite n -> a -> b) -> Vector n a -> Vector n b
- concatMap :: ( Prim a, Prim b) => (a -> Vector m b) -> Vector n a -> Vector (n * m) b
- mapM :: ( Monad m, Prim a, Prim b) => (a -> m b) -> Vector n a -> m ( Vector n b)
- imapM :: ( Monad m, Prim a, Prim b) => ( Finite n -> a -> m b) -> Vector n a -> m ( Vector n b)
- mapM_ :: ( Monad m, Prim a) => (a -> m b) -> Vector n a -> m ()
- imapM_ :: ( Monad m, Prim a) => ( Finite n -> a -> m b) -> Vector n a -> m ()
- forM :: ( Monad m, Prim a, Prim b) => Vector n a -> (a -> m b) -> m ( Vector n b)
- forM_ :: ( Monad m, Prim a) => Vector n a -> (a -> m b) -> m ()
- zipWith :: ( Prim a, Prim b, Prim c) => (a -> b -> c) -> Vector n a -> Vector n b -> Vector n c
- zipWith3 :: ( Prim a, Prim b, Prim c, Prim d) => (a -> b -> c -> d) -> Vector n a -> Vector n b -> Vector n c -> Vector n d
- zipWith4 :: ( Prim a, Prim b, Prim c, Prim d, Prim e) => (a -> b -> c -> d -> e) -> Vector n a -> Vector n b -> Vector n c -> Vector n d -> Vector n e
- zipWith5 :: ( Prim a, Prim b, Prim c, Prim d, Prim e, Prim f) => (a -> b -> c -> d -> e -> f) -> Vector n a -> Vector n b -> Vector n c -> Vector n d -> Vector n e -> Vector n f
- zipWith6 :: ( Prim a, Prim b, Prim c, Prim d, Prim e, Prim f, Prim g) => (a -> b -> c -> d -> e -> f -> g) -> Vector n a -> Vector n b -> Vector n c -> Vector n d -> Vector n e -> Vector n f -> Vector n g
- izipWith :: ( Prim a, Prim b, Prim c) => ( Finite n -> a -> b -> c) -> Vector n a -> Vector n b -> Vector n c
- izipWith3 :: ( Prim a, Prim b, Prim c, Prim d) => ( Finite n -> a -> b -> c -> d) -> Vector n a -> Vector n b -> Vector n c -> Vector n d
- izipWith4 :: ( Prim a, Prim b, Prim c, Prim d, Prim e) => ( Finite n -> a -> b -> c -> d -> e) -> Vector n a -> Vector n b -> Vector n c -> Vector n d -> Vector n e
- izipWith5 :: ( Prim a, Prim b, Prim c, Prim d, Prim e, Prim f) => ( Finite n -> a -> b -> c -> d -> e -> f) -> Vector n a -> Vector n b -> Vector n c -> Vector n d -> Vector n e -> Vector n f
- izipWith6 :: ( Prim a, Prim b, Prim c, Prim d, Prim e, Prim f, Prim g) => ( Finite n -> a -> b -> c -> d -> e -> f -> g) -> Vector n a -> Vector n b -> Vector n c -> Vector n d -> Vector n e -> Vector n f -> Vector n g
- zipWithM :: ( Monad m, Prim a, Prim b, Prim c) => (a -> b -> m c) -> Vector n a -> Vector n b -> m ( Vector n c)
- izipWithM :: ( Monad m, Prim a, Prim b, Prim c) => ( Finite n -> a -> b -> m c) -> Vector n a -> Vector n b -> m ( Vector n c)
- zipWithM_ :: ( Monad m, Prim a, Prim b) => (a -> b -> m c) -> Vector n a -> Vector n b -> m ()
- izipWithM_ :: ( Monad m, Prim a, Prim b) => ( Finite n -> a -> b -> m c) -> Vector n a -> Vector n b -> m ()
- elem :: ( Prim a, Eq a) => a -> Vector n a -> Bool
- notElem :: ( Prim a, Eq a) => a -> Vector n a -> Bool
- find :: Prim a => (a -> Bool ) -> Vector n a -> Maybe a
- findIndex :: Prim a => (a -> Bool ) -> Vector n a -> Maybe ( Finite n)
- elemIndex :: ( Prim a, Eq a) => a -> Vector n a -> Maybe ( Finite n)
- foldl :: Prim b => (a -> b -> a) -> a -> Vector n b -> a
- foldl1 :: Prim a => (a -> a -> a) -> Vector (1 + n) a -> a
- foldl' :: Prim b => (a -> b -> a) -> a -> Vector n b -> a
- foldl1' :: Prim a => (a -> a -> a) -> Vector (1 + n) a -> a
- foldr :: Prim a => (a -> b -> b) -> b -> Vector n a -> b
- foldr1 :: Prim a => (a -> a -> a) -> Vector (n + 1) a -> a
- foldr' :: Prim a => (a -> b -> b) -> b -> Vector n a -> b
- foldr1' :: Prim a => (a -> a -> a) -> Vector (n + 1) a -> a
- ifoldl :: Prim b => (a -> Finite n -> b -> a) -> a -> Vector n b -> a
- ifoldl' :: Prim b => (a -> Finite n -> b -> a) -> a -> Vector n b -> a
- ifoldr :: Prim a => ( Finite n -> a -> b -> b) -> b -> Vector n a -> b
- ifoldr' :: Prim a => ( Finite n -> a -> b -> b) -> b -> Vector n a -> b
- all :: Prim a => (a -> Bool ) -> Vector n a -> Bool
- any :: Prim a => (a -> Bool ) -> Vector n a -> Bool
- sum :: ( Prim a, Num a) => Vector n a -> a
- product :: ( Prim a, Num a) => Vector n a -> a
- maximum :: ( Prim a, Ord a) => Vector (n + 1) a -> a
- maximumBy :: Prim a => (a -> a -> Ordering ) -> Vector (n + 1) a -> a
- minimum :: ( Prim a, Ord a) => Vector (n + 1) a -> a
- minimumBy :: Prim a => (a -> a -> Ordering ) -> Vector (n + 1) a -> a
- maxIndex :: ( Prim a, Ord a) => Vector (n + 1) a -> Finite (n + 1)
- maxIndexBy :: Prim a => (a -> a -> Ordering ) -> Vector (n + 1) a -> Finite (n + 1)
- minIndex :: ( Prim a, Ord a) => Vector (n + 1) a -> Finite (n + 1)
- minIndexBy :: Prim a => (a -> a -> Ordering ) -> Vector (n + 1) a -> Finite (n + 1)
- foldM :: ( Monad m, Prim b) => (a -> b -> m a) -> a -> Vector n b -> m a
- ifoldM :: ( Monad m, Prim b) => (a -> Finite n -> b -> m a) -> a -> Vector n b -> m a
- fold1M :: ( Monad m, Prim a) => (a -> a -> m a) -> Vector (1 + n) a -> m a
- foldM' :: ( Monad m, Prim b) => (a -> b -> m a) -> a -> Vector n b -> m a
- ifoldM' :: ( Monad m, Prim b) => (a -> Finite n -> b -> m a) -> a -> Vector n b -> m a
- fold1M' :: ( Monad m, Prim a) => (a -> a -> m a) -> Vector (n + 1) a -> m a
- foldM_ :: ( Monad m, Prim b) => (a -> b -> m a) -> a -> Vector n b -> m ()
- ifoldM_ :: ( Monad m, Prim b) => (a -> Finite n -> b -> m a) -> a -> Vector n b -> m ()
- fold1M_ :: ( Monad m, Prim a) => (a -> a -> m a) -> Vector (n + 1) a -> m ()
- foldM'_ :: ( Monad m, Prim b) => (a -> b -> m a) -> a -> Vector n b -> m ()
- ifoldM'_ :: ( Monad m, Prim b) => (a -> Finite n -> b -> m a) -> a -> Vector n b -> m ()
- fold1M'_ :: ( Monad m, Prim a) => (a -> a -> m a) -> Vector (n + 1) a -> m ()
- prescanl :: ( Prim a, Prim b) => (a -> b -> a) -> a -> Vector n b -> Vector n a
- prescanl' :: ( Prim a, Prim b) => (a -> b -> a) -> a -> Vector n b -> Vector n a
- postscanl :: ( Prim a, Prim b) => (a -> b -> a) -> a -> Vector n b -> Vector n a
- postscanl' :: ( Prim a, Prim b) => (a -> b -> a) -> a -> Vector n b -> Vector n a
- scanl :: ( Prim a, Prim b) => (a -> b -> a) -> a -> Vector n b -> Vector (1 + n) a
- scanl' :: ( Prim a, Prim b) => (a -> b -> a) -> a -> Vector n b -> Vector (1 + n) a
- scanl1 :: Prim a => (a -> a -> a) -> Vector (1 + n) a -> Vector (2 + n) a
- scanl1' :: Prim a => (a -> a -> a) -> Vector (1 + n) a -> Vector (2 + n) a
- prescanr :: ( Prim a, Prim b) => (a -> b -> b) -> b -> Vector n a -> Vector n b
- prescanr' :: ( Prim a, Prim b) => (a -> b -> b) -> b -> Vector n a -> Vector n b
- postscanr :: ( Prim a, Prim b) => (a -> b -> b) -> b -> Vector n a -> Vector n b
- postscanr' :: ( Prim a, Prim b) => (a -> b -> b) -> b -> Vector n a -> Vector n b
- scanr :: ( Prim a, Prim b) => (a -> b -> b) -> b -> Vector n a -> Vector (n + 1) b
- scanr' :: ( Prim a, Prim b) => (a -> b -> b) -> b -> Vector n a -> Vector (n + 1) b
- scanr1 :: Prim a => (a -> a -> a) -> Vector (n + 1) a -> Vector (n + 2) a
- scanr1' :: Prim a => (a -> a -> a) -> Vector (n + 1) a -> Vector (n + 2) a
- toList :: Prim a => Vector n a -> [a]
- fromList :: ( Prim a, KnownNat n) => [a] -> Maybe ( Vector n a)
- fromListN :: forall n a. ( Prim a, KnownNat n) => [a] -> Maybe ( Vector n a)
- fromListN' :: forall n a p. ( Prim a, KnownNat n) => p n -> [a] -> Maybe ( Vector n a)
- withSizedList :: forall a r. Prim a => [a] -> ( forall n. KnownNat n => Vector n a -> r) -> r
- freeze :: ( PrimMonad m, Prim a) => MVector n ( PrimState m) a -> m ( Vector n a)
- thaw :: ( PrimMonad m, Prim a) => Vector n a -> m ( MVector n ( PrimState m) a)
- copy :: ( PrimMonad m, Prim a) => MVector n ( PrimState m) a -> Vector n a -> m ()
- unsafeFreeze :: ( PrimMonad m, Prim a) => MVector n ( PrimState m) a -> m ( Vector n a)
- unsafeThaw :: ( PrimMonad m, Prim a) => Vector n a -> m ( MVector n ( PrimState m) a)
- toSized :: forall n a. ( Prim a, KnownNat n) => Vector a -> Maybe ( Vector n a)
- withSized :: forall a r. Prim a => Vector a -> ( forall n. KnownNat n => Vector n a -> r) -> r
- fromSized :: Vector n a -> Vector a
- withVectorUnsafe :: forall a b (n :: Nat ). ( Vector a -> Vector b) -> Vector n a -> Vector n b
Documentation
pattern SomeSized :: Prim a => KnownNat n => Vector n a -> Vector a Source #
Pattern synonym that lets you treat an unsized vector as if it "contained" a sized vector. If you pattern match on an unsized vector, its contents will be the sized vector counterpart.
testFunc :: Unsized.Vector Int -> Int testFunc (SomeSized
v) =sum
(zipWith
(+) v (replicate
1)) -- ^ here, v is `Sized.Vector n Int`, and we have `KnownNat
n`
The
n
type variable will be properly instantiated to whatever the
length of the vector is, and you will also have a
instance available. You can get
KnownNat
n
n
in scope by turning on
ScopedTypeVariables and matching on
.
SomeSized
(v :: Sized.Vector
n Int)
Without this, you would otherwise have to use
withSized
to do the same
thing:
testFunc :: Unsized.Vector Int -> Int testFunc u =withSized
u $ \v ->sum
(zipWith
(+) v (replicate
1))
Remember that the type of final result of your function (the
Int
,
here) must
not
depend on
n
. However, the types of the intermediate
values are allowed to depend on
n
.
This is
especially
useful in do blocks, where you can pattern match on
the unsized results of actions, to use the sized vector in the rest of
the do block. You also get a
constraint for the
remainder of the do block.
KnownNat
n
-- If you had: getAVector :: IO (Unsized.Vector Int) main :: IO () main = do SomeSized v <- getAVector -- v is `Sized.Vector n Int` -- get n in scope SomeSized (v :: Sized.Vector n Int) <- getAVector print v
Remember that the final type of the result of the do block (
()
, here)
must not depend on
n
. However, the
Also useful in ghci, where you can pattern match to get sized vectors from unsized vectors.
ghci> SomeSized v <- pure (myUnsizedVector :: Unsized.Vector Int) -- ^ v is `Sized.Vector n Int`
This enables interactive exploration with sized vectors in ghci, and is useful for using with other libraries and functions that expect sized vectors in an interactive setting.
(Note that as of GHC 8.6, you cannot get the
n
in scope in your ghci
session using ScopedTypeVariables, like you can with do blocks)
You can also use this as a constructor, to take a sized vector and "hide" the size, to produce an unsized vector:
SomeSized :: Sized.Vector n a -> Unsized.Vector a
Accessors
Length information
:: forall n a r. Prim a | |
=> Vector n a |
a vector of some (potentially unknown) length |
-> ( KnownNat n => r) |
a value that depends on knowing the vector's length |
-> r |
the value computed with the length |
O(1)
Reveal a
KnownNat
instance for a vector's length, determined
at runtime.
Indexing
index :: forall n a. Prim a => Vector n a -> Finite n -> a Source #
O(1)
Safe indexing using a
Finite
.
index' :: forall n m a p. ( KnownNat n, Prim a) => Vector ((n + m) + 1) a -> p n -> a Source #
O(1)
Safe indexing using a
Proxy
.
unsafeIndex :: forall n a. Prim a => Vector n a -> Int -> a Source #
O(1)
Indexing using an
Int
without bounds checking.
head :: forall n a. Prim a => Vector (1 + n) a -> a Source #
O(1) Yield the first element of a non-empty vector.
last :: forall n a. Prim a => Vector (n + 1) a -> a Source #
O(1) Yield the last element of a non-empty vector.
Monadic indexing
indexM :: forall n a m. ( Prim a, Monad m) => Vector n a -> Finite n -> m a Source #
O(1)
Safe indexing in a monad. See the documentation for
indexM
for
an explanation of why this is useful.
indexM' :: forall n k a m p. ( KnownNat n, Prim a, Monad m) => Vector (n + k) a -> p n -> m a Source #
headM :: forall n a m. ( Prim a, Monad m) => Vector (1 + n) a -> m a Source #
O(1)
Yield the first element of a non-empty vector in a monad. See the
documentation for
indexM
for an explanation of why this is useful.
lastM :: forall n a m. ( Prim a, Monad m) => Vector (n + 1) a -> m a Source #
O(1)
Yield the last element of a non-empty vector in a monad. See the
documentation for
indexM
for an explanation of why this is useful.
Extracting subvectors (slicing)
:: forall i n m a p. ( KnownNat i, KnownNat n, Prim a) | |
=> p i |
starting index |
-> Vector ((i + n) + m) a | |
-> Vector n a |
O(1) Yield a slice of the vector without copying it with an inferred length argument.
:: forall i n m a p. ( KnownNat i, KnownNat n, Prim a) | |
=> p i |
starting index |
-> p n |
length |
-> Vector ((i + n) + m) a | |
-> Vector n a |
O(1) Yield a slice of the vector without copying it with an explicit length argument.
init :: forall n a. Prim a => Vector (n + 1) a -> Vector n a Source #
O(1) Yield all but the last element of a non-empty vector without copying.
tail :: forall n a. Prim a => Vector (1 + n) a -> Vector n a Source #
O(1) Yield all but the first element of a non-empty vector without copying.
take :: forall n m a. ( KnownNat n, Prim a) => Vector (n + m) a -> Vector n a Source #
O(1)
Yield the first
n
elements. The resulting vector always contains
this many elements. The length of the resulting vector is inferred from the
type.
take' :: forall n m a p. ( KnownNat n, Prim a) => p n -> Vector (n + m) a -> Vector n a Source #
O(1)
Yield the first
n
elements. The resulting vector always contains
this many elements. The length of the resulting vector is given explicitly
as a
Proxy
argument.
drop :: forall n m a. ( KnownNat n, Prim a) => Vector (n + m) a -> Vector m a Source #
O(1)
Yield all but the the first
n
elements. The given vector must
contain at least this many elements. The length of the resulting vector is
inferred from the type.
drop' :: forall n m a p. ( KnownNat n, Prim a) => p n -> Vector (n + m) a -> Vector m a Source #
O(1)
Yield all but the the first
n
elements. The given vector must
contain at least this many elements. The length of the resulting vector is
givel explicitly as a
Proxy
argument.
splitAt :: forall n m a. ( KnownNat n, Prim a) => Vector (n + m) a -> ( Vector n a, Vector m a) Source #
O(1)
Yield the first
n
elements, paired with the rest, without copying.
The lengths of the resulting vectors are inferred from the type.
splitAt' :: forall n m a p. ( KnownNat n, Prim a) => p n -> Vector (n + m) a -> ( Vector n a, Vector m a) Source #
O(1)
Yield the first
n
elements paired with the remainder without
copying. The length of the first resulting vector is passed explicitly as a
Proxy
argument.
Construction
Initialization
fromTuple :: forall a input length. ( Prim a, IndexedListLiterals input length a, KnownNat length) => input -> Vector length a Source #
O(n)
Construct a vector in a type safe manner
fromTuple (1,2) :: Vector 2 Int
fromTuple ("hey", "what's", "going", "on") :: Vector 4 String
replicate :: forall n a. ( KnownNat n, Prim a) => a -> Vector n a Source #
O(n) Construct a vector with the same element in each position where the length is inferred from the type.
replicate' :: forall n a p. ( KnownNat n, Prim a) => p n -> a -> Vector n a Source #
O(n)
Construct a vector with the same element in each position where the
length is given explicitly as a
Proxy
argument.
generate :: forall n a. ( KnownNat n, Prim a) => ( Finite n -> a) -> Vector n a Source #
O(n) construct a vector of the given length by applying the function to each index where the length is inferred from the type.
generate' :: forall n a p. ( KnownNat n, Prim a) => p n -> ( Finite n -> a) -> Vector n a Source #
O(n)
construct a vector of the given length by applying the function to
each index where the length is given explicitly as a
Proxy
argument.
iterateN :: forall n a. ( KnownNat n, Prim a) => (a -> a) -> a -> Vector n a Source #
O(n)
Apply function
n
times to value. Zeroth element is original value.
The length is inferred from the type.
iterateN' :: forall n a p. ( KnownNat n, Prim a) => p n -> (a -> a) -> a -> Vector n a Source #
O(n)
Apply function
n
times to value. Zeroth element is original value.
The length is given explicitly as a
Proxy
argument.
Monadic initialization
replicateM :: forall n m a. ( KnownNat n, Prim a, Monad m) => m a -> m ( Vector n a) Source #
O(n)
Execute the monadic action
n
times and store the results in a
vector where
n
is inferred from the type.
replicateM' :: forall n m a p. ( KnownNat n, Prim a, Monad m) => p n -> m a -> m ( Vector n a) Source #
O(n)
Execute the monadic action
n
times and store the results in a
vector where
n
is given explicitly as a
Proxy
argument.
generateM :: forall n m a. ( KnownNat n, Prim a, Monad m) => ( Finite n -> m a) -> m ( Vector n a) Source #
O(n)
Construct a vector of length
n
by applying the monadic action to
each index where
n
is inferred from the type.
generateM' :: forall n m a p. ( KnownNat n, Prim a, Monad m) => p n -> ( Finite n -> m a) -> m ( Vector n a) Source #
O(n)
Construct a vector of length
n
by applying the monadic action to
each index where
n
is given explicitly as a
Proxy
argument.
Unfolding
unfoldrN :: forall n a b. ( KnownNat n, Prim a) => (b -> (a, b)) -> b -> Vector n a Source #
O(n)
Construct a vector with exactly
n
elements by repeatedly applying
the generator function to the a seed. The length is inferred from the
type.
unfoldrN' :: forall n a b p. ( KnownNat n, Prim a) => p n -> (b -> (a, b)) -> b -> Vector n a Source #
O(n)
Construct a vector with exactly
n
elements by repeatedly applying
the generator function to the a seed. The length is given explicitly
as a
Proxy
argument.
Enumeration
enumFromN :: forall n a. ( KnownNat n, Prim a, Num a) => a -> Vector n a Source #
O(n)
Yield a vector of length
n
containing the values
x
,
x+1
, ...,
x + (n - 1)
. The length is inferred from the type.
enumFromN' :: forall n a p. ( KnownNat n, Prim a, Num a) => a -> p n -> Vector n a Source #
O(n)
Yield a vector of length
n
containing the values
x
,
x+1
, ...,
x + (n - 1)
. The length is given explicitly as a
Proxy
argument.
enumFromStepN :: forall n a. ( KnownNat n, Prim a, Num a) => a -> a -> Vector n a Source #
O(n)
Yield a vector of the given length containing the values
x
,
x+y
,
x+2y
, ...,
x + (n - 1)y
. The length is inferred from the type.
enumFromStepN' :: forall n a p. ( KnownNat n, Prim a, Num a) => a -> a -> p n -> Vector n a Source #
O(n)
Yield a vector of the given length containing the values
x
,
x+y
,
x+2y
, ...,
x + (n - 1)y
. The length is given explicitly as a
Proxy
argument.
Concatenation
(++) :: forall n m a. Prim a => Vector n a -> Vector m a -> Vector (n + m) a Source #
O(m+n) Concatenate two vectors.
Restricting memory usage
force :: Prim a => Vector n a -> Vector n a Source #
O(n) Yield the argument but force it not to retain any extra memory, possibly by copying it.
This is especially useful when dealing with slices. For example:
force (slice 0 2 <huge vector>)
Here, the slice retains a reference to the huge vector. Forcing it creates a copy of just the elements that belong to the slice and allows the huge vector to be garbage collected.
Modifying vectors
Bulk updates
:: Prim a | |
=> Vector m a |
initial vector (of length
|
-> [( Finite m, a)] |
list of index/value pairs (of length
|
-> Vector m a |
O(m+n)
For each pair
(i,a)
from the list, replace the vector
element at position
i
by
a
.
<5,9,2,7> // [(2,1),(0,3),(2,8)] = <3,9,8,7>
:: Prim a | |
=> Vector m a |
initial vector (of length
|
-> Vector n Int |
index vector (of length
|
-> Vector n a |
value vector (of length
|
-> Vector m a |
O(m+n)
For each index
i
from the index vector and the
corresponding value
a
from the value vector, replace the element of the
initial vector at position
i
by
a
.
update_ <5,9,2,7> <2,0,2> <1,3,8> = <3,9,8,7>
This function is useful for instances of
Vector
that cannot store pairs.
Otherwise,
update
is probably more convenient.
update_ xs is ys =update
xs (zip
is ys)
:: Prim a | |
=> Vector m a |
initial vector (of length
|
-> [( Int , a)] |
list of index/value pairs (of length
|
-> Vector m a |
Same as (
//
) but without bounds checking.
:: Prim a | |
=> Vector m a |
initial vector (of length
|
-> Vector n Int |
index vector (of length
|
-> Vector n a |
value vector (of length
|
-> Vector m a |
Same as
update_
but without bounds checking.
Accumulations
:: Prim a | |
=> (a -> b -> a) |
accumulating function
|
-> Vector m a |
initial vector (of length
|
-> [( Finite m, b)] |
list of index/value pairs (of length
|
-> Vector m a |
O(m+n)
For each pair
(i,b)
from the list, replace the vector element
a
at position
i
by
f a b
.
accum (+) <5,9,2> [(2,4),(1,6),(0,3),(1,7)] = <5+3, 9+6+7, 2+4>
:: ( Prim a, Prim b) | |
=> (a -> b -> a) |
accumulating function
|
-> Vector m a |
initial vector (of length
|
-> Vector n Int |
index vector (of length
|
-> Vector n b |
value vector (of length
|
-> Vector m a |
O(m+n)
For each index
i
from the index vector and the
corresponding value
b
from the the value vector,
replace the element of the initial vector at
position
i
by
f a b
.
accumulate_ (+) <5,9,2> <2,1,0,1> <4,6,3,7> = <5+3, 9+6+7, 2+4>
This function is useful for instances of
Vector
that cannot store pairs.
Otherwise,
accumulate
is probably more convenient:
accumulate_ f as is bs =accumulate
f as (zip
is bs)
:: Prim a | |
=> (a -> b -> a) |
accumulating function
|
-> Vector m a |
initial vector (of length
|
-> [( Int , b)] |
list of index/value pairs (of length
|
-> Vector m a |
Same as
accum
but without bounds checking.
:: ( Prim a, Prim b) | |
=> (a -> b -> a) |
accumulating function
|
-> Vector m a |
initial vector (of length
|
-> Vector n Int |
index vector (of length
|
-> Vector n b |
value vector (of length
|
-> Vector m a |
Same as
accumulate_
but without bounds checking.
Permutations
:: Prim a | |
=> Vector m a |
|
-> Vector n Int |
|
-> Vector n a |
O(n)
Yield the vector obtained by replacing each element
i
of the
index vector by
xs
. This is equivalent to
!
i
but is
often much more efficient.
map
(xs
!
) is
backpermute <a,b,c,d> <0,3,2,3,1,0> = <a,d,c,d,b,a>
:: Prim a | |
=> Vector m a |
|
-> Vector n Int |
|
-> Vector n a |
Same as
backpermute
but without bounds checking.
Lenses
ix :: forall n a f. ( Prim a, Functor f) => Finite n -> (a -> f a) -> Vector n a -> f ( Vector n a) Source #
Lens to access ( O(1) ) and update ( O(n) ) an arbitrary element by its index.
_head :: forall n a f. ( Prim a, Functor f) => (a -> f a) -> Vector (1 + n) a -> f ( Vector (1 + n) a) Source #
Lens to access ( O(1) ) and update ( O(n) ) the first element of a non-empty vector.
_last :: forall n a f. ( Prim a, Functor f) => (a -> f a) -> Vector (n + 1) a -> f ( Vector (n + 1) a) Source #
Lens to access ( O(1) ) and update ( O(n) ) the last element of a non-empty vector.
Elementwise operations
Mapping
map :: ( Prim a, Prim b) => (a -> b) -> Vector n a -> Vector n b Source #
O(n) Map a function over a vector.
imap :: ( Prim a, Prim b) => ( Finite n -> a -> b) -> Vector n a -> Vector n b Source #
O(n) Apply a function to every element of a vector and its index.
concatMap :: ( Prim a, Prim b) => (a -> Vector m b) -> Vector n a -> Vector (n * m) b Source #
O(n*m) Map a function over a vector and concatenate the results. The function is required to always return the same length vector.
Monadic mapping
mapM :: ( Monad m, Prim a, Prim b) => (a -> m b) -> Vector n a -> m ( Vector n b) Source #
O(n) Apply the monadic action to all elements of the vector, yielding a vector of results.
imapM :: ( Monad m, Prim a, Prim b) => ( Finite n -> a -> m b) -> Vector n a -> m ( Vector n b) Source #
O(n) Apply the monadic action to every element of a vector and its index, yielding a vector of results.
mapM_ :: ( Monad m, Prim a) => (a -> m b) -> Vector n a -> m () Source #
O(n) Apply the monadic action to all elements of a vector and ignore the results.
imapM_ :: ( Monad m, Prim a) => ( Finite n -> a -> m b) -> Vector n a -> m () Source #
O(n) Apply the monadic action to every element of a vector and its index, ignoring the results.
forM :: ( Monad m, Prim a, Prim b) => Vector n a -> (a -> m b) -> m ( Vector n b) Source #
O(n)
Apply the monadic action to all elements of the vector, yielding a
vector of results. Equvalent to
flip
.
mapM
forM_ :: ( Monad m, Prim a) => Vector n a -> (a -> m b) -> m () Source #
O(n)
Apply the monadic action to all elements of a vector and ignore the
results. Equivalent to
flip
.
mapM_
Zipping
zipWith :: ( Prim a, Prim b, Prim c) => (a -> b -> c) -> Vector n a -> Vector n b -> Vector n c Source #
O(n) Zip two vectors of the same length with the given function.
zipWith3 :: ( Prim a, Prim b, Prim c, Prim d) => (a -> b -> c -> d) -> Vector n a -> Vector n b -> Vector n c -> Vector n d Source #
Zip three vectors with the given function.
zipWith4 :: ( Prim a, Prim b, Prim c, Prim d, Prim e) => (a -> b -> c -> d -> e) -> Vector n a -> Vector n b -> Vector n c -> Vector n d -> Vector n e Source #
zipWith5 :: ( Prim a, Prim b, Prim c, Prim d, Prim e, Prim f) => (a -> b -> c -> d -> e -> f) -> Vector n a -> Vector n b -> Vector n c -> Vector n d -> Vector n e -> Vector n f Source #
zipWith6 :: ( Prim a, Prim b, Prim c, Prim d, Prim e, Prim f, Prim g) => (a -> b -> c -> d -> e -> f -> g) -> Vector n a -> Vector n b -> Vector n c -> Vector n d -> Vector n e -> Vector n f -> Vector n g Source #
izipWith :: ( Prim a, Prim b, Prim c) => ( Finite n -> a -> b -> c) -> Vector n a -> Vector n b -> Vector n c Source #
O(n) Zip two vectors of the same length with a function that also takes the elements' indices).
izipWith3 :: ( Prim a, Prim b, Prim c, Prim d) => ( Finite n -> a -> b -> c -> d) -> Vector n a -> Vector n b -> Vector n c -> Vector n d Source #
izipWith4 :: ( Prim a, Prim b, Prim c, Prim d, Prim e) => ( Finite n -> a -> b -> c -> d -> e) -> Vector n a -> Vector n b -> Vector n c -> Vector n d -> Vector n e Source #
izipWith5 :: ( Prim a, Prim b, Prim c, Prim d, Prim e, Prim f) => ( Finite n -> a -> b -> c -> d -> e -> f) -> Vector n a -> Vector n b -> Vector n c -> Vector n d -> Vector n e -> Vector n f Source #
izipWith6 :: ( Prim a, Prim b, Prim c, Prim d, Prim e, Prim f, Prim g) => ( Finite n -> a -> b -> c -> d -> e -> f -> g) -> Vector n a -> Vector n b -> Vector n c -> Vector n d -> Vector n e -> Vector n f -> Vector n g Source #
Monadic zipping
zipWithM :: ( Monad m, Prim a, Prim b, Prim c) => (a -> b -> m c) -> Vector n a -> Vector n b -> m ( Vector n c) Source #
O(n) Zip the two vectors of the same length with the monadic action and yield a vector of results.
izipWithM :: ( Monad m, Prim a, Prim b, Prim c) => ( Finite n -> a -> b -> m c) -> Vector n a -> Vector n b -> m ( Vector n c) Source #
O(n) Zip the two vectors with a monadic action that also takes the element index and yield a vector of results.
zipWithM_ :: ( Monad m, Prim a, Prim b) => (a -> b -> m c) -> Vector n a -> Vector n b -> m () Source #
O(n) Zip the two vectors with the monadic action and ignore the results.
izipWithM_ :: ( Monad m, Prim a, Prim b) => ( Finite n -> a -> b -> m c) -> Vector n a -> Vector n b -> m () Source #
O(n) Zip the two vectors with a monadic action that also takes the element index and ignore the results.
Working with predicates
Searching
elem :: ( Prim a, Eq a) => a -> Vector n a -> Bool infix 4 Source #
O(n) Check if the vector contains an element.
notElem :: ( Prim a, Eq a) => a -> Vector n a -> Bool infix 4 Source #
O(n)
Check if the vector does not contain an element (inverse of
elem
).
Folding
foldl1 :: Prim a => (a -> a -> a) -> Vector (1 + n) a -> a Source #
O(n) Left fold on non-empty vectors.
foldl' :: Prim b => (a -> b -> a) -> a -> Vector n b -> a Source #
O(n) Left fold with strict accumulator.
foldl1' :: Prim a => (a -> a -> a) -> Vector (1 + n) a -> a Source #
O(n) Left fold on non-empty vectors with strict accumulator.
foldr1 :: Prim a => (a -> a -> a) -> Vector (n + 1) a -> a Source #
O(n) Right fold on non-empty vectors.
foldr' :: Prim a => (a -> b -> b) -> b -> Vector n a -> b Source #
O(n) Right fold with a strict accumulator.
foldr1' :: Prim a => (a -> a -> a) -> Vector (n + 1) a -> a Source #
O(n) Right fold on non-empty vectors with strict accumulator.
ifoldl :: Prim b => (a -> Finite n -> b -> a) -> a -> Vector n b -> a Source #
O(n) Left fold (function applied to each element and its index).
ifoldl' :: Prim b => (a -> Finite n -> b -> a) -> a -> Vector n b -> a Source #
O(n) Left fold with strict accumulator (function applied to each element and its index).
ifoldr :: Prim a => ( Finite n -> a -> b -> b) -> b -> Vector n a -> b Source #
O(n) Right fold (function applied to each element and its index).
ifoldr' :: Prim a => ( Finite n -> a -> b -> b) -> b -> Vector n a -> b Source #
O(n) Right fold with strict accumulator (function applied to each element and its index).
Specialised folds
all :: Prim a => (a -> Bool ) -> Vector n a -> Bool Source #
O(n) Check if all elements satisfy the predicate.
any :: Prim a => (a -> Bool ) -> Vector n a -> Bool Source #
O(n) Check if any element satisfies the predicate.
maximum :: ( Prim a, Ord a) => Vector (n + 1) a -> a Source #
O(n) Yield the maximum element of the non-empty vector.
maximumBy :: Prim a => (a -> a -> Ordering ) -> Vector (n + 1) a -> a Source #
O(n) Yield the maximum element of the non-empty vector according to the given comparison function.
minimum :: ( Prim a, Ord a) => Vector (n + 1) a -> a Source #
O(n) Yield the minimum element of the non-empty vector.
minimumBy :: Prim a => (a -> a -> Ordering ) -> Vector (n + 1) a -> a Source #
O(n) Yield the minimum element of the non-empty vector according to the given comparison function.
maxIndex :: ( Prim a, Ord a) => Vector (n + 1) a -> Finite (n + 1) Source #
O(n) Yield the index of the maximum element of the non-empty vector.
maxIndexBy :: Prim a => (a -> a -> Ordering ) -> Vector (n + 1) a -> Finite (n + 1) Source #
O(n) Yield the index of the maximum element of the non-empty vector according to the given comparison function.
minIndex :: ( Prim a, Ord a) => Vector (n + 1) a -> Finite (n + 1) Source #
O(n) Yield the index of the minimum element of the non-empty vector.
minIndexBy :: Prim a => (a -> a -> Ordering ) -> Vector (n + 1) a -> Finite (n + 1) Source #
O(n) Yield the index of the minimum element of the non-empty vector according to the given comparison function.
Monadic folds
foldM :: ( Monad m, Prim b) => (a -> b -> m a) -> a -> Vector n b -> m a Source #
O(n) Monadic fold.
ifoldM :: ( Monad m, Prim b) => (a -> Finite n -> b -> m a) -> a -> Vector n b -> m a Source #
O(n) Monadic fold (action applied to each element and its index).
fold1M :: ( Monad m, Prim a) => (a -> a -> m a) -> Vector (1 + n) a -> m a Source #
O(n) Monadic fold over non-empty vectors.
foldM' :: ( Monad m, Prim b) => (a -> b -> m a) -> a -> Vector n b -> m a Source #
O(n) Monadic fold with strict accumulator.
ifoldM' :: ( Monad m, Prim b) => (a -> Finite n -> b -> m a) -> a -> Vector n b -> m a Source #
O(n) Monadic fold with strict accumulator (action applied to each element and its index).
fold1M' :: ( Monad m, Prim a) => (a -> a -> m a) -> Vector (n + 1) a -> m a Source #
O(n) Monadic fold over non-empty vectors with strict accumulator.
foldM_ :: ( Monad m, Prim b) => (a -> b -> m a) -> a -> Vector n b -> m () Source #
O(n) Monadic fold that discards the result.
ifoldM_ :: ( Monad m, Prim b) => (a -> Finite n -> b -> m a) -> a -> Vector n b -> m () Source #
O(n) Monadic fold that discards the result (action applied to each element and its index).
fold1M_ :: ( Monad m, Prim a) => (a -> a -> m a) -> Vector (n + 1) a -> m () Source #
O(n) Monadic fold over non-empty vectors that discards the result.
foldM'_ :: ( Monad m, Prim b) => (a -> b -> m a) -> a -> Vector n b -> m () Source #
O(n) Monadic fold with strict accumulator that discards the result.
ifoldM'_ :: ( Monad m, Prim b) => (a -> Finite n -> b -> m a) -> a -> Vector n b -> m () Source #
O(n) Monadic fold with strict accumulator that discards the result (action applied to each element and its index).
fold1M'_ :: ( Monad m, Prim a) => (a -> a -> m a) -> Vector (n + 1) a -> m () Source #
O(n) Monad fold over non-empty vectors with strict accumulator that discards the result.
Prefix sums (scans)
prescanl' :: ( Prim a, Prim b) => (a -> b -> a) -> a -> Vector n b -> Vector n a Source #
O(n) Prescan with strict accumulator.
postscanl :: ( Prim a, Prim b) => (a -> b -> a) -> a -> Vector n b -> Vector n a Source #
O(n) Scan.
postscanl' :: ( Prim a, Prim b) => (a -> b -> a) -> a -> Vector n b -> Vector n a Source #
O(n) Scan with strict accumulator.
scanl :: ( Prim a, Prim b) => (a -> b -> a) -> a -> Vector n b -> Vector (1 + n) a Source #
O(n) Haskell-style scan.
scanl' :: ( Prim a, Prim b) => (a -> b -> a) -> a -> Vector n b -> Vector (1 + n) a Source #
O(n) Haskell-style scan with strict accumulator.
scanl1 :: Prim a => (a -> a -> a) -> Vector (1 + n) a -> Vector (2 + n) a Source #
O(n) Scan over a non-empty vector.
scanl1' :: Prim a => (a -> a -> a) -> Vector (1 + n) a -> Vector (2 + n) a Source #
O(n) Scan over a non-empty vector with a strict accumulator.
prescanr :: ( Prim a, Prim b) => (a -> b -> b) -> b -> Vector n a -> Vector n b Source #
O(n) Right-to-left prescan.
prescanr' :: ( Prim a, Prim b) => (a -> b -> b) -> b -> Vector n a -> Vector n b Source #
O(n) Right-to-left prescan with strict accumulator.
postscanr :: ( Prim a, Prim b) => (a -> b -> b) -> b -> Vector n a -> Vector n b Source #
O(n) Right-to-left scan.
postscanr' :: ( Prim a, Prim b) => (a -> b -> b) -> b -> Vector n a -> Vector n b Source #
O(n) Right-to-left scan with strict accumulator.
scanr :: ( Prim a, Prim b) => (a -> b -> b) -> b -> Vector n a -> Vector (n + 1) b Source #
O(n) Right-to-left Haskell-style scan.
scanr' :: ( Prim a, Prim b) => (a -> b -> b) -> b -> Vector n a -> Vector (n + 1) b Source #
O(n) Right-to-left Haskell-style scan with strict accumulator.
scanr1 :: Prim a => (a -> a -> a) -> Vector (n + 1) a -> Vector (n + 2) a Source #
O(n) Right-to-left scan over a non-empty vector.
scanr1' :: Prim a => (a -> a -> a) -> Vector (n + 1) a -> Vector (n + 2) a Source #
O(n) Right-to-left scan over a non-empty vector with a strict accumulator.
Conversions
Lists
fromList :: ( Prim a, KnownNat n) => [a] -> Maybe ( Vector n a) Source #
O(n) Convert a list to a vector.
fromListN :: forall n a. ( Prim a, KnownNat n) => [a] -> Maybe ( Vector n a) Source #
O(n)
Convert the first
n
elements of a list to a vector. The length of
the resulting vector is inferred from the type.
fromListN' :: forall n a p. ( Prim a, KnownNat n) => p n -> [a] -> Maybe ( Vector n a) Source #
O(n)
Convert the first
n
elements of a list to a vector. The length of
the resulting vector is given explicitly as a
Proxy
argument.
withSizedList :: forall a r. Prim a => [a] -> ( forall n. KnownNat n => Vector n a -> r) -> r Source #
O(n) Takes a list and returns a continuation providing a vector with a size parameter corresponding to the length of the list.
Essentially converts a list into a vector with the proper size parameter, determined at runtime.
See
withSized
Mutable vectors
freeze :: ( PrimMonad m, Prim a) => MVector n ( PrimState m) a -> m ( Vector n a) Source #
O(n) Yield an immutable copy of the mutable vector.
thaw :: ( PrimMonad m, Prim a) => Vector n a -> m ( MVector n ( PrimState m) a) Source #
O(n) Yield a mutable copy of the immutable vector.
copy :: ( PrimMonad m, Prim a) => MVector n ( PrimState m) a -> Vector n a -> m () Source #
O(n) Copy an immutable vector into a mutable one.
unsafeFreeze :: ( PrimMonad m, Prim a) => MVector n ( PrimState m) a -> m ( Vector n a) Source #
O(1) Unsafely convert a mutable vector to an immutable one withouy copying. The mutable vector may not be used after this operation.
unsafeThaw :: ( PrimMonad m, Prim a) => Vector n a -> m ( MVector n ( PrimState m) a) Source #
O(n) Unsafely convert an immutable vector to a mutable one without copying. The immutable vector may not be used after this operation.