HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulenonempty-vector-0.2.3Haskell2010

Data.Vector.NonEmpty

A library for non-empty boxed vectors (that is, polymorphic arrays capable of holding any Haskell value). Non-empty vectors come in two flavors:

  • mutable

  • immutable

This library attempts to provide support for all standard Vector operations in the API, with some slight variation in types and implementation. For example, since head and foldr are always gauranteed to be over a non-empty Vector, it is safe to make use of the 'unsafe-*' Vector operations and semigroupal folds available in the API in lieu of the standard implementations.

In contrast, some operations such as filter may "break out" of a NonEmptyVector due to the fact that there are no guarantees that may be made on the types of Bool-valued functions passed in, hence one could write the following:

filter (const false) v

which always produces an empty vector. Thus, some operations must return either a Maybe containing a NonEmptyVector or a Vector whenever appropriate. Generally The former is used in initialization and generation operations, and the latter is used in iterative operations where the intent is not to create an instance of NonEmptyVector.

Credit to Roman Leshchinskiy for the original Vector library upon which this is based.

  • 1 type
  • 204 values

Boxed non-empty vectors

1 declaration
newtypenewtype NonEmptyVector a
#

NonEmptyVector is a thin wrapper around Vector that witnesses an API requiring non-empty construction, initialization, and generation of non-empty vectors by design.

A newtype wrapper was chosen so that no new pointer indirection is introduced when working with Vectors, and all performance characteristics inherited from the Vector API still apply.

Instances18Monad, Functor, Applicative, Foldable, Traversable, MonadZip, …

Accessors

0 declarations

Length information

Indexing

valuehead :: NonEmptyVector a -> a
#

O(1) First element. Since head is gauranteed, bounds checks are bypassed by deferring to unsafeHead.

Example1 expression
head $ unsafeFromList [1..10]1
valuelast :: NonEmptyVector a -> a
#

O(1) Last element. Since a last element is gauranteed, bounds checks are bypassed by deferring to unsafeLast.

Example1 expression
last $ unsafeFromList [1..10]10
value(!) :: NonEmptyVector a -> Int -> a
#

O(1) Indexing.

Example1 expression
(unsafeFromList [1..10]) ! 01
value(!?) :: NonEmptyVector a -> Int -> Maybe a
#

O(1) Safe indexing.

Example1 expression
(unsafeFromList [1..10]) !? 0Just 1
Example1 expression
(unsafeFromList [1..10]) !? 11Nothing

Monadic Indexing

valueheadM :: Monad m => NonEmptyVector a -> m a
#

O(1) First element of a non-empty vector in a monad.

See indexM for an explanation of why this is useful.

Note that this function defers to unsafeHeadM since head is gauranteed to be safe by construction.

Example1 expression
headM @[] (unsafeFromList [1..10])[1]
valuelastM :: Monad m => NonEmptyVector a -> m a
#

O(1) Last element of a non-empty vector in a monad. See indexM for an explanation of why this is useful.

Note that this function defers to unsafeHeadM since a last element is gauranteed.

Example1 expression
lastM @[] (unsafeFromList [1..10])[10]
valueindexM :: Monad m => NonEmptyVector a -> Int -> m a
#

O(1) Indexing in a monad.

The monad allows operations to be strict in the non-empty vector when necessary.

See indexM for more details

Example1 expression
indexM @[] (unsafeFromList [1..10]) 3[4]

Extracting subvectors (slicing)

valuetail :: NonEmptyVector a -> Vector a
#

O(1) Yield all but the first element without copying. Since the vector returned may be empty (i.e. input was a singleton), this function returns a normal Vector

Example1 expression
tail (unsafeFromList [1..10])[2,3,4,5,6,7,8,9,10]
valueslice :: Int -> Int -> NonEmptyVector a -> Vector a
#

O(1) Yield a slice of the non-empty vector without copying it. The vector must contain at least i+n elements. Because this is not guaranteed, this function returns a Vector which could be empty

Example1 expression
slice 0 3 (unsafeFromList [1..10])[1,2,3]
valueinit :: NonEmptyVector a -> Vector a
#

O(1) Yield all but the last element without copying. Since the vector returned may be empty (i.e. input was a singleton), this function returns a normal Vector

Example1 expression
init (unsafeFromList [1..3])[1,2]
valuetake :: Int -> NonEmptyVector a -> Vector a
#

O(1) Yield at the first n elements without copying. The non-empty vector may contain less than n elements in which case it is returned as a vector unchanged.

Example1 expression
take 2 (unsafeFromList [1..3])[1,2]
valuedrop :: Int -> NonEmptyVector a -> Vector a
#

O(1) Yield all but the first n elements without copying. The non-empty vector may contain less than n elements in which case an empty vector is returned.

Example1 expression
drop 2 (unsafeFromList [1..3])[3]
valueuncons :: NonEmptyVector a -> (a, Vector a)
#

O(1) Yield a slice of a non-empty vector without copying at the 0th and 1st indices.

