packages feed

papa-0.7.0: src/Papa/Vector/Data/Vector.hs

{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wall #-}

{- HLINT ignore "Avoid restricted function" -}
-- Reason: this module defines total versions of head, last, init, tail,
-- maximum and minimum, whose names HLint restricts whatever module they come
-- from.

{- HLINT ignore "Eta reduce" -}
-- Reason: each function takes, on the left of its definition, the arguments
-- before its vector (none for most): GHC inlines a function only once it is
-- applied to those, so @foldl1' (+)@, passed to a higher-order function,
-- is inlined and runs at the speed of vector's. A function with no such
-- arguments applies its generic version to all its arguments, so that the
-- generic one is specialised to this vector type in the compiled function
-- too.

-- | Boxed vectors, with total versions of the partial functions of
-- "Data.Vector". Import qualified:
--
-- > import qualified Papa.Vector.Data.Vector as V
--
-- The functions of "Data.Vector" that throw (for an index out of range, an
-- empty vector, a negative length, or vectors of different lengths) are
-- replaced by functions of the same names that return
-- 'Data.Maybe.Nothing' there instead, in the same time and with one copy of
-- the vector, as the originals. They are those of
-- "Papa.Vector.Data.Vector.Generic", at this vector type.
--
-- Read and set elements with the traversal @'Control.Lens.ix' i@ of lens's
-- 'Control.Lens.Ixed' instance (@v ^? ix i@, @v & ix i .~ a@), or read with
-- '!?'. Setting an element with 'Control.Lens.ix' copies the vector: set many
-- at once with '//'. lens's instances for this vector type: Ixed, Cons, Snoc, Each, AsEmpty, Reversing, FunctorWithIndex, FoldableWithIndex, TraversableWithIndex.
--
-- Not exported:
--
-- * @(!)@, which throws for an index out of range: use '!?' or
--   'Control.Lens.ix';
-- * the functions without bounds checks (@unsafeIndex@, @unsafeSlice@,
--   @unsafeUpd@, …), which read or write outside the vector for a bad
--   index, and @unsafeFreeze@, @unsafeThaw@, which let a mutable vector
--   change an immutable one;
-- * @unsafeFromArraySlice@, which reads outside the array for a bad slice.
--
-- The mutable vectors (@MVector@) are exported as a type; their functions
-- (in "Data.Vector.Mutable") are not wrapped.
module Papa.Vector.Data.Vector (
  module X,
  head,
  last,
  indexM,
  headM,
  lastM,
  slice,
  init,
  tail,
  constructN,
  constructrN,
  (//),
  update,
  update_,
  accum,
  accumulate,
  accumulate_,
  backpermute,
  foldl1,
  foldl1',
  foldr1,
  foldr1',
  maximum,
  maximumBy,
  maximumOn,
  minimum,
  minimumBy,
  minimumOn,
  maxIndex,
  maxIndexBy,
  minIndex,
  minIndexBy,
  fold1M,
  fold1M',
  fold1M_,
  fold1M'_,
  copy,
) where

import Control.Monad (Monad)
import Control.Monad.Primitive (PrimMonad, PrimState)
import Data.Int (Int)
import Data.Maybe (Maybe)
import Data.Ord (Ord, Ordering)
import Data.Vector as X hiding (
  accum,
  accumulate,
  accumulate_,
  backpermute,
  constructN,
  constructrN,
  copy,
  fold1M,
  fold1M',
  fold1M'_,
  fold1M_,
  foldl1,
  foldl1',
  foldr1,
  foldr1',
  head,
  headM,
  indexM,
  init,
  last,
  lastM,
  maxIndex,
  maxIndexBy,
  maximum,
  maximumBy,
  maximumOn,
  minIndex,
  minIndexBy,
  minimum,
  minimumBy,
  minimumOn,
  slice,
  tail,
  unsafeAccum,
  unsafeAccumulate,
  unsafeAccumulate_,
  unsafeBackpermute,
  unsafeCopy,
  unsafeDrop,
  unsafeFreeze,
  unsafeFromArraySlice,
  unsafeHead,
  unsafeHeadM,
  unsafeIndex,
  unsafeIndexM,
  unsafeInit,
  unsafeLast,
  unsafeLastM,
  unsafeSlice,
  unsafeTail,
  unsafeTake,
  unsafeThaw,
  unsafeUpd,
  unsafeUpdate,
  unsafeUpdate_,
  update,
  update_,
  (!),
  (//),
 )
import qualified Papa.Vector.Data.Vector.Generic as G

-- $setup
-- >>> :set -XNoImplicitPrelude
-- >>> import Papa
-- >>> import qualified Papa as P
-- >>> import qualified Papa.Vector.Data.Vector as V
-- >>> import qualified Data.Vector.Mutable as MV
-- >>> import Control.Monad.ST (runST)
-- >>> import Hedgehog
-- >>> import qualified Hedgehog.Gen as Gen
-- >>> import qualified Hedgehog.Range as Range
-- >>> let genInt = Gen.int (Range.linear (-100) 100)
-- >>> let genIndex = Gen.int (Range.linear (-3) 12)
-- >>> let genVector = V.fromList <$> Gen.list (Range.linear 0 10) genInt
-- >>> let genUpdates = Gen.list (Range.linear 0 5) ((,) <$> genIndex <*> genInt)

-- | Total @head@: the first element. 'Data.Maybe.Nothing' for an empty vector.
--
-- ==== __Examples__
--
-- >>> V.head (V.fromList "abc")
-- Just 'a'
--
-- >>> V.head (V.empty :: V.Vector Char)
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do v <- forAll genVector; V.head v === listToMaybe (V.toList v)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
head :: Vector a -> Maybe a
head v =
  G.head v
{-# INLINE head #-}

-- | Total @last@: the last element. 'Data.Maybe.Nothing' for an empty vector.
--
-- ==== __Examples__
--
-- >>> V.last (V.fromList "abc")
-- Just 'c'
--
-- >>> V.last (V.empty :: V.Vector Char)
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do v <- forAll genVector; V.last v === listToMaybe (P.reverse (V.toList v))
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
last :: Vector a -> Maybe a
last v =
  G.last v
{-# INLINE last #-}

-- | Total @indexM@: the element at an index, read in a monad (so that the
-- vector is not retained by an unevaluated read). 'Data.Maybe.Nothing' for an index out
-- of range.
--
-- ==== __Examples__
--
-- >>> V.indexM (V.fromList "abc") 1 :: Identity (Maybe Char)
-- Identity (Just 'b')
--
-- >>> V.indexM (V.fromList "abc") 3 :: Identity (Maybe Char)
-- Identity Nothing
--
-- >>> check $ withTests 100 $ property $ do v <- forAll genVector; i <- forAll genIndex; runIdentity (V.indexM v i) === (v V.!? i)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
indexM :: (Monad m) => Vector a -> Int -> m (Maybe a)
indexM v i =
  G.indexM v i
{-# INLINE indexM #-}

-- | Total @headM@: the first element, read in a monad. 'Data.Maybe.Nothing' for an
-- empty vector.
--
-- ==== __Examples__
--
-- >>> V.headM (V.fromList "abc") :: Identity (Maybe Char)
-- Identity (Just 'a')
--
-- >>> check $ withTests 100 $ property $ do v <- forAll genVector; runIdentity (V.headM v) === V.head v
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
headM :: (Monad m) => Vector a -> m (Maybe a)
headM v =
  G.headM v
{-# INLINE headM #-}

-- | Total @lastM@: the last element, read in a monad. 'Data.Maybe.Nothing' for an empty
-- vector.
--
-- ==== __Examples__
--
-- >>> V.lastM (V.fromList "abc") :: Identity (Maybe Char)
-- Identity (Just 'c')
--
-- >>> check $ withTests 100 $ property $ do v <- forAll genVector; runIdentity (V.lastM v) === V.last v
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
lastM :: (Monad m) => Vector a -> m (Maybe a)
lastM v =
  G.lastM v
{-# INLINE lastM #-}

-- | Total @slice@: the slice of a length from an index, in constant time.
-- 'Data.Maybe.Nothing' unless the slice is within the vector.
--
-- ==== __Examples__
--
-- >>> V.slice 1 2 (V.fromList "abcd")
-- Just "bc"
--
-- >>> V.slice 3 2 (V.fromList "abcd")
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do v <- forAll genVector; i <- forAll genIndex; n <- forAll genIndex; fmap V.toList (V.slice i n v) === bool Nothing (Just (P.take n (P.drop i (V.toList v)))) (i >= 0 && n >= 0 && i + n <= V.length v)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
slice :: Int -> Int -> Vector a -> Maybe (Vector a)
slice i n =
  G.slice i n
{-# INLINE slice #-}

-- | Total @init@: every element but the last, in constant time. 'Data.Maybe.Nothing'
-- for an empty vector.
--
-- ==== __Examples__
--
-- >>> V.init (V.fromList "abc")
-- Just "ab"
--
-- >>> V.init (V.empty :: V.Vector Char)
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do v <- forAll genVector; fmap V.toList (V.init v) === bool Nothing (Just (P.take (V.length v - 1) (V.toList v))) (not (V.null v))
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
init :: Vector a -> Maybe (Vector a)
init v =
  G.init v
{-# INLINE init #-}

-- | Total @tail@: every element but the first, in constant time. 'Data.Maybe.Nothing'
-- for an empty vector.
--
-- ==== __Examples__
--
-- >>> V.tail (V.fromList "abc")
-- Just "bc"
--
-- >>> V.tail (V.empty :: V.Vector Char)
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do v <- forAll genVector; fmap V.toList (V.tail v) === fmap snd (P.uncons (V.toList v))
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
tail :: Vector a -> Maybe (Vector a)
tail v =
  G.tail v
{-# INLINE tail #-}

-- | Total @constructN@: a vector of a length, each element computed from
-- those before it. 'Data.Maybe.Nothing' for a negative length.
--
-- ==== __Examples__
--
-- >>> V.constructN 4 (\v -> V.length v * 10) :: Maybe (V.Vector Int)
-- Just [0,10,20,30]
--
-- >>> V.constructN (-1) V.length :: Maybe (V.Vector Int)
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do n <- forAll genIndex; fmap V.toList (V.constructN n V.sum :: Maybe (V.Vector Int)) === bool Nothing (Just (P.replicate n 0)) (n >= 0)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
constructN :: Int -> (Vector a -> a) -> Maybe (Vector a)
constructN i f =
  G.constructN i f
{-# INLINE constructN #-}

-- | Total @constructrN@: a vector of a length, each element computed from
-- those after it. 'Data.Maybe.Nothing' for a negative length.
--
-- ==== __Examples__
--
-- >>> V.constructrN 4 (\v -> V.length v * 10) :: Maybe (V.Vector Int)
-- Just [30,20,10,0]
--
-- >>> V.constructrN (-1) V.length :: Maybe (V.Vector Int)
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do n <- forAll genIndex; fmap V.toList (V.constructrN n V.length :: Maybe (V.Vector Int)) === fmap (\w -> P.reverse (V.toList w)) (V.constructN n V.length :: Maybe (V.Vector Int))
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
constructrN :: Int -> (Vector a -> a) -> Maybe (Vector a)
constructrN i f =
  G.constructrN i f
{-# INLINE constructrN #-}

-- | Total @(//)@: a vector with the elements at the indices of a list of
-- updates replaced, in one copy. 'Data.Maybe.Nothing' if an index is out of range.
--
-- ==== __Examples__
--
-- >>> V.fromList "abc" V.// [(0, 'x'), (2, 'z')]
-- Just "xbz"
--
-- >>> V.fromList "abc" V.// [(3, 'x')]
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do v <- forAll genVector; us <- forAll genUpdates; (v V.// us) === bool Nothing (Just (P.foldl (\w (i, a) -> w & ix i .~ a) v us)) (P.all (\(i, _) -> i >= 0 && i < V.length v) us)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
(//) :: Vector a -> [(Int, a)] -> Maybe (Vector a)
(//) v us =
  (G.//) v us
{-# INLINE (//) #-}

infixl 9 //

-- | Total @update@: '//' with the updates in a vector. 'Data.Maybe.Nothing' if an index
-- is out of range.
--
-- ==== __Examples__
--
-- >>> V.update (V.fromList "abc") (V.fromList [(1, 'x')])
-- Just "axc"
--
-- >>> V.update (V.fromList "abc") (V.fromList [(-1, 'x')])
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do v <- forAll genVector; us <- forAll genUpdates; V.update v (V.fromList us) === (v V.// us)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
update :: Vector a -> Vector (Int, a) -> Maybe (Vector a)
update v w =
  G.update v w
{-# INLINE update #-}

-- | Total @update_@: 'update' with the indices and the values in two
-- vectors, paired up to the shorter. 'Data.Maybe.Nothing' if a paired index is out of
-- range.
--
-- ==== __Examples__
--
-- >>> V.update_ (V.fromList "abc") (V.fromList [2, 0]) (V.fromList "xy")
-- Just "ybx"
--
-- >>> V.update_ (V.fromList "abc") (V.fromList [2, 9]) (V.fromList "x")
-- Just "abx"
--
-- >>> V.update_ (V.fromList "abc") (V.fromList [9]) (V.fromList "x")
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do v <- forAll genVector; us <- forAll genUpdates; V.update_ v (V.fromList (fmap P.fst us)) (V.fromList (fmap P.snd us)) === (v V.// us)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
update_ :: Vector a -> Vector Int -> Vector a -> Maybe (Vector a)
update_ v w xs =
  G.update_ v w xs
{-# INLINE update_ #-}

-- | Total @accum@: a vector with each element at the indices of a list of
-- updates combined with the update, in one copy. 'Data.Maybe.Nothing' if an index is
-- out of range.
--
-- ==== __Examples__
--
-- >>> V.accum (+) (V.fromList [10, 20, 30 :: Int]) [(0, 1), (2, 3), (0, 4)]
-- Just [15,20,33]
--
-- >>> V.accum (+) (V.fromList [10, 20, 30 :: Int]) [(3, 1)]
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do v <- forAll genVector; us <- forAll genUpdates; V.accum (+) v us === bool Nothing (Just (P.foldl (\w (i, a) -> w & ix i +~ a) v us)) (P.all (\(i, _) -> i >= 0 && i < V.length v) us)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
accum :: (a -> b -> a) -> Vector a -> [(Int, b)] -> Maybe (Vector a)
accum f =
  G.accum f
{-# INLINE accum #-}

-- | Total @accumulate@: 'accum' with the updates in a vector. 'Data.Maybe.Nothing' if
-- an index is out of range.
--
-- ==== __Examples__
--
-- >>> V.accumulate (+) (V.fromList [10, 20 :: Int]) (V.fromList [(1, 5)])
-- Just [10,25]
--
-- >>> check $ withTests 100 $ property $ do v <- forAll genVector; us <- forAll genUpdates; V.accumulate (+) v (V.fromList us) === V.accum (+) v us
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
accumulate :: (a -> b -> a) -> Vector a -> Vector (Int, b) -> Maybe (Vector a)
accumulate f =
  G.accumulate f
{-# INLINE accumulate #-}

-- | Total @accumulate_@: 'accumulate' with the indices and the values in two
-- vectors, paired up to the shorter. 'Data.Maybe.Nothing' if a paired index is out of
-- range.
--
-- ==== __Examples__
--
-- >>> V.accumulate_ (+) (V.fromList [10, 20 :: Int]) (V.fromList [1, 1]) (V.fromList [5, 6])
-- Just [10,31]
--
-- >>> check $ withTests 100 $ property $ do v <- forAll genVector; us <- forAll genUpdates; V.accumulate_ (+) v (V.fromList (fmap P.fst us)) (V.fromList (fmap P.snd us)) === V.accum (+) v us
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
accumulate_ :: (a -> b -> a) -> Vector a -> Vector Int -> Vector b -> Maybe (Vector a)
accumulate_ f =
  G.accumulate_ f
{-# INLINE accumulate_ #-}

-- | Total @backpermute@: the elements at the indices of a vector of indices.
-- 'Data.Maybe.Nothing' if an index is out of range.
--
-- ==== __Examples__
--
-- >>> V.backpermute (V.fromList "abcd") (V.fromList [3, 0, 3])
-- Just "dad"
--
-- >>> V.backpermute (V.fromList "abcd") (V.fromList [4])
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do v <- forAll genVector; is <- forAll (Gen.list (Range.linear 0 5) genIndex); fmap V.toList (V.backpermute v (V.fromList is)) === traverse (v V.!?) is
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
backpermute :: Vector a -> Vector Int -> Maybe (Vector a)
backpermute v w =
  G.backpermute v w
{-# INLINE backpermute #-}

-- | Total @foldl1@: a left fold with no starting value. 'Data.Maybe.Nothing' for an
-- empty vector.
--
-- ==== __Examples__
--
-- >>> V.foldl1 (-) (V.fromList [10, 2, 3 :: Int])
-- Just 5
--
-- >>> V.foldl1 (-) (V.empty :: V.Vector Int)
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do v <- forAll genVector; V.foldl1 (-) v === fmap (\(a, as) -> P.foldl (-) a as) (P.uncons (V.toList v))
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
foldl1 :: (a -> a -> a) -> Vector a -> Maybe a
foldl1 f =
  G.foldl1 f
{-# INLINE foldl1 #-}

-- | Total @foldl1'@: 'foldl1', strict in the accumulator. 'Data.Maybe.Nothing' for an
-- empty vector.
--
-- ==== __Examples__
--
-- >>> V.foldl1' (-) (V.fromList [10, 2, 3 :: Int])
-- Just 5
--
-- >>> check $ withTests 100 $ property $ do v <- forAll genVector; V.foldl1' (-) v === V.foldl1 (-) v
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
foldl1' :: (a -> a -> a) -> Vector a -> Maybe a
foldl1' f =
  G.foldl1' f
{-# INLINE foldl1' #-}

-- | Total @foldr1@: a right fold with no starting value. 'Data.Maybe.Nothing' for an
-- empty vector.
--
-- ==== __Examples__
--
-- >>> V.foldr1 (-) (V.fromList [10, 2, 3 :: Int])
-- Just 11
--
-- >>> V.foldr1 (-) (V.empty :: V.Vector Int)
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do v <- forAll genVector; V.foldr1 (-) v === fmap (\(as, a) -> P.foldr (-) a as) (P.unsnoc (V.toList v))
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
foldr1 :: (a -> a -> a) -> Vector a -> Maybe a
foldr1 f =
  G.foldr1 f
{-# INLINE foldr1 #-}

-- | Total @foldr1'@: 'foldr1', strict in the accumulator. 'Data.Maybe.Nothing' for an
-- empty vector.
--
-- ==== __Examples__
--
-- >>> V.foldr1' (-) (V.fromList [10, 2, 3 :: Int])
-- Just 11
--
-- >>> check $ withTests 100 $ property $ do v <- forAll genVector; V.foldr1' (-) v === V.foldr1 (-) v
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
foldr1' :: (a -> a -> a) -> Vector a -> Maybe a
foldr1' f =
  G.foldr1' f
{-# INLINE foldr1' #-}

-- | Total @maximum@: the greatest element. 'Data.Maybe.Nothing' for an empty vector.
--
-- ==== __Examples__
--
-- >>> V.maximum (V.fromList [3, 1, 4 :: Int])
-- Just 4
--
-- >>> V.maximum (V.empty :: V.Vector Int)
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do v <- forAll genVector; V.maximum v === maximumOf folded (V.toList v)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
maximum :: (Ord a) => Vector a -> Maybe a
maximum v =
  G.maximum v
{-# INLINE maximum #-}

-- | Total @maximumBy@: the greatest element by a comparison; the first of
-- equals. 'Data.Maybe.Nothing' for an empty vector.
--
-- ==== __Examples__
--
-- >>> V.maximumBy (comparing negate) (V.fromList [3, 1, 4 :: Int])
-- Just 1
--
-- >>> check $ withTests 100 $ property $ do v <- forAll genVector; V.maximumBy compare v === V.maximum v
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
maximumBy :: (a -> a -> Ordering) -> Vector a -> Maybe a
maximumBy f =
  G.maximumBy f
{-# INLINE maximumBy #-}

-- | Total @maximumOn@: the element with the greatest image under a
-- function; the first of equals. 'Data.Maybe.Nothing' for an empty vector.
--
-- ==== __Examples__
--
-- >>> V.maximumOn negate (V.fromList [3, 1, 4 :: Int])
-- Just 1
--
-- >>> check $ withTests 100 $ property $ do v <- forAll genVector; V.maximumOn id v === V.maximum v
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
maximumOn :: (Ord b) => (a -> b) -> Vector a -> Maybe a
maximumOn f =
  G.maximumOn f
{-# INLINE maximumOn #-}

-- | Total @minimum@: the least element. 'Data.Maybe.Nothing' for an empty vector.
--
-- ==== __Examples__
--
-- >>> V.minimum (V.fromList [3, 1, 4 :: Int])
-- Just 1
--
-- >>> V.minimum (V.empty :: V.Vector Int)
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do v <- forAll genVector; V.minimum v === minimumOf folded (V.toList v)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
minimum :: (Ord a) => Vector a -> Maybe a
minimum v =
  G.minimum v
{-# INLINE minimum #-}

-- | Total @minimumBy@: the least element by a comparison; the first of
-- equals. 'Data.Maybe.Nothing' for an empty vector.
--
-- ==== __Examples__
--
-- >>> V.minimumBy (comparing negate) (V.fromList [3, 1, 4 :: Int])
-- Just 4
--
-- >>> check $ withTests 100 $ property $ do v <- forAll genVector; V.minimumBy compare v === V.minimum v
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
minimumBy :: (a -> a -> Ordering) -> Vector a -> Maybe a
minimumBy f =
  G.minimumBy f
{-# INLINE minimumBy #-}

-- | Total @minimumOn@: the element with the least image under a function;
-- the first of equals. 'Data.Maybe.Nothing' for an empty vector.
--
-- ==== __Examples__
--
-- >>> V.minimumOn negate (V.fromList [3, 1, 4 :: Int])
-- Just 4
--
-- >>> check $ withTests 100 $ property $ do v <- forAll genVector; V.minimumOn id v === V.minimum v
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
minimumOn :: (Ord b) => (a -> b) -> Vector a -> Maybe a
minimumOn f =
  G.minimumOn f
{-# INLINE minimumOn #-}

-- | Total @maxIndex@: the index of the greatest element; the first of
-- equals. 'Data.Maybe.Nothing' for an empty vector.
--
-- ==== __Examples__
--
-- >>> V.maxIndex (V.fromList [3, 4, 1, 4 :: Int])
-- Just 1
--
-- >>> V.maxIndex (V.empty :: V.Vector Int)
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do v <- forAll genVector; (V.maxIndex v >>= (v V.!?)) === V.maximum v
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
maxIndex :: (Ord a) => Vector a -> Maybe Int
maxIndex v =
  G.maxIndex v
{-# INLINE maxIndex #-}

-- | Total @maxIndexBy@: the index of the greatest element by a comparison.
-- 'Data.Maybe.Nothing' for an empty vector.
--
-- ==== __Examples__
--
-- >>> V.maxIndexBy (comparing negate) (V.fromList [3, 1, 4 :: Int])
-- Just 1
--
-- >>> check $ withTests 100 $ property $ do v <- forAll genVector; V.maxIndexBy compare v === V.maxIndex v
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
maxIndexBy :: (a -> a -> Ordering) -> Vector a -> Maybe Int
maxIndexBy f =
  G.maxIndexBy f
{-# INLINE maxIndexBy #-}

-- | Total @minIndex@: the index of the least element; the first of equals.
-- 'Data.Maybe.Nothing' for an empty vector.
--
-- ==== __Examples__
--
-- >>> V.minIndex (V.fromList [3, 1, 4, 1 :: Int])
-- Just 1
--
-- >>> V.minIndex (V.empty :: V.Vector Int)
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do v <- forAll genVector; (V.minIndex v >>= (v V.!?)) === V.minimum v
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
minIndex :: (Ord a) => Vector a -> Maybe Int
minIndex v =
  G.minIndex v
{-# INLINE minIndex #-}

-- | Total @minIndexBy@: the index of the least element by a comparison.
-- 'Data.Maybe.Nothing' for an empty vector.
--
-- ==== __Examples__
--
-- >>> V.minIndexBy (comparing negate) (V.fromList [3, 1, 4 :: Int])
-- Just 2
--
-- >>> check $ withTests 100 $ property $ do v <- forAll genVector; V.minIndexBy compare v === V.minIndex v
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
minIndexBy :: (a -> a -> Ordering) -> Vector a -> Maybe Int
minIndexBy f =
  G.minIndexBy f
{-# INLINE minIndexBy #-}

-- | Total @fold1M@: a monadic left fold with no starting value. 'Data.Maybe.Nothing'
-- for an empty vector.
--
-- ==== __Examples__
--
-- >>> V.fold1M (\a b -> Identity (a - b)) (V.fromList [10, 2, 3 :: Int])
-- Identity (Just 5)
--
-- >>> V.fold1M (\a b -> Identity (a - b)) (V.empty :: V.Vector Int)
-- Identity Nothing
--
-- >>> check $ withTests 100 $ property $ do v <- forAll genVector; runIdentity (V.fold1M (\a b -> Identity (a - b)) v) === V.foldl1 (-) v
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
fold1M :: (Monad m) => (a -> a -> m a) -> Vector a -> m (Maybe a)
fold1M f =
  G.fold1M f
{-# INLINE fold1M #-}

-- | Total @fold1M'@: 'fold1M', strict in the accumulator. 'Data.Maybe.Nothing' for an
-- empty vector.
--
-- ==== __Examples__
--
-- >>> V.fold1M' (\a b -> Identity (a - b)) (V.fromList [10, 2, 3 :: Int])
-- Identity (Just 5)
--
-- >>> check $ withTests 100 $ property $ do v <- forAll genVector; runIdentity (V.fold1M' (\a b -> Identity (a - b)) v) === V.foldl1 (-) v
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
fold1M' :: (Monad m) => (a -> a -> m a) -> Vector a -> m (Maybe a)
fold1M' f =
  G.fold1M' f
{-# INLINE fold1M' #-}

-- | Total @fold1M_@: 'fold1M' for its effects. 'Data.Maybe.Nothing' for an empty vector.
--
-- ==== __Examples__
--
-- >>> V.fold1M_ (\a b -> [a + b]) (V.fromList [1, 2 :: Int])
-- [Just ()]
--
-- >>> V.fold1M_ (\a b -> [a + b]) (V.empty :: V.Vector Int)
-- [Nothing]
--
-- >>> check $ withTests 100 $ property $ do v <- forAll genVector; runIdentity (V.fold1M_ (\a b -> Identity (a - b)) v) === (() <$ V.foldl1 (-) v)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
fold1M_ :: (Monad m) => (a -> a -> m a) -> Vector a -> m (Maybe ())
fold1M_ f =
  G.fold1M_ f
{-# INLINE fold1M_ #-}

-- | Total @fold1M'_@: 'fold1M'' for its effects. 'Data.Maybe.Nothing' for an empty
-- vector.
--
-- ==== __Examples__
--
-- >>> V.fold1M'_ (\a b -> [a + b]) (V.fromList [1, 2 :: Int])
-- [Just ()]
--
-- >>> check $ withTests 100 $ property $ do v <- forAll genVector; runIdentity (V.fold1M'_ (\a b -> Identity (a - b)) v) === (() <$ V.foldl1 (-) v)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
fold1M'_ :: (Monad m) => (a -> a -> m a) -> Vector a -> m (Maybe ())
fold1M'_ f =
  G.fold1M'_ f
{-# INLINE fold1M'_ #-}

-- | Total @copy@: copy an immutable vector into a mutable one of the same
-- length. 'Data.Maybe.Nothing', and no copy, for vectors of different lengths.
--
-- ==== __Examples__
--
-- >>> runST (do m <- MV.replicate 2 'x'; r <- V.copy m (V.fromList "ab"); fmap ((,) r) (V.freeze m))
-- (Just (),"ab")
--
-- >>> runST (do m <- MV.replicate 3 'x'; r <- V.copy m (V.fromList "ab"); fmap ((,) r) (V.freeze m))
-- (Nothing,"xxx")
--
-- >>> check $ withTests 100 $ property $ do v <- forAll genVector; n <- forAll genIndex; runST (do m <- MV.replicate (max 0 n) 0; r <- V.copy m v; fmap ((,) r) (V.freeze m)) === bool (Nothing, V.replicate n 0) (Just (), v) (max 0 n == V.length v)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
copy :: (PrimMonad m) => MVector (PrimState m) a -> Vector a -> m (Maybe ())
copy dst =
  G.copy dst
{-# INLINE copy #-}