Fix indexMaybe and add more utility functions

This commit is contained in:
Kiana Sheibani 2022-06-15 22:37:34 -04:00
parent de66efe75b
commit b0d48eaf00
Signed by: toki
GPG key ID: 6CB106C25E86A9F7
3 changed files with 46 additions and 12 deletions

View file

@ -59,7 +59,6 @@ create size f = unsafePerformIO $ do
= do arrayDataSet loc (f loc) arr
addToArray (S loc) n arr
||| Index into a primitive array. This function is unsafe, as it
||| performs no boundary check on the index given.
export
@ -73,6 +72,18 @@ safeIndex n arr = if n < length arr
then Just $ index n arr
else Nothing
export
copy : PrimArray a -> PrimArray a
copy arr = create (length arr) (\n => index n arr)
export
updateAt : Nat -> (a -> a) -> PrimArray a -> PrimArray a
updateAt n f arr = if n >= length arr then arr else
unsafePerformIO $ do
let cpy = copy arr
x <- arrayDataGet n cpy.content
arrayDataSet n (f x) cpy.content
pure cpy
||| Convert a primitive array to a list.
export