Example1 expression
uncons (unsafeFromList [1..10])(1,[2,3,4,5,6,7,8,9,10])
valueunsnoc :: NonEmptyVector a -> (Vector a, a)
#

O(1) Yield a slice of a non-empty vector without copying at the n-1th and nth indices

Example1 expression
unsnoc (unsafeFromList [1..10])([1,2,3,4,5,6,7,8,9],10)
valuesplitAt :: Int -> NonEmptyVector a -> (Vector a, Vector a)
#

O(1) Yield the first n elements paired with the remainder without copying.

This function returns a pair of vectors, as one may slice a (0, n+1).

Example1 expression
splitAt 2 (unsafeFromList [1..3])([1,2],[3])
valueunsafeTake :: Int -> NonEmptyVector a -> Vector a
#

O(1) Yield the first n elements without copying. The vector must contain at least n elements but this is not checked.

valueunsafeDrop :: Int -> NonEmptyVector a -> Vector a
#

O(1) Yield all but the first n elements without copying. The vector must contain at least n elements but this is not checked.

Construction

0 declarations

Initialization

valuesingleton :: a -> NonEmptyVector a
#

O(1) Non-empty vector with exactly one element

Example1 expression
singleton "a"["a"]
valuereplicate :: Int -> a -> Maybe (NonEmptyVector a)
#

O(n) Non-empty vector of the given length with the same value in each position.

When given a index n <= 0, then Nothing is returned, otherwise Just.

Example1 expression
replicate 3 "a"Just ["a","a","a"]
Example1 expression
replicate 0 "a"Nothing
valuereplicate1 :: Int -> a -> NonEmptyVector a
#

O(n) Non-empty vector of the given length with the same value in each position.

This variant takes max n 1 for the supplied length parameter.

Example1 expression
replicate1 3 "a"["a","a","a"]
Example1 expression
replicate1 0 "a"["a"]
Example1 expression
replicate1 (-1) "a"["a"]
valuegenerate :: Int -> (Int -> a) -> Maybe (NonEmptyVector a)
#

O(n) Construct a vector of the given length by applying the function to each index.

When given a index n <= 0, then Nothing is returned, otherwise Just.

Example1 expression
let f 0 = "a"; f _ = "k"; f :: Int -> String
Example1 expression
generate 1 fJust ["a"]
Example1 expression
generate 0 fNothing
Example1 expression
generate 2 fJust ["a","k"]
valuegenerate1 :: Int -> (Int -> a) -> NonEmptyVector a
#

O(n) Construct a vector of the given length by applying the function to each index.

This variant takes max n 1 for the supplied length parameter.

Example1 expression
let f 0 = "a"; f _ = "k"; f :: Int -> String
Example1 expression
generate1 2 f["a","k"]
Example1 expression
generate1 0 f["a"]
Example1 expression
generate1 (-1) f["a"]
valueiterateN :: Int -> (a -> a) -> a -> Maybe (NonEmptyVector a)
#

O(n) Apply function n times to value. Zeroth element is original value.

When given a index n <= 0, then Nothing is returned, otherwise Just.

Example1 expression
iterateN 3 (+1) 0Just [0,1,2]
Example1 expression
iterateN 0 (+1) 0Nothing
Example1 expression
iterateN (-1) (+1) 0Nothing
valueiterateN1 :: Int -> (a -> a) -> a -> NonEmptyVector a
#

O(n) Apply function n times to value. Zeroth element is original value.

This variant takes max n 1 for the supplied length parameter.

Example1 expression
iterateN1 3 (+1) 0[0,1,2]
Example1 expression
iterateN1 0 (+1) 0[0]
Example1 expression
iterateN1 (-1) (+1) 0[0]

Monad Initialization

valuereplicateM :: Monad m => Int -> m a -> m (Maybe (NonEmptyVector a))
#

O(n) Execute the monadic action the given number of times and store the results in a vector.

When given a index n <= 0, then Nothing is returned, otherwise Just.

Example1 expression
replicateM @Maybe 3 (Just "a")Just (Just ["a","a","a"])
Example1 expression
replicateM @Maybe 3 NothingNothing
Example1 expression
replicateM @Maybe 0 (Just "a")Just Nothing
Example1 expression
replicateM @Maybe (-1) (Just "a")Just Nothing
valuereplicate1M :: Monad m => Int -> m a -> m (NonEmptyVector a)
#

O(n) Execute the monadic action the given number of times and store the results in a vector.

This variant takes max n 1 for the supplied length parameter.

Example1 expression
replicate1M @Maybe 3 (Just "a")Just ["a","a","a"]
Example1 expression
replicate1M @Maybe 3 NothingNothing
Example1 expression
replicate1M @Maybe 0 (Just "a")Just ["a"]
Example1 expression
replicate1M @Maybe (-1) (Just "a")Just ["a"]
valuegenerateM :: Monad m => Int -> (Int -> m a) -> m (Maybe (NonEmptyVector a))
#

