HORIZON HASKELLDocslts/ghc-10.0.x8c2f8e82026-10-19Search names, modules, packages, or :: a typeCtrl K

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 8c2f8e8 · 2026-10-19

Modulemassiv-1.0.5.0Haskell2010

Data.Massiv.Vector

  • 2 types
  • 164 values
  • Packagemassiv-1.0.5.0
  • Exports170
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceCommon.hs
typetype Vector r e = Array r Ix1 e
#

Type synonym for a single dimension array, or simply a flat vector.

typetype MVector s r e = MArray s r Ix1 e
#

Type synonym for a single dimension mutable array, or simply a flat mutable vector.

Accessors

0 declarations

Size

valueslength :: Stream r ix e => Array r ix e -> Maybe Sz1
#

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
Example6 expressions
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.length

For 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

Stream

vectors that is not always the case.

Data.Vector.Generic.length

In the vector package this function will always break fusion, unless it is the only operation that is applied to the vector.

methodmaxLinearSize :: Array r ix e -> Maybe Sz1
#

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.

methodsize :: Array r ix e -> Sz ix
#

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.

methodisNull :: Array r ix e -> Bool
#

O(1) - Check whether an array is empty or not.

Examples
Example5 expressions
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
valueisNotNull :: Shape r ix => Array r ix e -> Bool
#

O(1) - Check if array has elements.

Examples
Example3 expressions
import Data.Massiv.ArrayisNotNull (singleton 1 :: Array D Ix2 Int)TrueisNotNull (empty :: Array D Ix2 Int)False

Indexing

value(!?) :: (Index ix, Manifest r e, MonadThrow m) => Array r ix e -> ix -> m e
#

O(1) - Infix version of indexM.

Exceptions: IndexOutOfBoundsException

Examples
Example7 expressions
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
value(!) :: (HasCallStack, Manifest r e, Index ix) => Array r ix e -> ix -> e
#

O(1) - Infix version of index'.

Examples
Example4 expressions
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
valueindex :: (Index ix, Manifest r e) => Array r ix e -> ix -> Maybe e
#

O(1) - Lookup an element in the array. Returns Nothing, when index is out of bounds and returns the element at the supplied index otherwise. Use indexM instead, since it is more general and it can just as well be used with Maybe.

valueindex' :: (HasCallStack, Index ix, Manifest r e) => Array r ix e -> ix -> e
#

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
Example4 expressions
import Data.Massiv.Array:set -XOverloadedListsxs = [0..100] :: Array U Ix1 Intindex' xs 5050
valuehead' :: (HasCallStack, Source r e) => Vector r e -> e
#

O(1) - Get the first element of a Source vector. Throws an error on empty.

Related: shead', headM, sheadM, unconsM.

Examples
Example1 expression
head' (Ix1 10 ..: 10000000000000)10

Similar:

Data.List.Data.List.head

Also constant time and partial. Fusion is broken if there other consumers of the list.

Data.Vector.Generic.head

Also constant time and partial. Will cause materialization of the full vector if any other function is applied to the vector.

valuelast' :: (HasCallStack, Source r e) => Vector r e -> e
#

O(1) - Get the last element of a Source vector. Throws an error on empty.

Related: lastM, unsnocM

Examples
Example1 expression
last' (Ix1 10 ... 10000000000000)10000000000000

Similar:

Data.List.Data.List.last

Also partial, but it has

O(n)

complexity. Fusion is broken if there other consumers of the list.

Data.Vector.Generic.last

Also constant time and partial. Will cause materialization of the full vector if any other function is applied to the vector.

Monadic Indexing

valueheadM :: (Source r e, MonadThrow m) => Vector r e -> m e
#

O(1) - Get the first element of a Source vector.

Related: head', shead', sheadM, unconsM.

Throws Exceptions: SizeEmptyException when array is empty

Examples
Example4 expressions
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.listToMaybe

It also a safe way to get the head of the list, except it is restricted to

Maybe

valuesheadM :: (Stream r Ix1 e, MonadThrow m) => Vector r e -> m e
#

O(1) - Get the first element of a Stream vector.

Related: head', shead', headM, unconsM.

Throws Exceptions: SizeEmptyException

Examples
Example4 expressions
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
valuelastM :: (Source r e, MonadThrow m) => Vector r e -> m e
#

O(1) - Get the last element of a Source vector.

Related: last', unsnocM

