Module

Data.FastVect.FastVect

Package
purescript-fast-vect
Repository
sigma-andex/purescript-fast-vect

#(:) Source

Operator alias for Data.FastVect.FastVect.cons (right-associative / precedence 6)

#Vect Source

newtype Vect :: Int -> Type -> Typenewtype Vect len elem

A Vector: A list-like data structure that encodes it's length in the type, backed by an Array.

vect :: Vect 1 String
vect = singleton "a"

Instances

#adjust Source

adjust :: forall len elem. Reflectable len Int => Compare len NegOne GT => Proxy len -> elem -> Array elem -> Vect len elem

Creates a Vect by adjusting the given Array, padding with the provided element if the array is to small or dropping elements if the array is to big.

toArray $ adjust (Common.term :: _ 10) 0 [ 1, 2, 3 ] == [ 0, 0, 0, 0, 0, 0, 0, 1, 2, 3 ]

toArray $ adjust (Common.term :: _ 3) 0 [ 0, 0, 0, 0, 1, 2, 3 ] == [ 1, 2, 3 ]

#adjustM Source

adjustM :: forall len elem. Monoid elem => Reflectable len Int => Compare len NegOne GT => Proxy len -> Array elem -> Vect len elem

Like adjust but uses the Moinoid instance of elem to create the elements.

#append Source

append :: forall m n m_plus_n elem. Append Vect m n m_plus_n elem

Append two Vects.

as :: Vect 300 String
as = replicate (Common.term :: _ 300) "a"

bs :: Vect 200 String
bs = replicate (Common.term :: _ 200) "b"

cs :: Vect 500 String
cs = append as bs

#cons Source

cons :: forall len len_plus_1 elem. Cons Vect len len_plus_1 elem

Attaches an element to the front of the Vect, creating a new Vect with size incremented.

Note, the running time of this function is O(n).

#drop Source

drop :: forall m n m_plus_n elem. Drop Vect m n m_plus_n elem

Safely drop m elements from a Vect. Will result in a compile-time error if you are trying to drop more elements than exist in the vector.

vect :: Vect 300 String
vect = replicate (Common.term :: _ 300) "a"

newVect :: Vect 200 String
newVect = drop (Common.term :: _ 100) vect

#empty Source

empty :: forall elem. Empty Vect elem

Creates the empty Vect.

vect :: Vect 0 String
vect = empty

#fromArray Source

fromArray :: forall len elem. Reflectable len Int => Compare len NegOne GT => Proxy len -> Array elem -> Maybe (Vect len elem)

Attempt to create a Vect of a given size from an Array.

fromArray (Common.term :: _ 3) ["a", "b", "c"] = Just (Vect (Common.term :: _ 3) ["a", "b", "c"])

fromArray (Common.term :: _ 4) ["a", "b", "c"] = Nothing

#generate Source

generate :: forall len elem. Generate Vect len elem

Generate a Vect of the given size by applying a function to each type level index.

#head Source

head :: forall m elem. Head Vect m elem

Safely access the head of a Vect.

vect :: Vect 300 String
vect = replicate (Common.term :: _ 300) "a"

elem :: String
elem = head vect

#last Source

last :: forall m elem. Last Vect m elem

Safely access the last element of a Vect.

vect :: Vect 300 String
vect = replicate (Common.term :: _ 300) "a"

elem :: String
elem = last vect

#index Source

index :: forall m n elem. Index Vect m n elem

Safely access the i-th element of a Vect.

vect :: Vect 300 String
vect = replicate (Common.term :: _ 300) "a"

elem :: String
elem = index (Common.term :: _ 299) vect

#indexModulo Source

indexModulo :: forall m elem. IndexModulo Vect m elem

Safely access the n-th modulo m element of a Vect.

vect :: Vect 300 String
vect = replicate (Common.term :: _ 300) "a"

elem :: String
elem = indexModulo 5352523 vect

#mapWithTerm Source

mapWithTerm :: forall len elem elem'. MapWithTerm Vect len elem elem'

Map a function over a Vect with the type level index of each element.

#modify Source

modify :: forall m n elem. Modify Vect m n elem

Safely modify element m from a Vect.

vect :: Vect 300 String
vect = replicate (Common.term :: _ 300) "a"

newVect :: Vect 100 String
newVect = modify (Common.term :: _ 100) (append "b") vect

#reifyVect Source

reifyVect :: forall elem r. Array elem -> (forall len. Vect len elem -> r) -> r

Applies a function to Array that takes a Vector of arbitrary length.

#replicate Source

replicate :: forall len elem. Replicate Vect len elem

Create a Vect by replicating len times the given element

vect :: Vect 300 String
vect = replicate (Common.term :: _ 300) "a"

#set Source

set :: forall m n elem. Set Vect m n elem

Safely set element m from a Vect.

vect :: Vect 300 String
vect = replicate (Common.term :: _ 300) "a"

newVect :: Vect 100 String
newVect = modify (Common.term :: _ 100) "b" vect
`

#singleton Source

singleton :: forall elem. Singleton Vect elem

Create a Vect of one element.

vect :: Vect 1 String
vect = singleton "a"

#snoc Source

snoc :: forall len len_plus_1 elem. Snoc Vect len len_plus_1 elem

Attaches an element to the end of the Vect, creating a new Vect with size incremented.

#splitAt Source

splitAt :: forall m n m_plus_n elem. SplitAt Vect m n m_plus_n elem

Split the Vect into two sub vectors before and after, where before contains up to m elements.

vect :: Vect 10 String
vect = replicate (Common.term :: _ 10) "a"

split ::
  { after :: Vect 7 String
  , before :: Vect 3 String
  }
split = splitAt (Common.term :: _ 3) vect

#take Source

take :: forall m n m_plus_n elem. Take Vect m n m_plus_n elem

Safely take m elements from a Vect. Will result in a compile-time error if you are trying to take more elements than exist in the vector.

vect :: Vect 300 String
vect = replicate (Common.term :: _ 300) "a"

newVect :: Vect 100 String
newVect = take (Common.term :: _ 100) vect

#toArray Source

toArray :: forall len elem. Compare len NegOne GT => Vect len elem -> Array elem

Converts the Vect to an Array, effectively dropping the size information.

#toNonEmptyArray Source

toNonEmptyArray :: forall len elem. Compare len Zero GT => Vect len elem -> NonEmptyArray elem

Converts the Vect to an NonEmptyArray, dropping most of the size information.