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.