Throws Exceptions: SizeEmptyException

Examples
Example3 expressions
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"
valueunconsM :: (MonadThrow m, Source r e) => Vector r e -> m (e, Vector r e)
#

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
Example1 expression
unconsM (fromList Seq [1,2,3] :: Array P Ix1 Int)(1,Array P Seq (Sz1 2)  [ 2, 3 ])

Similar:

Data.List.Data.List.uncons

Same concept, except it is restricted to

Maybe

instead of the more general

MonadThrow

valueunsnocM :: (MonadThrow m, Source r e) => Vector r e -> m (Vector r e, e)
#

O(1) - Take one element off of the vector from the right side, as well as the remaining part of the vector.

Related: last', lastM, snoc

Throws Exceptions: SizeEmptyException

Examples
Example1 expression
unsnocM (fromList Seq [1,2,3] :: Array P Ix1 Int)(Array P Seq (Sz1 2)  [ 1, 2 ],3)

Slicing

valueslice :: Source r e => Ix1 -> Sz1 -> Vector r e -> Vector r e
#

O(1) - Take a slice of a Source vector. Never fails, instead adjusts the indices.

Examples
Example3 expressions
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 ]
valueslice'
  1. :: (HasCallStack, Source r e)
  2. => Ix1
  3. -> Sz1
  4. -> Vector r e
  5. -> Vector r e
#

O(1) - Take a slice of a Source vector. Throws an error on incorrect indices.

Examples
Example2 expressions
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 ]
valuesliceM
  1. :: (Source r e, MonadThrow m)
  2. => Ix1

    Starting index

  3. -> Sz1

    Number of elements to take from the Source vector

  4. -> Vector r e

    Source vector to take a slice from

  5. -> m (Vector r e)
#

O(1) - Take a slice of a Source vector. Throws an error on incorrect indices.

Throws Exceptions: SizeSubregionException

Examples
Example4 expressions
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 ]
valuesslice
  1. :: Stream r Ix1 e
  2. => Ix1

    Starting index

  3. -> Sz1

    Number of elements to take from the stream vector

  4. -> Vector r e

    Stream vector to take a slice from

  5. -> Vector DS e
#

Take a slice of a Stream vector. Never fails, instead adjusts the indices.

Examples
Example3 expressions
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.

Example3 expressions
import System.Timeout (timeout)let smallArr = sslice 9999999999998 50 (Ix1 0 ... 10000000000000)timeout 500000 (computeIO smallArr :: IO (Array P Ix1 Int))Nothing

Init

valueinit :: Source r e => Vector r e -> Vector r e
#

O(1) - Get a vector without the last element. Never fails.

Examples
Example3 expressions
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)  [  ]
valueinit' :: (HasCallStack, Source r e) => Vector r e -> Vector r e
#

O(1) - Get a vector without the last element. Throws an error on empty

Examples
Example1 expression
init' (0 ..: 10)Array D Seq (Sz1 9)  [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ]
valueinitM :: (Source r e, MonadThrow m) => Vector r e -> m (Vector r e)
#

O(1) - Get a vector without the last element. Throws an error on empty

Examples
Example4 expressions
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

valuetail :: Source r e => Vector r e -> Vector r e
#

O(1) - Get a vector without the first element. Never fails

Examples
Example3 expressions
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)  [  ]
valuetail' :: (HasCallStack, Source r e) => Vector r e -> Vector r e
#

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

valuetailM :: (Source r e, MonadThrow m) => Vector r e -> m (Vector r e)
#

O(1) - Get the vector without the first element. Throws an error on empty

Examples
Example4 expressions
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

valuetake :: Source r e => Sz1 -> Vector r e -> Vector r e
#

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
Example4 expressions
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 ]
valuetake' :: (HasCallStack, Source r e) => Sz1 -> Vector r e -> Vector r e
#

O(1) - Get the vector with the first n elements. Throws an error size is less than n.

Examples
Example2 expressions
take' 0 (0 ..: 0)Array D Seq (Sz1 0)  [  ]take' 5 (0 ..: 10)Array D Seq (Sz1 5)  [ 0, 1, 2, 3, 4 ]
valuetakeM :: (Source r e, MonadThrow m) => Sz1 -> Vector r e -> m (Vector r e)
#

O(1) - Get the vector with the first n elements. Throws an error size is less than n

Examples
Example5 expressions
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)
valuetakeWhile :: Manifest r e => (e -> Bool) -> Vector r e -> Vector r e
#

