Type synonym for a single dimension array, or simply a flat vector.
Modulemassiv-1.0.5.0Haskell2010
Data.Massiv.Vector
- 2 types
- 164 values
- Packagemassiv-1.0.5.0
- Exports170
- LanguageHaskell2010
- LicenceBSD-3-Clause
- SourceCommon.hs
Type synonym for a single dimension mutable array, or simply a flat mutable vector.
Accessors
0 declarationsSize
O(1) - Get the length of a Stream array, but only if it is known exactly in constant time without looking at any of the elements in the array.
Related: maxLinearSize, size, elemsCount and totalElem
Examples
slength $ sfromList []Nothingslength $ sreplicate 5 ()Just (Sz1 5)slength $ makeArrayLinearR D Seq (Sz1 5) idJust (Sz1 5)slength $ sunfoldr (\x -> Just (x, x)) (0 :: Int)Nothingslength $ sunfoldrN 10 (\x -> Just (x, x)) (0 :: Int)Nothingslength $ sunfoldrExactN 10 (\x -> (x, x)) (0 :: Int)Just (Sz1 10)
Similar:
Data.Foldable.lengthFor some data structures, like a list for example, it is an
O(n)
operation, because there is a need to evaluate the full spine and possibly even the elements in order to get the full length. With
vectors that is not always the case.
Data.Vector.Generic.lengthIn the vector package this function will always break fusion, unless it is the only operation that is applied to the vector.
O(1) - Get the possible maximum linear size of an immutabe array. If the lookup of size in constant time is not possible, Nothing will be returned. This value will be used as the initial size of the mutable array into which the loading will happen.
O(1) - Get the exact size of an immutabe array. Most of the time will
produce the size in constant time, except for Data.Massiv.Array.DS
representation, which could result in evaluation of the whole stream. See
maxLinearSize and slength for more info.
O(1) - Check whether an array is empty or not.
Examples
import Data.Massiv.ArrayisNull $ range Seq (Ix2 10 20) (11 :. 21)FalseisNull $ range Seq (Ix2 10 20) (10 :. 21)TrueisNull (empty :: Array D Ix5 Int)TrueisNull $ sfromList []True
O(1) - Check if array has elements.
Examples
import Data.Massiv.ArrayisNotNull (singleton 1 :: Array D Ix2 Int)TrueisNotNull (empty :: Array D Ix2 Int)False
Indexing
O(1) - Infix version of indexM.
Exceptions: IndexOutOfBoundsException
Examples
import Data.Massiv.Array as A:set -XTypeApplicationsa <- fromListsM @U @Ix2 @Int Seq [[1,2,3],[4,5,6]]aArray U Seq (Sz (2 :. 3)) [ [ 1, 2, 3 ] , [ 4, 5, 6 ] ]a !? 0 :. 23a !? 0 :. 3*** Exception: IndexOutOfBoundsException: (0 :. 3) is not safe for (Sz (2 :. 3))a !? 0 :. 3 :: Maybe IntNothing
O(1) - Infix version of index'.
Examples
import Data.Massiv.Array as Aa = computeAs U $ iterateN (Sz (2 :. 3)) succ (0 :: Int)aArray U Seq (Sz (2 :. 3)) [ [ 1, 2, 3 ] , [ 4, 5, 6 ] ]a ! 0 :. 23
O(1) - Lookup an element in the array. This is a partial function and it will throw an error when index is out of bounds. It is safer to use indexM instead.
Examples
import Data.Massiv.Array:set -XOverloadedListsxs = [0..100] :: Array U Ix1 Intindex' xs 5050
O(1) - Get the first element of a Source vector. Throws an error on empty.
Related: shead', headM, sheadM, unconsM.
Examples
head' (Ix1 10 ..: 10000000000000)10
Similar:
Data.List.Data.List.headAlso constant time and partial. Fusion is broken if there other consumers of the list.
Data.Vector.Generic.headAlso constant time and partial. Will cause materialization of the full vector if any other function is applied to the vector.
O(1) - Get the last element of a Source vector. Throws an error on empty.
Examples
last' (Ix1 10 ... 10000000000000)10000000000000
Similar:
Data.List.Data.List.lastAlso partial, but it has
O(n)
complexity. Fusion is broken if there other consumers of the list.
Data.Vector.Generic.lastAlso constant time and partial. Will cause materialization of the full vector if any other function is applied to the vector.
Monadic Indexing
O(1) - Lookup an element in the array.
Exceptions: IndexOutOfBoundsException
O(1) - Get the first element of a Source vector.
Related: head', shead', sheadM, unconsM.
Throws Exceptions: SizeEmptyException when array is empty
Examples
headM (Ix1 10 ..: 10000000000000)10headM (Ix1 10 ..: 10000000000000) :: Maybe IntJust 10headM (empty :: Array D Ix1 Int) :: Maybe IntNothingeither show (const "") $ headM (Ix1 10 ..: 10)"SizeEmptyException: (Sz1 0) corresponds to an empty array"
Similar:
Data.Maybe.listToMaybeIt also a safe way to get the head of the list, except it is restricted to
O(1) - Get the first element of a Stream vector.
Related: head', shead', headM, unconsM.
Throws Exceptions: SizeEmptyException
Examples
maybe 101 id $ sheadM (empty :: Vector D Int)101maybe 101 id $ sheadM (singleton 202 :: Vector D Int)202sheadM $ sunfoldr (\x -> Just (x, x)) (0 :: Int)0x <- sheadM $ sunfoldr (\_ -> Nothing) (0 :: Int)*** Exception: SizeEmptyException: (Sz1 0) corresponds to an empty array
O(1) - Get the last element of a Source vector.
Throws Exceptions: SizeEmptyException
Examples
lastM (Ix1 10 ... 10000000000000)10000000000000lastM (Ix1 10 ... 10000000000000) :: Maybe IntJust 10000000000000either show (const "") $ lastM (fromList Seq [] :: Array P Ix1 Int)"SizeEmptyException: (Sz1 0) corresponds to an empty array"
O(1) - Take one element off of the Source vector from the left side, as well as the remaining part of the vector in delayed D representation.
Related: head', shead', headM, sheadM, cons
Throws Exceptions: SizeEmptyException
Examples
unconsM (fromList Seq [1,2,3] :: Array P Ix1 Int)(1,Array P Seq (Sz1 2) [ 2, 3 ])
Similar:
Data.List.Data.List.unconsSame concept, except it is restricted to
instead of the more general
O(1) - Take one element off of the vector from the right side, as well as the remaining part of the vector.
Throws Exceptions: SizeEmptyException
Examples
unsnocM (fromList Seq [1,2,3] :: Array P Ix1 Int)(Array P Seq (Sz1 2) [ 1, 2 ],3)
Slicing
O(1) - Take a slice of a Source vector. Never fails, instead adjusts the indices.
Examples
slice 10 5 (Ix1 0 ... 10000000000000)Array D Seq (Sz1 5) [ 10, 11, 12, 13, 14 ]slice (-10) 5 (Ix1 0 ... 10000000000000)Array D Seq (Sz1 5) [ 0, 1, 2, 3, 4 ]slice 9999999999998 50 (Ix1 0 ... 10000000000000)Array D Seq (Sz1 3) [ 9999999999998, 9999999999999, 10000000000000 ]
O(1) - Take a slice of a Source vector. Throws an error on incorrect indices.
Examples
slice' 10 5 (Ix1 0 ... 100)Array D Seq (Sz1 5) [ 10, 11, 12, 13, 14 ]slice' 9999999999998 3 (Ix1 0 ... 10000000000000)Array D Seq (Sz1 3) [ 9999999999998, 9999999999999, 10000000000000 ]
sliceM O(1) - Take a slice of a Source vector. Throws an error on incorrect indices.
Throws Exceptions: SizeSubregionException
Examples
sliceM 10 5 (Ix1 0 ... 100)Array D Seq (Sz1 5) [ 10, 11, 12, 13, 14 ]sliceM (-10) 5 (Ix1 0 ... 100)*** Exception: SizeSubregionException: (Sz1 101) is to small for -10 (Sz1 5)sliceM 98 50 (Ix1 0 ... 100)*** Exception: SizeSubregionException: (Sz1 101) is to small for 98 (Sz1 50)sliceM 9999999999998 3 (Ix1 0 ... 10000000000000)Array D Seq (Sz1 3) [ 9999999999998, 9999999999999, 10000000000000 ]
sslice Take a slice of a Stream vector. Never fails, instead adjusts the indices.
Examples
sslice 10 5 (Ix1 0 ... 10000000000000)Array DS Seq (Sz1 5) [ 10, 11, 12, 13, 14 ]sslice 10 5 (sfromList [0 :: Int .. ])Array DS Seq (Sz1 5) [ 10, 11, 12, 13, 14 ]sslice (-10) 5 (Ix1 0 ... 10000000000000)Array DS Seq (Sz1 5) [ 0, 1, 2, 3, 4 ]
Unlike slice it has to iterate through each element until the staring index is reached,
therefore something like sslice 9999999999998 50 (Ix1 0 ... 10000000000000) will not
be feasable.
import System.Timeout (timeout)let smallArr = sslice 9999999999998 50 (Ix1 0 ... 10000000000000)timeout 500000 (computeIO smallArr :: IO (Array P Ix1 Int))Nothing
Same as sliceAt', except it never fails.
Examples
Same as splitAt', except for a flat vector.
Examples
Same as splitAtM, except for a flat vector.
Examples
Init
O(1) - Get a vector without the last element. Never fails.
Examples
import Data.Massiv.Array as AA.init (0 ..: 10)Array D Seq (Sz1 9) [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ]A.init (empty :: Array D Ix1 Int)Array D Seq (Sz1 0) [ ]
O(1) - Get a vector without the last element. Throws an error on empty
Examples
init' (0 ..: 10)Array D Seq (Sz1 9) [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ]
O(1) - Get a vector without the last element. Throws an error on empty
Examples
import Data.Massiv.Array as AinitM (0 ..: 10)Array D Seq (Sz1 9) [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ]maybe 0 A.sum $ initM (0 ..: 10)36maybe 0 A.sum $ initM (empty :: Array D Ix1 Int)0
Tail
O(1) - Get a vector without the first element. Never fails
Examples
import Data.Massiv.Array as AA.tail (0 ..: 10)Array D Seq (Sz1 9) [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]A.tail (empty :: Array D Ix1 Int)Array D Seq (Sz1 0) [ ]
O(1) - Get a vector without the first element. Throws an error on empty
Examples
λ> tail' (0 ..: 10) Array D Seq (Sz1 9) [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ] λ> tail' (empty :: Array D Ix1 Int) Array D *** Exception: SizeEmptyException: (Sz1 0) corresponds to an empty array
O(1) - Get the vector without the first element. Throws an error on empty
Examples
import Data.Massiv.Array as AtailM (0 ..: 10)Array D Seq (Sz1 9) [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]maybe 0 A.sum $ tailM (0 ..: 10)45maybe 0 A.sum $ tailM (empty :: Array D Ix1 Int)0
Take
O(1) - Take first n elements from a vector. This function never fails and has
similar semantics as the Data.List.take for lists.
Examples
import Data.Massiv.Array as AA.take 5 (0 ..: 10)Array D Seq (Sz1 5) [ 0, 1, 2, 3, 4 ]A.take 0 (0 ..: 10)Array D Seq (Sz1 0) [ ]A.take 100 (0 ..: 10)Array D Seq (Sz1 10) [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
O(1) - Get the vector with the first n elements. Throws an error size is less
than n.
Examples
take' 0 (0 ..: 0)Array D Seq (Sz1 0) [ ]take' 5 (0 ..: 10)Array D Seq (Sz1 5) [ 0, 1, 2, 3, 4 ]
O(1) - Get the vector with the first n elements. Throws an error size is less than n
Examples
import Data.Massiv.Array as AtakeM 5 (0 ..: 10)Array D Seq (Sz1 5) [ 0, 1, 2, 3, 4 ]maybe 0 A.sum $ takeM 5 (0 ..: 10)10maybe (-1) A.sum $ takeM 15 (0 ..: 10)-1takeM 15 (0 ..: 10)*** Exception: SizeSubregionException: (Sz1 10) is to small for 0 (Sz1 15)
Slice a manifest vector in such a way that it will contain all initial elements that satisfy the supplied predicate.
O(1) - Create a Stream vector with the first n elements. Never fails
Examples
Drop
O(1) - Drop n elements from a vector. This function never fails and has
similar semantics as the Data.List.drop for lists.
Examples
import Data.Massiv.Array as Av = makeVectorR D Seq 10 idvArray D Seq (Sz1 10) [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]A.drop 5 vArray D Seq (Sz1 5) [ 5, 6, 7, 8, 9 ]A.drop 25 vArray D Seq (Sz1 0) [ ]
Slice a manifest vector in such a way that it will not contain all initial elements that satisfy the supplied predicate.
O(1) - Drop n elements from a vector. Unlike drop, this function will
produce an error when supplied number of elements to drop is larger than size
of the supplied vector
Examples
Examples
Keep all but the first n elements from the delayed stream vector.
Examples
Construction
0 declarationsInitialization
Create an Array with no elements. By itself it is not particularly useful, but it serves as a nice base for constructing larger arrays.
Examples
import Data.Massiv.Array as A:set -XTypeApplicationsxs = empty @DL @Ix1 @Doublesnoc (cons 4 (cons 5 xs)) 22Array DL Seq (Sz1 3) [ 4.0, 5.0, 22.0 ]
Create an empty delayed stream vector
Examples
Create an Array with a single element.
Examples
import Data.Massiv.Array as Asingleton 7 :: Array D Ix4 DoubleArray D Seq (Sz (1 :> 1 :> 1 :. 1)) [ [ [ [ 7.0 ] ] ] ]
Instead of specifying type signature we could use TypeApplications
:set -XTypeApplicationssingleton @U @Ix4 @Double 7Array U Seq (Sz (1 :> 1 :> 1 :. 1)) [ [ [ [ 7.0 ] ] ] ]
Create a delayed stream vector with a single element
Examples
O(1) - Add an element to the vector from the left side
O(1) - Add an element to the vector from the right side
Construct an array of the specified size that contains the same element in all of the cells.
Replicate the same element n times
Examples
Create a delayed vector of length n with a function that maps an index to an
element. Same as makeLinearArray
Examples
Create a delayed stream vector of length n with a function that maps an index to an
element. Same as makeLinearArray
Examples
Create a delayed stream vector of infinite length by repeatedly applying a function to the initial value.
Examples
stake 10 $ siterate succ 'a'Array DS Seq (Sz1 10) [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' ]
Create a delayed stream vector of length n by repeatedly applying a function to the
initial value.
Examples
siterateN 10 succ 'a'Array DS Seq (Sz1 10) [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' ]
Monadic initialization
Create a vector by using the same monadic action n times
Examples
Create a delayed stream vector of length n with a monadic action that from an index
generates an element.
Examples
Create a delayed stream vector of length n by repeatedly apply a monadic action to
the initial value.
Examples
Unfolding
Right unfolding function. Useful when it is unknown ahead of time how many elements a vector will have.
Example
import Data.Massiv.Array as Asunfoldr (\i -> if i < 9 then Just (i * i, i + 1) else Nothing) (0 :: Int)Array DS Seq (Sz1 9) [ 0, 1, 4, 9, 16, 25, 36, 49, 64 ]
O(n) - Same as sunfoldr, but with monadic generating function.
Examples
import Control.Monad (when, guard)sunfoldrM (\i -> when (i == 0) (Left "Zero denominator") >> Right (guard (i < 5) >> Just (100 `div` i, i + 1))) (-10 :: Int)Left "Zero denominator"sunfoldrM (\i -> when (i == 0) (Left "Zero denominator") >> Right (guard (i < -5) >> Just (100 `div` i, i + 1))) (-10 :: Int)Right (Array DS Seq (Sz1 5) [ -10, -12, -13, -15, -17 ])
sunfoldrN O(n) - Right unfolding function with at most n number of elements.
Example
import Data.Massiv.Array as AsunfoldrN 9 (\i -> Just (i*i, i + 1)) (0 :: Int)Array DS Seq (Sz1 9) [ 0, 1, 4, 9, 16, 25, 36, 49, 64 ]
O(n) - Same as sunfoldrN, but with monadic generating function.
Examples
import Control.Monad (guard)sunfoldrNM 6 (\i -> print i >> pure (guard (i < 5) >> Just (i * i, i + 1))) (10 :: Int)10Array DS Seq (Sz1 0) [ ]sunfoldrNM 6 (\i -> print i >> pure (guard (i < 15) >> Just (i * i, i + 1))) (10 :: Int)101112131415Array DS Seq (Sz1 5) [ 100, 121, 144, 169, 196 ]
O(n) - Similar to sunfoldrN, except the length of the resulting vector will be exactly n
Examples
sunfoldrExactN 10 (\i -> (i * i, i + 1)) (10 :: Int)Array DS Seq (Sz1 10) [ 100, 121, 144, 169, 196, 225, 256, 289, 324, 361 ]
O(n) - Similar to sunfoldrNM, except the length of the resulting vector will be exactly n
Examples
λ> sunfoldrExactNM 11 (i -> pure (100 div i, i + 1)) (-10 :: Int) Array DS *** Exception: divide by zero λ> sunfoldrExactNM 11 (i -> guard (i /= 0) >> Just (100 div i, i + 1)) (-10 :: Int) Nothing λ> sunfoldrExactNM 9 (i -> guard (i /= 0) >> Just (100 div i, i + 1)) (-10 :: Int) Just (Array DS Seq (Sz1 9) [ -10, -12, -13, -15, -17, -20, -25, -34, -50 ] )
Enumeration
Handy synonym for rangeInclusive Seq. Similar to .. for list.
Ix1 4 ... 10Array D Seq (Sz1 7) [ 4, 5, 6, 7, 8, 9, 10 ]
enumFromN Same as enumFromStepN with step dx = 1.
Related: senumFromN, senumFromStepN, enumFromStepN, rangeSize, rangeStepSize, range
Examples
import Data.Massiv.ArrayenumFromN Seq (5 :: Double) 3Array D Seq (Sz1 3) [ 5.0, 6.0, 7.0 ]
Similar:
O(n) - Enumerate from a starting number x exactly n times with a step 1.
Related: senumFromStepN, enumFromN, enumFromStepN, rangeSize, rangeStepSize, range, rangeStep'
Examples
senumFromN (10 :: Int) 9Array DS Seq (Sz1 9) [ 10, 11, 12, 13, 14, 15, 16, 17, 18 ]
Similar:
Prelude.enumFromToVery similar to
[x .. x + n - 1], except that
is faster and it only works for
and not for
elements
Data.Vector.Generic.enumFromNUses exactly the same implementation underneath.
enumFromStepN Enumerate from a starting number x exactly n times with a custom step value
dx. Unlike senumFromStepN, there is no dependency on neigboring
elements therefore enumFromStepN is parallelizable.
Related: senumFromN, senumFromStepN, enumFromN, rangeSize, rangeStepSize, range, rangeStepM
Examples
import Data.Massiv.ArrayenumFromStepN Seq 1 (0.1 :: Double) 5Array D Seq (Sz1 5) [ 1.0, 1.1, 1.2, 1.3, 1.4 ]enumFromStepN Seq (-pi :: Float) (pi/4) 9Array D Seq (Sz1 9) [ -3.1415927, -2.3561945, -1.5707964, -0.78539824, 0.0, 0.78539824, 1.5707963, 2.3561947, 3.1415927 ]
Similar:
Prelude.enumFromSimilar to
take n [x, x + dx ..], except that
is parallelizable and it only works for
and not for
elements. Floating point value will be slightly different as well.
Data.Vector.Generic.enumFromStepNSimilar in the outcome, but very different in the way it works.
senumFromStepN O(n) - Enumerate from a starting number x exactly n times with a custom step value dx
Examples
senumFromStepN (5 :: Int) 2 10Array DS Seq (Sz1 10) [ 5, 7, 9, 11, 13, 15, 17, 19, 21, 23 ]
Similar:
Prelude.enumFromJust like
take n [x, x + dx ..], except that
is faster and it only works for
and not for
elements
Data.Vector.Generic.enumFromStepNUses exactly the same implementation underneath.
Concatenation
Append two vectors together
Related: appendM, appendOuterM,
Examples
λ> sappend (1 ..: 6) (senumFromStepN 6 (-1) 6) Array DS Seq (Sz1 11) [ 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1 ]
Similar:
Data.Semigroup.Data.Semigroup.<>and
arrays have instances for
, so they will work in a similar fashion.
differs in that it accepts
arrays with possibly different representations.
Data.List.Data.List.++Same operation, but for lists.
Data.Vector.Generic.++Uses exactly the same implementation underneath as
, except that it cannot append two vectors with different memory representations.
Concat vectors together
Related: concatM, concatOuterM,
Examples
sconcat [2 ... 6, empty, singleton 1, generate Seq 5 id]Array DS Seq (Sz1 11) [ 2, 3, 4, 5, 6, 1, 0, 1, 2, 3, 4 ]sconcat [senumFromN 2 5, sempty, ssingleton 1, sgenerate 5 id]Array DS Seq (Sz1 11) [ 2, 3, 4, 5, 6, 1, 0, 1, 2, 3, 4 ]
Similar:
Map a function over a stream vector
Examples
Map an index aware function over a stream vector
Examples
Monadic mapping
Traverse a stream vector with an applicative function.
Examples
Traverse a stream vector with an index aware applicative function.
Examples
Traverse a stream vector with a monadic function.
Examples
Traverse a stream vector with a monadic function, while discarding the result
Examples
Traverse a stream vector with a monadic index aware function.
Corresponds to: mapM (uncurry f) . imap (,) v
Examples
Traverse a stream vector with a monadic index aware function, while discarding the result
Examples
Same as smapM, but with arguments flipped.
Examples
Same as smapM_, but with arguments flipped.
Examples
Same as simapM, but with arguments flipped.
Examples
Same as simapM_, but with arguments flipped.
Examples
Zipping
Zip two vectors together into a vector. The length of a resulting vector will be the smallest length of the supplied vectors.
Examples
Zip three vectors together into a vector. The length of a resulting vector will be the smallest length of the supplied vectors.
Zip four vectors together into a vector. The length of a resulting vector will be the smallest length of the supplied vectors.
Zip five vectors together into a vector. The length of a resulting vector will be the smallest length of the supplied vectors.
Zip six vectors together into a vector. The length of a resulting vector will be the smallest length of the supplied vectors.
Zip two vectors together with a binary function into a vector. The length of a resulting vector will be the smallest length of the supplied vectors.
Examples
Zip three vectors together with a ternary function into a vector. The length of a resulting vector will be the smallest length of the supplied vectors.
Zip four vectors together with a quaternary function into a vector. The length of a resulting vector will be the smallest length of the supplied vectors.
Zip five vectors together with a quinary function into a vector. The length of a resulting vector will be the smallest length of the supplied vectors.
Zip six vectors together with a senary function into a vector. The length of a resulting vector will be the smallest length of the supplied vectors.
Just like szipWith, zip two vectors together, but with an index aware function. The length of a resulting vector will be the smallest length of the supplied vectors.
Examples
Just like szipWith3, zip three vectors together, but with an index aware function. The length of a resulting vector will be the smallest length of the supplied vectors.
Just like szipWith4, zip four vectors together, but with an index aware function. The length of a resulting vector will be the smallest length of the supplied vectors.
Just like szipWith5, zip five vectors together, but with an index aware function. The length of a resulting vector will be the smallest length of the supplied vectors.
Just like szipWith6, zip six vectors together, but with an index aware function. The length of a resulting vector will be the smallest length of the supplied vectors.
Monadic zipping
Zip two vectors together with a binary monadic action into a vector. The length of a resulting vector will be the smallest length of the supplied vectors.
Examples
Zip three vectors together with a ternary monadic action into a vector. The length of a resulting vector will be the smallest length of the supplied vectors.
Zip four vectors together with a quaternary monadic action into a vector. The length of a resulting vector will be the smallest length of the supplied vectors.
Zip five vectors together with a quinary monadic action into a vector. The length of a resulting vector will be the smallest length of the supplied vectors.
Zip six vectors together with a senary monadic action into a vector. The length of a resulting vector will be the smallest length of the supplied vectors.
Just like szipWithM, zip two vectors together, but with an index aware monadic action. The length of a resulting vector will be the smallest length of the supplied vectors.
Examples
Just like szipWith3M, zip three vectors together, but with an index aware monadic action. The length of a resulting vector will be the smallest length of the supplied vectors.
Just like szipWith4M, zip four vectors together, but with an index aware monadic action. The length of a resulting vector will be the smallest length of the supplied vectors.
Just like szipWith6M, zip five vectors together, but with an index aware monadic action. The length of a resulting vector will be the smallest length of the supplied vectors.
Just like szipWith6M, zip six vectors together, but with an index aware monadic action. The length of a resulting vector will be the smallest length of the supplied vectors.
Examples
Similar to szipWithM, zip two vectors together with a binary monadic action, while discarding its result. The action will be invoked as many times as the length of the smallest vector.
Examples
Similar to szipWith3M, zip three vectors together with a ternary monadic action, while discarding its result. The action will be invoked as many times as the length of the smallest vector.
Similar to szipWith4M, zip four vectors together with a quaternary monadic action, while discarding its result. The action will be invoked as many times as the length of the smallest vector.
Similar to szipWith5M, zip five vectors together with a quinary monadic action, while discarding its result. The action will be invoked as many times as the length of the smallest vector.
Similar to szipWith6M, zip six vectors together with a senary monadic action, while discarding its result. The action will be invoked as many times as the length of the smallest vector.
Same as szipWithM_, zip two vectors together, but with an index aware monadic action. The action will be invoked as many times as the length of the smallest vector.
Examples
Same as szipWith3M_, zip three vectors together, but with an index aware monadic action. The action will be invoked as many times as the length of the smallest vector.
Same as szipWith4M_, zip four vectors together, but with an index aware monadic action. The action will be invoked as many times as the length of the smallest vector.
Same as szipWith5M_, zip five vectors together, but with an index aware monadic action. The action will be invoked as many times as the length of the smallest vector.
Same as szipWith6M_, zip six vectors together, but with an index aware monadic action. The action will be invoked as many times as the length of the smallest vector.
Predicates
0 declarationsFiltering
Sequentially filter out elements from the array according to the supplied predicate.
Example
import Data.Massiv.Array as Aarr = makeArrayR D Seq (Sz2 3 4) fromIx2arrArray D Seq (Sz (3 :. 4)) [ [ (0,0), (0,1), (0,2), (0,3) ] , [ (1,0), (1,1), (1,2), (1,3) ] , [ (2,0), (2,1), (2,2), (2,3) ] ]sfilter (even . fst) arrArray DS Seq (Sz1 8) [ (0,0), (0,1), (0,2), (0,3), (2,0), (2,1), (2,2), (2,3) ]
Similar to sfilter, but filter with an index aware function.
Examples
Sequentially filter out elements from the array according to the supplied applicative predicate.
Example
import Data.Massiv.Array as Aarr = makeArrayR D Seq (Sz2 3 4) fromIx2arrArray D Seq (Sz (3 :. 4)) [ [ (0,0), (0,1), (0,2), (0,3) ] , [ (1,0), (1,1), (1,2), (1,3) ] , [ (2,0), (2,1), (2,2), (2,3) ] ]sfilterM (Just . odd . fst) arrJust (Array DS Seq (Sz1 4) [ (1,0), (1,1), (1,2), (1,3) ])sfilterM (\ix@(_, j) -> print ix >> return (even j)) arr(0,0)(0,1)(0,2)(0,3)(1,0)(1,1)(1,2)(1,3)(2,0)(2,1)(2,2)(2,3)Array DS Seq (Sz1 6) [ (0,0), (0,2), (1,0), (1,2), (2,0), (2,2) ]
Similar to filterM, but filter with an index aware function.
Corresponds to: filterM (uncurry f) . simap (,)
Similar to smapMaybe, but map with an index aware function.
Examples
Similar to smapMaybeM, but map with an index aware function.
Examples
O(n) - Perform a row-major search starting at 0 for an element. Returns the index
of the first occurance of an element or Nothing if a predicate could not be satisifed
after it was applyied to all elements of the array.
Folding
9 declarationsStreaming fold over an array in a row-major fashion with a left biased function and a strict accumulator.
Examples
Examples
Examples
Examples
Examples
Examples
Examples
Examples
Examples
Specialized folds
Examples
Examples
Examples
Examples
Add all elements of the array together
Related: sum.
Examples
import Data.Massiv.Vector as VV.ssum $ V.sfromList [10, 3, 70, 5 :: Int]88
Multiply all elements of the array together
Related: product.
Examples
import Data.Massiv.Vector as VV.sproduct $ V.sfromList [10, 3, 70, 5 :: Int]10500
O(n) - Find the largest value in the array.
Related: smaximum, maximum, maximumM.
Throws Exceptions: SizeEmptyException when array is empty
Examples
import Data.Massiv.Vector as VV.smaximumM $ V.sfromList [10, 3, 70, 5 :: Int]70V.smaximumM (V.empty :: Vector D Int) :: Maybe IntNothing
O(n) - Find the smallest value in the array.
Related: sminimum', minimum, minimumM.
Throws Exceptions: SizeEmptyException when array is empty
Examples
import Data.Massiv.Vector as VV.sminimumM $ V.sfromList [10, 3, 70, 5 :: Int]3V.sminimumM (V.empty :: Array D Ix2 Int) :: Maybe IntNothing
Scanning
O(n) - left scan with strict accumulator. First element is the value of the accumulator. Last element is not included.
Examples
import Data.Massiv.Vectorsprescanl min 6 $ sfromList [10, 5, 70, 3 :: Int]Array DS Seq (Sz1 4) [ 6, 6, 5, 5 ]sprescanl (+) 0 $ sfromList [10, 5, 70, 3 :: Int]Array DS Seq (Sz1 4) [ 0, 10, 15, 85 ]
O(n) - left scan with strict accumulator. First element is the result of applying the supplied function.
Examples
import Data.Massiv.Vectorspostscanl min 6 $ sfromList [10, 5, 70, 3 :: Int]Array DS Seq (Sz1 4) [ 6, 5, 5, 3 ]spostscanl (+) 0 $ sfromList [10, 5, 70, 3 :: Int]Array DS Seq (Sz1 4) [ 10, 15, 85, 88 ]
O(n) - Just like spostscanl except it is possible to produce a vector with an element type that differes from accumulator type.
Examples
import Data.Massiv.VectorspostscanlAcc (\x y -> if x < y then (True, x) else (False, y)) 6 $ sfromList [10, 5, 70, 3 :: Int]Array DS Seq (Sz1 4) [ True, False, True, False ]
O(n) - left scan with strict accumulator. First element is the value of the accumulator.
Examples
import Data.Massiv.Vectorsscanl min 6 $ sfromList [10, 5, 70, 3 :: Int]Array DS Seq (Sz1 5) [ 6, 6, 5, 5, 3 ]sscanl (+) 0 $ sfromList [10, 5, 70, 3 :: Int]Array DS Seq (Sz1 5) [ 0, 10, 15, 85, 88 ]
O(n) - left scan with strict accumulator and no initial value for the accumulator.
Examples
import Data.Massiv.Vectorsscanl1 min $ sfromList [10, 5, 70, 3 :: Int]Array DS Seq (Sz1 4) [ 10, 5, 5, 3 ]sscanl1 (+) $ sfromList [10, 5, 70, 3 :: Int]Array DS Seq (Sz1 4) [ 10, 15, 85, 88 ]sscanl1 (+) $ sfromList ([] :: [Int])Array DS Seq (Sz1 0) [ ]
Conversions
0 declarationsLists
Convert an array to a list by the means of a delayed stream vector.
Related: toList
Examples
Convert a flat list into a vector
Convert a list to a delayed stream vector
Related: fromList, fromListN, sfromListN
Examples
sfromList ([] :: [Int])Array DS Seq (Sz1 0) [ ]sfromList ([1,2,3] :: [Int])Array DS Seq (Sz1 3) [ 1, 2, 3 ]
Convert a list to a delayed stream vector. Length of the resulting vector will be at
most n. This version isn't really more efficient then sfromList, but there is
unsafeFromListN
Related: fromList, fromListN, sfromList
Examples
sfromListN 10 [1 :: Int ..]Array DS Seq (Sz1 10) [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]sfromListN 10 [1 :: Int .. 5]Array DS Seq (Sz1 5) [ 1, 2, 3, 4, 5 ]
Computation
13 declarationsCompute array sequentially disregarding predefined computation strategy. Very much the same as computePrimM, but executed in ST, thus pure.
Compute an array in PrimMonad sequentially disregarding predefined computation strategy.
Just as compute, but let's you supply resulting representation type as an argument.
Examples
import Data.Massiv.ArraycomputeAs P $ range Seq (Ix1 0) 10Array P Seq (Sz1 10) [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
Same as compute and computeAs, but let's you supply resulting representation type as a proxy argument.
Examples
Useful only really for cases when representation constructor or TypeApplications extension
aren't desireable for some reason:
import Data.Proxyimport Data.Massiv.ArraycomputeProxy (Proxy :: Proxy P) $ (^ (2 :: Int)) <$> range Seq (Ix1 0) 10Array P Seq (Sz1 10) [ 0, 1, 4, 9, 16, 25, 36, 49, 64, 81 ]
Same as computeWithStride, but with ability to specify resulting array representation.
O(n) - Make an exact immutable copy of an Array.
O(n) - conversion between array types. A full copy will occur, unless when the source and result arrays are of the same representation, in which case it is an O(1) operation.
Same as convert, but let's you supply resulting representation type as an argument.
Re-exports
module Data.Massiv.Core
module Data.Massiv.Array.Delayed
module Data.Massiv.Array.Manifest
module Data.Massiv.Array.Mutable