>>> ['a', 'b', 'c'] !? 0 Just 'a'
>>> ['a', 'b', 'c'] !? 2 Just 'c'
>>> ['a', 'b', 'c'] !? 3 Nothing
>>> ['a', 'b', 'c'] !? (-1) Nothing
>>> ['a', 'b', 'c'] !? 0 Just 'a' >>> ['a', 'b', 'c'] !? 2 Just 'c' >>> ['a', 'b', 'c'] !? 3 Nothing >>> ['a', 'b', 'c'] !? (-1) NothingThis is the total variant of the partial !! operator. WARNING: This function takes linear time in the index.
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 xHere, no references to v are retained because indexing (but not the element) is evaluated eagerly.
fromListN n xs = fromList (take n xs)
>>> import qualified Data.Vector.Strict as V >>> V.fromListN 3 [1,2,3,4,5] [1,2,3] >>> V.fromListN 3 [1] [1]
>>> fromReverseListN 3 [1,2,3] :: Data.Vector.Vector Int [3,2,1]