Slice a manifest vector in such a way that it will contain all initial elements that satisfy the supplied predicate.

Drop

valuedrop :: Source r e => Sz1 -> Vector r e -> Vector r e
#

O(1) - Drop n elements from a vector. This function never fails and has similar semantics as the Data.List.drop for lists.

Examples
Example5 expressions
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)  [  ]
valuedropWhile :: Manifest r e => (e -> Bool) -> Vector r e -> Vector r e
#

Slice a manifest vector in such a way that it will not contain all initial elements that satisfy the supplied predicate.

valuedrop' :: (HasCallStack, Source r e) => Sz1 -> Vector r e -> Vector r e
#

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

Construction

0 declarations

Initialization

valueempty :: Load r ix e => Array r ix e
#

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
Example4 expressions
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 ]
valuesempty :: Vector DS e
#

Create an empty delayed stream vector

Examples
valuesingleton
  1. :: Load r ix e
  2. => e

    The only element

  3. -> Array r ix e
#

Create an Array with a single element.

Examples
Example2 expressions
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

Example2 expressions
:set -XTypeApplicationssingleton @U @Ix4 @Double 7Array U Seq (Sz (1 :> 1 :> 1 :. 1))  [ [ [ [ 7.0 ]      ]    ]  ]
valuessingleton :: e -> Vector DS e
#

Create a delayed stream vector with a single element

Examples
methodreplicate :: Comp -> Sz ix -> e -> Array r ix e
#

Construct an array of the specified size that contains the same element in all of the cells.

valuegenerate :: Comp -> Sz1 -> (Ix1 -> e) -> Vector D e
#

Create a delayed vector of length n with a function that maps an index to an element. Same as makeLinearArray

Examples
valuesgenerate :: Sz1 -> (Ix1 -> e) -> Vector DS e
#

Create a delayed stream vector of length n with a function that maps an index to an element. Same as makeLinearArray

Examples
valuesiterate :: (e -> e) -> e -> Vector DS e
#

Create a delayed stream vector of infinite length by repeatedly applying a function to the initial value.

Examples
Example1 expression
stake 10 $ siterate succ 'a'Array DS Seq (Sz1 10)  [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' ]
valuesiterateN :: Sz1 -> (e -> e) -> e -> Vector DS e
#

Create a delayed stream vector of length n by repeatedly applying a function to the initial value.

Examples
Example1 expression
siterateN 10 succ 'a'Array DS Seq (Sz1 10)  [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' ]

Monadic initialization

valuesgenerateM :: Monad m => Sz1 -> (Ix1 -> m e) -> m (Vector DS e)
#

Create a delayed stream vector of length n with a monadic action that from an index generates an element.

Examples
valuesiterateNM :: Monad m => Sz1 -> (e -> m e) -> e -> m (Vector DS e)
#

Create a delayed stream vector of length n by repeatedly apply a monadic action to the initial value.

Examples

Unfolding

valuesunfoldr :: (s -> Maybe (e, s)) -> s -> Vector DS e
#

Right unfolding function. Useful when it is unknown ahead of time how many elements a vector will have.

Example
Example2 expressions
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 ]
valuesunfoldrM :: Monad m => (s -> m (Maybe (e, s))) -> s -> m (Vector DS e)
#

O(n) - Same as sunfoldr, but with monadic generating function.

Examples
Example3 expressions
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 ])
valuesunfoldrN
  1. :: Sz1

    n - maximum number of elements that the vector will have

  2. -> (s -> Maybe (e, s))

    Unfolding function. Stops when Nothing is returned or maximum number of elements is reached.

  3. -> s

    Inititial element.

  4. -> Vector DS e
#

O(n) - Right unfolding function with at most n number of elements.

Example
Example2 expressions
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 ]
valuesunfoldrNM
  1. :: Monad m
  2. => Sz1
  3. -> s -> m (Maybe (e, s))
  4. -> s
  5. -> m (Vector DS e)
#

O(n) - Same as sunfoldrN, but with monadic generating function.

Examples
Example3 expressions
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 ]
valuesunfoldrExactN :: Sz1 -> (s -> (e, s)) -> s -> Vector DS e
#

O(n) - Similar to sunfoldrN, except the length of the resulting vector will be exactly n