O(n) Construct a vector of the given length by applying the monadic action to each index

When given a index n <= 0, then Nothing is returned, otherwise Just.

Example1 expression
generateM 3 (\i -> if i P.< 1 then ["a"] else ["b"])[Just ["a","b","b"]]
Example1 expression
generateM @[] @Int 3 (const [])[]
Example1 expression
generateM @[] @Int 0 (const [1])[Nothing]
Example1 expression
generateM @Maybe @Int (-1) (const Nothing)Just Nothing
valuegenerate1M :: Monad m => Int -> (Int -> m a) -> m (NonEmptyVector a)
#

O(n) Construct a vector of the given length by applying the monadic action to each index

This variant takes max n 1 for the supplied length parameter.

Example1 expression
generate1M 3 (\i -> if i P.< 1 then Just "a" else Just "b")Just ["a","b","b"]
Example1 expression
generate1M 3 (const [])[]
Example1 expression
generate1M 0 (const $ Just 1)Just [1]
Example1 expression
generate1M (-1) (const Nothing)Nothing
valueiterateNM
  1. :: Monad m
  2. => Int
  3. -> a -> m a
  4. -> a
  5. -> m (Maybe (NonEmptyVector a))
#

O(n) Apply monadic function n times to value. Zeroth element is original value.

When given a index n <= 0, then Nothing is returned, otherwise Just.

Example1 expression
iterateNM @Maybe 3 return "a"Just (Just ["a","a","a"])
Example1 expression
iterateNM @Maybe 3 (const Nothing) "a"Nothing
Example1 expression
iterateNM @Maybe 0 return "a"Just Nothing
valueiterateN1M :: Monad m => Int -> (a -> m a) -> a -> m (NonEmptyVector a)
#

O(n) Apply monadic function n times to value. Zeroth element is original value.

This variant takes max n 1 for the supplied length parameter.

Example1 expression
iterateN1M @Maybe 3 return "a"Just ["a","a","a"]
Example1 expression
iterateN1M @Maybe 3 (const Nothing) "a"Nothing
Example1 expression
iterateN1M @Maybe 0 return "a"Just ["a"]
Example1 expression
iterateN1M @Maybe (-1) return "a"Just ["a"]
valueunsafeCreate :: (forall s. ST s (MVector s a)) -> NonEmptyVector a
#

Execute the monadic action and freeze the resulting non-empty vector, bypassing emptiness checks.

The onus is on the caller to guarantee the created vector is non-empty.

Unfolding

valueunfoldr :: (b -> Maybe (a, b)) -> b -> Maybe (NonEmptyVector a)
#

O(n) Construct a non-empty vector by repeatedly applying the generator function to a seed. The generator function yields Just the next element and the new seed or Nothing if there are no more elements.

If an unfold does not create meaningful values, Nothing is returned. Otherwise, Just containing a non-empty vector is returned.

Example1 expression
unfoldr (\b -> case b of "a" -> Just ("a", "b"); _ ->  Nothing) "a"Just ["a"]
Example1 expression
unfoldr (const Nothing) "a"Nothing
valueunfoldr1 :: (b -> Maybe (a, b)) -> a -> b -> NonEmptyVector a
#

O(n) Construct a non-empty vector by repeatedly applying the generator function to a seed and a first element.

This variant of unfoldr guarantees the resulting vector is non- empty by supplying an initial element a.

Example1 expression
unfoldr1 (\b -> case b of "a" -> Just ("a", "b"); _ ->  Nothing) "first" "a"["first","a"]
Example1 expression
unfoldr1 (const Nothing) "first" "a"["first"]
valueunfoldrN :: Int -> (b -> Maybe (a, b)) -> b -> Maybe (NonEmptyVector a)
#

O(n) Construct a vector with at most n elements by repeatedly applying the generator function to a seed. The generator function yields Just the next element and the new seed or Nothing if there are no more elements.

If an unfold does not create meaningful values, Nothing is returned. Otherwise, Just containing a non-empty vector is returned.

Example1 expression
unfoldrN 3 (\b -> Just (b+1, b+1)) 0Just [1,2,3]
Example1 expression
unfoldrN 3 (const Nothing) 0Nothing
Example1 expression
unfoldrN 0 (\b -> Just (b+1, b+1)) 0Nothing
valueunfoldr1N :: Int -> (b -> Maybe (a, b)) -> a -> b -> NonEmptyVector a
#

O(n) Construct a vector with at most n elements by repeatedly applying the generator function to a seed. The generator function yields Just the next element and the new seed or Nothing if there are no more elements.

This variant of unfoldrN guarantees the resulting vector is non- empty by supplying an initial element a.

Example1 expression
unfoldr1N 3 (\b -> Just (b+1, b+1)) 0 0[0,1,2,3]
Example1 expression
unfoldr1N 3 (const Nothing) 0 0[0]
Example1 expression
unfoldr1N 0 (\b -> Just (b+1, b+1)) 0 0[0]
valueunfoldrM
  1. :: Monad m
  2. => b -> m (Maybe (a, b))
  3. -> b
  4. -> m (Maybe (NonEmptyVector a))
