:: [a] -> Int -> Maybe a -package:rio

Safer version of !!, returns a Maybe. get element from list using index value starting from `0`.
>>> [] !!? 0
>>> ["a", "b", "c"] !!? 3
>>> [1, 2, 3] !!? (-1)
>>> ["a", "b", "c"] !!? 2
Just "c"
Lookup function (partially safe). O(min n index).
O(1) Safe indexing
A (!!) that fails using mzero
Assumed complexity: O(1) Yield the element at the given position in a monad. No range checks are performed. The monad allows us to be strict in the vector if we want. Suppose we had
unsafeIndex :: v a -> Int -> a
instead. Now, if we wanted to copy a vector, we'd do something like
copy mv v ... = ... unsafeWrite mv i (unsafeIndex v i) ...
For lazy vectors, the indexing would not be evaluated which means that we would retain a reference to the original vector in each element we write. This is not what we want! With basicUnsafeIndexM, we can do
copy mv v ... = ... case basicUnsafeIndexM v i of
Box x -> unsafeWrite mv i x ...
which does not have this problem because indexing (but not the returned element!) is evaluated immediately.
O(1) Indexing in a monad. The monad allows operations to be strict in the vector when necessary. Suppose vector copying is implemented like this:
copy mv v = ... write mv i (v ! i) ...
For lazy vectors, v ! i would not be evaluated which means that mv would unnecessarily retain a reference to v in each element written. With indexM, copying can be implemented like this instead:
copy mv v = ... do
x <- indexM v i
write mv i x
Here, no references to v are retained because indexing (but not the elements) is evaluated eagerly.
O(1) Indexing in a monad without bounds checks. See indexM for an explanation of why this is useful.
Safe version of !!
Safer version of !! -
O(n) Convert the first n elements of a list to a vector
fromListN n xs = fromList (take n xs)


>>> import qualified Data.Vector as V

>>> V.fromListN 3 [1,2,3,4,5::Int]

>>> V.fromListN 3 [1::Int]