Examples
Example1 expression
sunfoldrExactN 10 (\i -> (i * i, i + 1)) (10 :: Int)Array DS Seq (Sz1 10)  [ 100, 121, 144, 169, 196, 225, 256, 289, 324, 361 ]
valuesunfoldrExactNM :: Monad m => Sz1 -> (s -> m (e, s)) -> s -> m (Vector DS e)
#

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

value(...) :: Index ix => ix -> ix -> Array D ix ix
#

Handy synonym for rangeInclusive Seq. Similar to .. for list.

Example1 expression
Ix1 4 ... 10Array D Seq (Sz1 7)  [ 4, 5, 6, 7, 8, 9, 10 ]
value(..:) :: Index ix => ix -> ix -> Array D ix ix
#

Handy synonym for range Seq

Example1 expression
Ix1 4 ..: 10Array D Seq (Sz1 6)  [ 4, 5, 6, 7, 8, 9 ]
valueenumFromN
  1. :: Num e
  2. => Comp
  3. -> e

    x - start value

  4. -> Sz1

    n - length of resulting vector.

  5. -> Vector D e
#

Same as enumFromStepN with step dx = 1.

Related: senumFromN, senumFromStepN, enumFromStepN, rangeSize, rangeStepSize, range

Examples
Example2 expressions
import Data.Massiv.ArrayenumFromN Seq (5 :: Double) 3Array D Seq (Sz1 3)  [ 5.0, 6.0, 7.0 ]

Similar:

Prelude.Prelude.enumFromTo

Very similar to

[i .. i + n - 1]

, except that

enumFromN

is faster, but it only works for

Num

and not for

Enum

elements

Data.Vector.Generic.enumFromN
valuesenumFromN
  1. :: Num e
  2. => e

    x - starting number

  3. -> Sz1

    n - length of resulting vector

  4. -> Vector DS e
#

O(n) - Enumerate from a starting number x exactly n times with a step 1.

Related: senumFromStepN, enumFromN, enumFromStepN, rangeSize, rangeStepSize, range, rangeStep'

Examples
Example1 expression
senumFromN (10 :: Int) 9Array DS Seq (Sz1 9)  [ 10, 11, 12, 13, 14, 15, 16, 17, 18 ]

Similar:

Prelude.enumFromTo

Very similar to

[x .. x + n - 1]

, except that

senumFromN

is faster and it only works for

Num

and not for

Enum

elements

Data.Vector.Generic.enumFromN

Uses exactly the same implementation underneath.

valueenumFromStepN
  1. :: Num e
  2. => Comp
  3. -> e

    x - start number

  4. -> e

    dx - step number

  5. -> Sz1

    n - length of resulting vector

  6. -> Vector D e
#

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
Example3 expressions
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.enumFrom

Similar to

take n [x, x + dx ..]

, except that

enumFromStepN

is parallelizable and it only works for

Num

and not for

Enum

elements. Floating point value will be slightly different as well.

Data.Vector.Generic.enumFromStepN

Similar in the outcome, but very different in the way it works.

valuesenumFromStepN
  1. :: Num e
  2. => e

    x - starting number

  3. -> e

    dx - Step

  4. -> Sz1

    n - length of resulting vector

  5. -> Vector DS e
#

O(n) - Enumerate from a starting number x exactly n times with a custom step value dx

Examples
Example1 expression
senumFromStepN (5 :: Int) 2 10Array DS Seq (Sz1 10)  [ 5, 7, 9, 11, 13, 15, 17, 19, 21, 23 ]

Similar:

Prelude.enumFrom

Just like

take n [x, x + dx ..]

, except that

senumFromN

is faster and it only works for

Num

and not for

Enum

elements

Data.Vector.Generic.enumFromStepN

Uses exactly the same implementation underneath.

Concatenation

valuesappend
  1. :: (Stream r1 Ix1 e, Stream r2 Ix1 e)
  2. => Vector r1 e
  3. -> Vector r2 e
  4. -> Vector DS e
#

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.<>

DS

and

DL

arrays have instances for

Semigroup

, so they will work in a similar fashion.

sappend

differs in that it accepts

Stream

arrays with possibly different representations.

Data.List.Data.List.++

Same operation, but for lists.

Data.Vector.Generic.++

Uses exactly the same implementation underneath as

sappend

, except that it cannot append two vectors with different memory representations.

valuesconcat :: Stream r Ix1 e => [Vector r e] -> Vector DS e
#

Concat vectors together