#

O(n) Construct a non-empty vector by repeatedly applying the monadic generator function to a seed. The generator function yields Just the next element and the new seed or Nothing if there are no more elements.

If an unfold does not create meaningful values, Nothing is returned. Otherwise, Just containing a non-empty vector is returned.

valueunfoldr1M
  1. :: Monad m
  2. => b -> m (Maybe (a, b))
  3. -> a
  4. -> b
  5. -> m (NonEmptyVector a)
#

O(n) Construct a non-empty vector by repeatedly applying the monadic generator function to a seed. The generator function yields Just the next element and the new seed or Nothing if there are no more elements.

This variant of unfoldrM guarantees the resulting vector is non- empty by supplying an initial element a.

valueunfoldrNM
  1. :: Monad m
  2. => Int
  3. -> b -> m (Maybe (a, b))
  4. -> b
  5. -> m (Maybe (NonEmptyVector a))
#

O(n) Construct a non-empty vector by repeatedly applying the monadic generator function to a seed. The generator function yields Just the next element and the new seed or Nothing if there are no more elements.

If an unfold does not create meaningful values, Nothing is returned. Otherwise, Just containing a non-empty vector is returned.

valueunfoldr1NM
  1. :: Monad m
  2. => Int
  3. -> b -> m (Maybe (a, b))
  4. -> a
  5. -> b
  6. -> m (NonEmptyVector a)
#

O(n) Construct a non-empty vector by repeatedly applying the monadic generator function to a seed. The generator function yields Just the next element and the new seed or Nothing if there are no more elements.

This variant of unfoldrNM guarantees the resulting vector is non- empty by supplying an initial element a.

valueconstructN :: Int -> (Vector a -> a) -> Maybe (NonEmptyVector a)
#

O(n) Construct a non-empty vector with n elements by repeatedly applying the generator function to the already constructed part of the vector.

If constructN does not create meaningful values, Nothing is returned. Otherwise, Just containing a non-empty vector is returned.

valueconstructrN :: Int -> (Vector a -> a) -> Maybe (NonEmptyVector a)
#

O(n) Construct a vector with n elements from right to left by repeatedly applying the generator function to the already constructed part of the vector.

If constructrN does not create meaningful values, Nothing is returned. Otherwise, Just containing a non-empty vector is returned.

Enumeration

valueenumFromN :: Num a => a -> Int -> Maybe (NonEmptyVector a)
#

O(n) Yield a non-emptyvector of the given length containing the values x, x+1 etc. This operation is usually more efficient than enumFromTo.

If an enumeration does not use meaningful indices, Nothing is returned, otherwise, Just containing a non-empty vector.

valueenumFromStepN :: Num a => a -> a -> Int -> Maybe (NonEmptyVector a)
#

O(n) Yield a non-empty vector of the given length containing the values x, x+y, x+y+y etc. This operations is usually more efficient than enumFromThenTo.

If an enumeration does not use meaningful indices, Nothing is returned, otherwise, Just containing a non-empty vector.

valueenumFromTo :: Enum a => a -> a -> Maybe (NonEmptyVector a)
#

O(n) Enumerate values from x to y.

If an enumeration does not use meaningful indices, Nothing is returned, otherwise, Just containing a non-empty vector.

WARNING: This operation can be very inefficient. If at all possible, use enumFromN instead.

valueenumFromThenTo :: Enum a => a -> a -> a -> Maybe (NonEmptyVector a)
#

O(n) Enumerate values from x to y with a specific step z.

If an enumeration does not use meaningful indices, Nothing is returned, otherwise, Just containing a non-empty vector.

WARNING: This operation can be very inefficient. If at all possible, use enumFromStepN instead.

Concatenation

valueconsV :: a -> Vector a -> NonEmptyVector a
#

O(n) Prepend an element to a Vector

Example1 expression
consV 1 (V.fromList [2,3])[1,2,3]
valuesnocV :: Vector a -> a -> NonEmptyVector a
#

O(n) Append an element to a Vector

Example1 expression
snocV (V.fromList [1,2]) 3[1,2,3]
valueconcat :: [NonEmptyVector a] -> Maybe (NonEmptyVector a)
#

O(n) Concatenate all non-empty vectors in the list

If list is empty, Nothing is returned, otherwise Just containing the concatenated non-empty vectors

Example1 expression
concat [(unsafeFromList [1..3]), (unsafeFromList [4..6])]Just [1,2,3,4,5,6]

O(n) Concatenate all non-empty vectors in a non-empty list.

Example1 expression
concat1 ((unsafeFromList [1..3]) :| [(unsafeFromList [4..6])])[1,2,3,4,5,6]

Restricting memory usage

Conversion

0 declarations

To/from non-empty lists

