Skip to content

Commit

Permalink
Expose getIndex from Array.Generic
Browse files Browse the repository at this point in the history
  • Loading branch information
rnjtranjan committed May 24, 2023
1 parent b608366 commit dea3d5f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions core/src/Streamly/Data/Array/Generic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module Streamly.Data.Array.Generic

-- * Random Access
, A.length
, A.getIndex

-- -- * Folding Arrays
-- , A.streamFold
Expand Down
11 changes: 11 additions & 0 deletions core/src/Streamly/Internal/Data/Array/Generic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ module Streamly.Internal.Data.Array.Generic

-- * Random Access
, getIndexUnsafe
, getIndex
, getSliceUnsafe
, strip
)
Expand Down Expand Up @@ -208,6 +209,16 @@ getIndexUnsafe :: Int -> Array a -> a
getIndexUnsafe i arr =
unsafePerformIO $ MArray.getIndexUnsafe i (unsafeThaw arr)

invalidIndex :: String -> Int -> a
invalidIndex label i =
error $ label ++ ": invalid array index " ++ show i

getIndex :: Int -> Array a -> a
getIndex i arr@Array {..} =
if i >= 0 && i < arrLen
then getIndexUnsafe i arr
else invalidIndex "getIndex" i

{-# INLINE writeLastN #-}
writeLastN :: MonadIO m => Int -> Fold m a (Array a)
writeLastN n = FL.rmapM f (RB.writeLastN n)
Expand Down

0 comments on commit dea3d5f

Please sign in to comment.