Related: concatM, concatOuterM,

Examples
Example2 expressions
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:

Data.Monoid.mconcat

DS

and

DL

arrays have instances for

Monoid

, so they will work in a similar fashion.

sconcat

differs in that it accepts

Stream

arrays of other representations.

Data.List.Data.List.concat

Same operation, but for lists.

Data.Vector.Generic.concat

Uses exactly the same implementation underneath as

sconcat

.

valuesimap :: Stream r ix a => (ix -> a -> b) -> Array r ix a -> Vector DS b
#

Map an index aware function over a stream vector

Examples

Monadic mapping

valuesmapM_ :: (Stream r ix a, Monad m) => (a -> m b) -> Array r ix a -> m ()
#

Traverse a stream vector with a monadic function, while discarding the result

Examples
valuesimapM
  1. :: (Stream r ix a, Monad m)
  2. => ix -> a -> m b
  3. -> Array r ix a
  4. -> m (Vector DS b)
#

Traverse a stream vector with a monadic index aware function.

Corresponds to: mapM (uncurry f) . imap (,) v

Examples
valuesimapM_
  1. :: (Stream r ix a, Monad m)
  2. => ix -> a -> m b
  3. -> Array r ix a
  4. -> m ()
#

Traverse a stream vector with a monadic index aware function, while discarding the result

Examples

Zipping

valueszip
  1. :: (Stream ra Ix1 a, Stream rb Ix1 b)
  2. => Vector ra a
  3. -> Vector rb b
  4. -> Vector DS (a, b)
#

Zip two vectors together into a vector. The length of a resulting vector will be the smallest length of the supplied vectors.

Examples
valueszipWith
  1. :: (Stream ra Ix1 a, Stream rb Ix1 b)
  2. => a -> b -> c
  3. -> Vector ra a
  4. -> Vector rb b
  5. -> Vector DS c
#

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

Monadic zipping

valueszipWithM
  1. :: (Stream ra Ix1 a, Stream rb Ix1 b, Monad m)
  2. => a -> b -> m c
  3. -> Vector ra a
  4. -> Vector rb b
  5. -> m (Vector DS c)
#

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
valueszipWithM_
  1. :: (Stream ra Ix1 a, Stream rb Ix1 b, Monad m)
  2. => a -> b -> m c
  3. -> Vector ra a
  4. -> Vector rb b
  5. -> m ()
#

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

Predicates

0 declarations

Filtering

valuesfilter :: Stream r ix e => (e -> Bool) -> Array r ix e -> Vector DS e
#

Sequentially filter out elements from the array according to the supplied predicate.

Example
Example4 expressions
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) ]
valuesfilterM
  1. :: (Stream r ix e, Applicative f)
  2. => e -> f Bool
  3. -> Array r ix e
  4. -> f (Vector DS e)
#

Sequentially filter out elements from the array according to the supplied applicative predicate.

Example
Example5 expressions
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) ]
valuefindIndex
  1. :: (Index ix, Manifest r e)
  2. => e -> Bool
  3. -> Array r ix e
  4. -> Maybe ix
#

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 declarations
valuesfoldl :: Stream r ix e => (a -> e -> a) -> a -> Array r ix e -> a
#

Streaming fold over an array in a row-major fashion with a left biased function and a strict accumulator.

Examples

Specialized folds

valuessum :: (Num e, Stream r ix e) => Array r ix e -> e
#

Add all elements of the array together

Related: sum.

Examples
Example2 expressions
import Data.Massiv.Vector as VV.ssum $ V.sfromList [10, 3, 70, 5 :: Int]88
valuesproduct :: (Num e, Stream r ix e) => Array r ix e -> e
#

Multiply all elements of the array together

Related: product.

Examples
Example2 expressions
import Data.Massiv.Vector as VV.sproduct $ V.sfromList [10, 3, 70, 5 :: Int]10500
valuesmaximum' :: (HasCallStack, Ord e, Stream r ix e) => Array r ix e -> e
#

O(n) - Find the largest value in the array. Throws an error on empty.

Related: smaximumM, maximum, maximumM.

Examples
Example2 expressions
import Data.Massiv.Vector as VV.smaximum' $ V.sfromList [10, 3, 70, 5 :: Int]70
valuesmaximumM :: (Ord e, Stream r ix e, MonadThrow m) => Array r ix e -> m e
#

O(n) - Find the largest value in the array.