valuetoNonEmpty :: NonEmptyVector a -> NonEmpty a
#

O(n) Convert a non-empty vector to a non-empty list.

Example1 expression
toNonEmpty (unsafeFromList [1..3])1 :| [2,3]

O(n) Convert from the first n-elements of a non-empty list to a non-empty vector.

Returns Nothing if indices are <= 0, otherwise Just containing the non-empty vector.

Example1 expression
fromNonEmptyN 3 (1 :| [2..5])Just [1,2,3]
Example1 expression
fromNonEmptyN 0 (1 :| [2..5])Nothing

O(n) Convert from the first n-elements of a non-empty list to a non-empty vector. This is a safe version of fromNonEmptyN which takes max n 1 of the first n-elements of the non-empty list.

Example1 expression
fromNonEmptyN1 3 (1 :| [2..5])[1,2,3]
Example1 expression
fromNonEmptyN1 0 (1 :| [2..5])[1]
valueunsafeFromList :: [a] -> NonEmptyVector a
#

O(n) Convert from a list to a non-empty vector.

Warning: the onus is on the user to ensure that their vector is not empty, otherwise all bets are off!

Example1 expression
unsafeFromList [1..3][1,2,3]

To/from vector

valuetoVector :: NonEmptyVector a -> Vector a
#

O(1) Convert from a non-empty vector to a vector.

Example1 expression
let nev :: NonEmptyVector Int = unsafeFromList [1..3] in toVector nev[1,2,3]
valuefromVector :: Vector a -> Maybe (NonEmptyVector a)
#

O(1) Convert from a vector to a non-empty vector.

If the vector is empty, then Nothing is returned, otherwise Just containing the non-empty vector.

Example1 expression
fromVector $ V.fromList [1..3]Just [1,2,3]
Example1 expression
fromVector $ V.fromList []Nothing

O(1) Convert from a vector to a non-empty vector without checking bounds.

Warning: the onus is on the user to ensure that their vector is not empty, otherwise all bets are off!

Example1 expression
unsafeFromVector $ V.fromList [1..3][1,2,3]

To/from list

valuetoList :: NonEmptyVector a -> [a]
#

O(n) Convert from a non-empty vector to a list.

Example1 expression
let nev :: NonEmptyVector Int = unsafeFromList [1..3] in toList nev[1,2,3]
valuefromList :: [a] -> Maybe (NonEmptyVector a)
#

O(n) Convert from a list to a non-empty vector.

Example1 expression
fromList [1..3]Just [1,2,3]
Example1 expression
fromList []Nothing
valuefromListN :: Int -> [a] -> Maybe (NonEmptyVector a)
#

O(n) Convert the first n elements of a list to a non-empty vector.

If the list is empty or <= 0 elements are chosen, Nothing is returned, otherwise Just containing the non-empty vector

Example1 expression
fromListN 3 [1..5]Just [1,2,3]
Example1 expression
fromListN 3 []Nothing
Example1 expression
fromListN 0 [1..5]Nothing

Modifying non-empty vectors

0 declarations

Bulk Updates

