Module

Data.Array.ST.Indexed

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

A mutable array whose length can't change, with indices that are valid for exactly that array.

reversed :: forall a. Array a -> Array a
reversed = withArray \arr -> go (first arr) (last arr) arr
  where
  go :: forall s h. Maybe (Index s) -> Maybe (Index s) -> Fixed s h a -> ST h Unit
  go (Just i) (Just j) arr | i < j = do
    swap i j arr
    go (next i arr) (prev j arr) arr
  go _ _ _ = pure unit

withFixed copies an array into a fresh mutable one and brands it with s. You only ever see it as a Fixed s h a, which can be read and written but never grown or shrunk, so every Index s stays valid.

#Fixed Source

data Fixed :: Type -> Region -> Type -> Typedata Fixed s h a

A mutable array of fixed length in region h, carrying the brand s.

#withFixed Source

withFixed :: forall h a r. Array a -> (forall s. Fixed s h a -> ST h r) -> ST h ((Array a) /\ r)

Copies the array, lets you work on the copy, and hands it back with your result. The brand can't leave the function you pass.

#withArray Source

withArray :: forall a r. (forall s h. Fixed s h a -> ST h r) -> Array a -> Array a

Runs withFixed on its own and keeps only the array.

#read Source

read :: forall s h a. Index s -> Fixed s h a -> ST h a

#write Source

write :: forall s h a. Index s -> a -> Fixed s h a -> ST h Unit

#modify Source

modify :: forall s h a. Index s -> (a -> a) -> Fixed s h a -> ST h Unit

#swap Source

swap :: forall s h a. Index s -> Index s -> Fixed s h a -> ST h Unit

Exchanges two elements.

#index Source

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

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

#indices Source

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

Every position, in order.

#length Source

length :: forall s h a. Fixed s h a -> Int

#first Source

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

#last Source

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

#next Source

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

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

#prev Source

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

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