Related: smaximum, maximum, maximumM.

Throws Exceptions: SizeEmptyException when array is empty

Examples
Example3 expressions
import Data.Massiv.Vector as VV.smaximumM $ V.sfromList [10, 3, 70, 5 :: Int]70V.smaximumM (V.empty :: Vector D Int) :: Maybe IntNothing
valuesminimum' :: (HasCallStack, Ord e, Stream r ix e) => Array r ix e -> e
#

O(n) - Find the smallest value in the array. Throws an error on empty.

Related: sminimumM, minimum, minimumM.

Examples
Example2 expressions
import Data.Massiv.Vector as VV.sminimum' $ V.sfromList [10, 3, 70, 5 :: Int]3
valuesminimumM :: (Ord e, Stream r ix e, MonadThrow m) => Array r ix e -> m e
#

O(n) - Find the smallest value in the array.

Related: sminimum', minimum, minimumM.

Throws Exceptions: SizeEmptyException when array is empty

Examples
Example3 expressions
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

valuesprescanl
  1. :: Stream r ix e
  2. => a -> e -> a
  3. -> a
  4. -> Array r ix e
  5. -> Vector DS a
#

O(n) - left scan with strict accumulator. First element is the value of the accumulator. Last element is not included.

Examples
Example3 expressions
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 ]
valuespostscanl
  1. :: Stream r ix e
  2. => a -> e -> a
  3. -> a
  4. -> Array r ix e
  5. -> Vector DS a
#

O(n) - left scan with strict accumulator. First element is the result of applying the supplied function.

Examples
Example3 expressions
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 ]
valuespostscanlAcc
  1. :: Stream r ix e
  2. => c -> e -> (a, c)
  3. -> c
  4. -> Array r ix e
  5. -> Vector DS a
#

O(n) - Just like spostscanl except it is possible to produce a vector with an element type that differes from accumulator type.

Examples
Example2 expressions
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 ]
valuesscanl :: Stream r ix e => (a -> e -> a) -> a -> Array r ix e -> Vector DS a
#

O(n) - left scan with strict accumulator. First element is the value of the accumulator.

Examples
Example3 expressions
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 ]
valuesscanl1 :: Stream r ix e => (e -> e -> e) -> Array r ix e -> Vector DS e
#

O(n) - left scan with strict accumulator and no initial value for the accumulator.

Examples
Example4 expressions
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 declarations

Lists

valuestoList :: Stream r ix e => Array r ix e -> [e]
#

Convert an array to a list by the means of a delayed stream vector.

Related: toList

Examples
valuesfromList :: [e] -> Vector DS e
#

Convert a list to a delayed stream vector

Related: fromList, fromListN, sfromListN

Examples
Example2 expressions
sfromList ([] :: [Int])Array DS Seq (Sz1 0)  [  ]sfromList ([1,2,3] :: [Int])Array DS Seq (Sz1 3)  [ 1, 2, 3 ]
valuesfromListN :: Sz1 -> [e] -> Vector DS e
#

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
Example2 expressions
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 declarations
valuecompute :: (Manifest r e, Load r' ix e) => Array r' ix e -> Array r ix e
#

Ensure that Array is computed, i.e. represented with concrete elements in memory, hence is the Mutable type class restriction. Use setComp if you'd like to change computation strategy before calling compute

valuecomputeIO
  1. :: (Manifest r e, Load r' ix e, MonadIO m)
  2. => Array r' ix e
  3. -> m (Array r ix e)
#

Very similar to compute, but computes an array inside the IO monad. Despite being deterministic and referentially transparent, because this is an IO action it can be very useful for enforcing the order of evaluation. Should be a prefered way of computing an array during benchmarking.

valuecomputeAs
  1. :: (Manifest r e, Load r' ix e)
  2. => r
  3. -> Array r' ix e
  4. -> Array r ix e
#

Just as compute, but let's you supply resulting representation type as an argument.

Examples
Example2 expressions
import Data.Massiv.ArraycomputeAs P $ range Seq (Ix1 0) 10Array P Seq (Sz1 10)  [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
valuecomputeProxy
  1. :: (Manifest r e, Load r' ix e)
  2. => proxy r
  3. -> Array r' ix e
  4. -> Array r ix e
#

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:

Example3 expressions
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 ]
valueconvert :: (Manifest r e, Load r' ix e) => Array r' ix e -> Array r ix e
#

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.

Re-exports