value(//) :: NonEmptyVector a -> [(Int, a)] -> NonEmptyVector a
#

O(m+n) For each pair (i,a) from the list, replace the non-empty vector element at position i by a.

Example1 expression
unsafeFromList [1..3] // [(2,4)][1,2,4]
Example1 expression
unsafeFromList [1..3] // [][1,2,3]
valueupdate :: NonEmptyVector a -> Vector (Int, a) -> NonEmptyVector a
#

O(m+n) For each pair (i,a) from the vector of index/value pairs, replace the vector element at position i by a.

Example1 expression
unsafeFromList [1..3] `update` V.fromList [(2,4)][1,2,4]
Example1 expression
unsafeFromList [1..3] `update` V.empty[1,2,3]

O(m+min(n1,n2)) 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.

Example1 expression
update_ (unsafeFromList [1..3]) (V.fromList [2]) (V.fromList [4])[1,2,4]
Example1 expression
update_ (unsafeFromList [1..3]) V.empty V.empty[1,2,3]

Accumulations

6 declarations
valueaccum
  1. :: (a -> b -> a)

    accumulating function f

  2. -> NonEmptyVector a

    initial non-empty vector (of length m)

  3. -> [(Int, b)]

    list of index/value pairs (of length n)

  4. -> NonEmptyVector a
#

O(m+n) For each pair (i,b) from the non-empty list, replace the non-empty vector element a at position i by f a b.

Example1 expression
accum (+) (unsafeFromList [1..3]) [(2,10)][1,2,13]
Example1 expression
accum (+) (unsafeFromList [1..3]) [][1,2,3]
valueaccumulate
  1. :: (a -> b -> a)

    accumulating function f

  2. -> NonEmptyVector a

    initial non-empty vector (of length m)

  3. -> Vector (Int, b)

    vector of index/value pairs (of length n)

  4. -> NonEmptyVector a
#

O(m+n) For each pair (i,b) from the vector of pairs, replace the non-empty vector element a at position i by f a b.

Example1 expression
accumulate (+) (unsafeFromList [1..3]) (V.fromList [(2,10)])[1,2,13]
Example1 expression
accumulate (+) (unsafeFromList [1..3]) V.empty[1,2,3]
valueaccumulate_
  1. :: (a -> b -> a)

    accumulating function f

  2. -> NonEmptyVector a

    initial non-empty vector (of length m)

  3. -> Vector Int

    vector of indices (of length n1)

  4. -> Vector b

    vector of values (of length n2)

  5. -> NonEmptyVector a
#

O(m+min(n1,n2)) For each index i from the index vector and the corresponding value b from the the value vector, replace the element of the initial non-empty vector at position i by f a b.

Example1 expression
accumulate_ (+) (unsafeFromList [1..3]) (V.fromList [2]) (V.fromList [10])[1,2,13]
Example1 expression
accumulate_ (+) (unsafeFromList [1..3]) V.empty V.empty[1,2,3]

Permutations

3 declarations

O(n) Yield the non-empty vector obtained by replacing each element i of the non-empty index vector by xs!i. This is equivalent to map (xs!) is but is often much more efficient.

Example1 expression
backpermute (unsafeFromList [1..3]) (unsafeFromList [2,0])[3,1]

Safe destructive updates

1 declaration
valuemodify
  1. :: forall s. MVector s a -> ST s ()
  2. -> NonEmptyVector a
  3. -> NonEmptyVector a
#

Apply a destructive operation to a non-empty vector. The operation will be performed in place if it is safe to do so and will modify a copy of the non-empty vector otherwise.

Elementwise operations

0 declarations

Indexing

valueindexed :: NonEmptyVector a -> NonEmptyVector (Int, a)
#

O(n) Pair each element in a vector with its index.

Example1 expression
indexed $ unsafeFromList ["a","b","c"][(0,"a"),(1,"b"),(2,"c")]

Mapping

valuemap :: (a -> b) -> NonEmptyVector a -> NonEmptyVector b
#

O(n) Map a function over a non-empty vector.

Example1 expression
map (+1) $ unsafeFromList [1..3][2,3,4]
valueimap :: (Int -> a -> b) -> NonEmptyVector a -> NonEmptyVector b
#

O(n) Apply a function to every element of a non-empty vector and its index.

Example1 expression
imap (\i a -> if i == 2 then a+1 else a+0) $ unsafeFromList [1..3][1,2,4]

Monadic mapping

valuemapM :: Monad m => (a -> m b) -> NonEmptyVector a -> m (NonEmptyVector b)
#

O(n) Apply the monadic action to all elements of the non-empty vector, yielding non-empty vector of results.

Example1 expression
mapM Just (unsafeFromList [1..3])Just [1,2,3]
Example1 expression
mapM (const Nothing) (unsafeFromList [1..3])Nothing
valueimapM
  1. :: Monad m
  2. => Int -> a -> m b
  3. -> NonEmptyVector a
  4. -> m (NonEmptyVector b)
#

O(n) Apply the monadic action to every element of a non-empty vector and its index, yielding a non-empty vector of results.

Example1 expression
imapM (\i a -> if i == 1 then Just a else Just 0) (unsafeFromList [1..3])Just [0,2,0]
Example1 expression
imapM (\_ _ -> Nothing) (unsafeFromList [1..3])Nothing
valuemapM_ :: Monad m => (a -> m b) -> NonEmptyVector a -> m ()
#

O(n) Apply the monadic action to all elements of a non-empty vector and ignore the results.

Example1 expression
mapM_ (const $ Just ()) (unsafeFromList [1..3])Just ()
Example1 expression
mapM_ (const Nothing) (unsafeFromList [1..3])Nothing
valueimapM_ :: Monad m => (Int -> a -> m b) -> NonEmptyVector a -> m ()
#

O(n) Apply the monadic action to every element of a non-emptpy vector and its index, ignoring the results

Example1 expression
imapM_ (\i a -> if i == 1 then P.print a else P.putStrLn "0") (unsafeFromList [1..3])020
Example1 expression
imapM_ (\_ _ -> Nothing) (unsafeFromList [1..3])Nothing
valueforM :: Monad m => NonEmptyVector a -> (a -> m b) -> m (NonEmptyVector b)
#

O(n) Apply the monadic action to all elements of the non-empty vector, yielding a non0empty vector of results.

Equivalent to flip mapM.

valueforM_ :: Monad m => NonEmptyVector a -> (a -> m b) -> m ()
#

O(n) Apply the monadic action to all elements of a non-empty vector and ignore the results.

Equivalent to flip mapM_.

Zipping

Monadic Zipping

Unzipping

Working with predicates

0 declarations

Filtering

valuemapMaybe :: (a -> Maybe b) -> NonEmptyVector a -> Vector b
#

O(n) Drop elements when predicate returns Nothing

If no elements satisfy the predicate, the resulting vector may be empty.

Example1 expression
mapMaybe (\a -> if a == 2 then Nothing else Just a) (unsafeFromList [1..3])[1,3]
valueimapMaybe :: (Int -> a -> Maybe b) -> NonEmptyVector a -> Vector b
#

O(n) Drop elements when predicate, applied to index and value, returns Nothing

If no elements satisfy the predicate, the resulting vector may be empty.

Example1 expression
imapMaybe (\i a -> if a == 2 || i == 2 then Nothing else Just a) (unsafeFromList [1..3])[1]
valuefilter :: (a -> Bool) -> NonEmptyVector a -> Vector a
#

O(n) Drop elements that do not satisfy the predicate.

If no elements satisfy the predicate, the resulting vector may be empty.

Example1 expression
filter (\a -> if a == 2 then False else True) (unsafeFromList [1..3])[1,3]
Example1 expression
filter (const False) (unsafeFromList [1..3])[]
valueifilter :: (Int -> a -> Bool) -> NonEmptyVector a -> Vector a
#

O(n) Drop elements that do not satisfy the predicate which is applied to values and their indices.

If no elements satisfy the predicate, the resulting vector may be empty.

Example1 expression
ifilter (\i a -> if a == 2 || i == 0 then False else True) (unsafeFromList [1..3])[3]
Example1 expression
ifilter (\_ _ -> False) (unsafeFromList [1..3])[]
valuefilterM :: Monad m => (a -> m Bool) -> NonEmptyVector a -> m (Vector a)
#

O(n) Drop elements that do not satisfy the monadic predicate.

If no elements satisfy the predicate, the resulting vector may be empty.

Example1 expression
filterM (\a -> if a == 2 then Just False else Just True) (unsafeFromList [1..3])Just [1,3]
Example1 expression
filterM (\a -> if a == 2 then Nothing else Just True) (unsafeFromList [1..3])Nothing
Example1 expression
filterM (const $ Just False) (unsafeFromList [1..3])Just []
valueifilterM
  1. :: Monad m
  2. => Int -> a -> m Bool
  3. -> NonEmptyVector a
  4. -> m (Vector a)
#

O(n) Drop elements that do not satisfy the monadic predicate that is a function of index and value.

If no elements satisfy the predicate, the resulting vector may be empty.

TODO: this should be a more efficient function in vector.

Example1 expression
ifilterM (\i a -> if a == 2 || i == 0 then Just False else Just True) (unsafeFromList [1..3])Just [3]
Example1 expression
ifilterM (\i a -> if a == 2 || i == 0 then Nothing else Just True) (unsafeFromList [1..3])Nothing
Example1 expression
ifilterM (\_ _ -> Just False) (unsafeFromList [1..3])Just []
valuetakeWhile :: (a -> Bool) -> NonEmptyVector a -> Vector a
#

O(n) Yield the longest prefix of elements satisfying the predicate without copying.

If no elements satisfy the predicate, the resulting vector may be empty.

Example1 expression
takeWhile (/= 3) (unsafeFromList [1..3])[1,2]
valuedropWhile :: (a -> Bool) -> NonEmptyVector a -> Vector a
#

O(n) Drop the longest prefix of elements that satisfy the predicate without copying.

If all elements satisfy the predicate, the resulting vector may be empty.

Example1 expression
dropWhile (/= 3) (unsafeFromList [1..3])[3]

Partitioning

5 declarations
valuepartition :: (a -> Bool) -> NonEmptyVector a -> (Vector a, Vector a)
#

O(n) Split the non-empty vector in two parts, the first one containing those elements that satisfy the predicate and the second one those that don't. The relative order of the elements is preserved at the cost of a sometimes reduced performance compared to unstablePartition.

If all or no elements satisfy the predicate, one of the resulting vectors may be empty.

Example1 expression
partition (< 3) (unsafeFromList [1..5])([1,2],[3,4,5])
valuepartitionWith
  1. :: a -> Either b c
  2. -> NonEmptyVector a
  3. -> (Vector b, Vector c)
#

O(n) Split the non-empty vector in two parts, the first one containing the Left elements and the second containing the Right elements. The relative order of the elements is preserved.

If all elements produce a Left (or Right), one of the resulting vectors may be empty.

Example1 expression
partitionWith (\a -> if a < 3 then Left a else Right (P.show a)) (unsafeFromList [1..5])([1,2],["3","4","5"])
valueunstablePartition :: (a -> Bool) -> NonEmptyVector a -> (Vector a, Vector a)
#

O(n) Split the non-empty vector in two parts, the first one containing those elements that satisfy the predicate and the second one those that don't. The order of the elements is not preserved but the operation is often faster than partition.

If all or no elements satisfy the predicate, one of the resulting vectors may be empty.

valuespan :: (a -> Bool) -> NonEmptyVector a -> (Vector a, Vector a)
#

O(n) Split the non-empty vector into the longest prefix of elements that satisfy the predicate and the rest without copying.

If all or no elements satisfy the predicate, one of the resulting vectors may be empty.

Example1 expression
span (== 1) (unsafeFromList [1,1,2,3,1])([1,1],[2,3,1])
valuebreak :: (a -> Bool) -> NonEmptyVector a -> (Vector a, Vector a)
#

O(n) Split the vector into the longest prefix of elements that do not satisfy the predicate and the rest without copying.

If all or no elements satisfy the predicate, one of the resulting vectors may be empty.

Example1 expression
break (== 2) (unsafeFromList [1,1,2,3,1])([1,1],[2,3,1])

Searching

7 declarations
valueelem :: Eq a => a -> NonEmptyVector a -> Bool
#

O(n) Check if the non-empty vector contains an element

Example2 expressions
elem 1 $ unsafeFromList [1..3]Trueelem 4 $ unsafeFromList [1..3]False
valuenotElem :: Eq a => a -> NonEmptyVector a -> Bool
#

O(n) Check if the non-empty vector does not contain an element (inverse of elem)

Example1 expression
notElem 1 $ unsafeFromList [1..3]False
Example1 expression
notElem 4 $ unsafeFromList [1..3]True
valuefind :: (a -> Bool) -> NonEmptyVector a -> Maybe a
#

O(n) Yield Just the first element matching the predicate or Nothing if no such element exists.

Example1 expression
find (< 2) $ unsafeFromList [1..3]Just 1
Example1 expression
find (< 0) $ unsafeFromList [1..3]Nothing
valuefindIndex :: (a -> Bool) -> NonEmptyVector a -> Maybe Int
#

O(n) Yield Just the index of the first element matching the predicate or Nothing if no such element exists.

Example1 expression
findIndex (< 2) $ unsafeFromList [1..3]Just 0
Example1 expression
findIndex (< 0) $ unsafeFromList [1..3]Nothing
valuefindIndices :: (a -> Bool) -> NonEmptyVector a -> Vector Int
#

O(n) Yield the indices of elements satisfying the predicate in ascending order.

Example1 expression
findIndices (< 3) $ unsafeFromList [1..3][0,1]
Example1 expression
findIndices (< 0) $ unsafeFromList [1..3][]
valueelemIndex :: Eq a => a -> NonEmptyVector a -> Maybe Int
#

O(n) Yield Just the index of the first occurence of the given element or Nothing if the non-empty vector does not contain the element. This is a specialised version of findIndex.

Example1 expression
elemIndex 1 $ unsafeFromList [1..3]Just 0
Example1 expression
elemIndex 0 $ unsafeFromList [1..3]Nothing
valueelemIndices :: Eq a => a -> NonEmptyVector a -> Vector Int
#

O(n) Yield the indices of all occurences of the given element in ascending order. This is a specialised version of findIndices.

Example1 expression
elemIndices 1 $ unsafeFromList [1,2,3,1][0,3]
Example1 expression
elemIndices 0 $ unsafeFromList [1..3][]

Folding

12 declarations
valueifoldl :: (a -> Int -> b -> a) -> a -> NonEmptyVector b -> a
#

O(n) Left monoidal fold with function applied to each element and its index

valueifoldl' :: (a -> Int -> b -> a) -> a -> NonEmptyVector b -> a
#

O(n) Strict left monoidal fold with function applied to each element and its index

valueifoldr :: (Int -> a -> b -> b) -> b -> NonEmptyVector a -> b
#

O(n) Right monoidal fold with function applied to each element and its index

valueifoldr' :: (Int -> a -> b -> b) -> b -> NonEmptyVector a -> b
#

O(n) strict right monoidal fold with function applied to each element and its index

Specialized folds

14 declarations
valuemaximumBy :: (a -> a -> Ordering) -> NonEmptyVector a -> a
#

O(n) Yield the maximum element of a non-empty vector according to the given comparison function.

valueminimumBy :: (a -> a -> Ordering) -> NonEmptyVector a -> a
#

O(n) Yield the minimum element of the non-empty vector according to the given comparison function.

Monadic Folds

12 declarations
valueifoldM :: Monad m => (a -> Int -> b -> m a) -> a -> NonEmptyVector b -> m a
#

O(n) Monadic fold (action applied to each element and its index)

valueifoldM' :: Monad m => (a -> Int -> b -> m a) -> a -> NonEmptyVector b -> m a
#

O(n) Strict monadic fold (action applied to each element and its index)

valueifoldM_
  1. :: Monad m
  2. => a -> Int -> b -> m a
  3. -> a
  4. -> NonEmptyVector b
  5. -> m ()
#

O(n) Monadic fold that discards the result (action applied to each element and its index)

valueifoldM'_
  1. :: Monad m
  2. => a -> Int -> b -> m a
  3. -> a
  4. -> NonEmptyVector b
  5. -> m ()
#

O(n) Strict monadic fold that discards the result (action applied to each element and its index)

Monadic Sequencing

2 declarations

Prefix sums (scans)

20 declarations