Fix crash when folding an empty array

This commit is contained in:
Kiana Sheibani 2022-09-20 13:47:26 -04:00
parent 09901977c2
commit d3f5ad3bda
Signed by: toki
GPG key ID: 6CB106C25E86A9F7

View file

@ -137,7 +137,9 @@ unzipWith3 f arr = (map ((\(x,_,_) => x) . f) arr,
export
foldl : (b -> a -> b) -> b -> PrimArray a -> b
foldl f z (MkPrimArray size arr) = unsafePerformIO $ do
foldl f z (MkPrimArray size arr) =
if size == 0 then z
else unsafePerformIO $ do
ref <- newIORef z
for_ [0..pred size] $ \n => do
x <- readIORef ref
@ -147,7 +149,9 @@ foldl f z (MkPrimArray size arr) = unsafePerformIO $ do
export
foldr : (a -> b -> b) -> b -> PrimArray a -> b
foldr f z (MkPrimArray size arr) = unsafePerformIO $ do
foldr f z (MkPrimArray size arr) =
if size == 0 then z
else unsafePerformIO $ do
ref <- newIORef z
for_ [pred size..0] $ \n => do
x <- arrayDataGet n arr