Module

Data.Array.Indexed

Package
purescript-arrays-indexed
Repository
rowtype-yoga/purescript-arrays-indexed

Arrays branded with a type s, and indices that are valid for exactly that array.

smoothed :: Array Number -> Array Number
smoothed signal = withIndexed signal \xs ->
  toArray $ xs # mapWithIndex \i x -> case next i xs of
    Just j -> (x + at j xs) / 2.0
    Nothing -> x

withIndexed gives the array a brand s that nothing else shares. An Index s can only come from that array, so at needs no Maybe.

#Indexed Source

newtype Indexed s a

An array carrying the brand s.

Instances

#Index Source

newtype Index s

A position that is valid in every array carrying the brand s.

Instances

#withIndexed Source

withIndexed :: forall a r. Array a -> (forall s. Indexed s a -> r) -> r

Brands an array. The brand can't leave the function you pass.

#sameLength Source

sameLength :: forall a b r. Array a -> Array b -> (forall s. Indexed s a -> Indexed s b -> r) -> Maybe r

Brands two arrays with the same brand if they have the same length, so one index works for both.

#at Source

at :: forall s a. Index s -> Indexed s a -> a

The element at a position. Always there.

#index Source

index :: forall s a. Int -> Indexed s a -> Maybe (Index s)

Checks an Int once and hands back an index you can use from then on.

#toInt Source

toInt :: forall s. Index s -> Int

Forgets that an index is valid.

#indices Source

indices :: forall s a. Indexed s a -> Array (Index s)

Every position, in order.

#length Source

length :: forall s a. Indexed s a -> Int

#first Source

first :: forall s a. Indexed s a -> Maybe (Index s)

#last Source

last :: forall s a. Indexed s a -> Maybe (Index s)

#next Source

next :: forall s a. Index s -> Indexed s a -> Maybe (Index s)

The position after this one, unless this is the last.

#prev Source

prev :: forall s a. Index s -> Indexed s a -> Maybe (Index s)

The position before this one, unless this is the first.

#zipWith Source

zipWith :: forall s a b c. (a -> b -> c) -> Indexed s a -> Indexed s b -> Indexed s c

Zips two arrays of the same brand. They have the same length, so nothing is dropped.

#toArray Source

toArray :: forall s a. Indexed s a -> Array a

Forgets the brand.