@@ -38,6 +38,7 @@ module Streamly.Internal.Data.Array.Generic
3838
3939 -- * Random Access
4040 , getIndexUnsafe
41+ , getSliceUnsafe
4142 , strip
4243 )
4344where
@@ -217,9 +218,9 @@ writeLastN n = FL.rmapM f (RB.writeLastN n)
217218 arr <- RB.toMutArray 0 n rb
218219 return $ unsafeFreeze arr
219220
220−{-# INLINE sliceUnsafe #-}
221−sliceUnsafe :: Int -> Int -> Array a -> Array a
222−sliceUnsafe offset len (Array cont off1 _) = Array cont (off1 + offset) len
221+{-# INLINE getSliceUnsafe #-}
222+getSliceUnsafe :: Int -> Int -> Array a -> Array a
223+getSliceUnsafe offset len (Array cont off1 _) = Array cont (off1 + offset) len
223224
224225-- XXX This is not efficient as it copies the array. We should support array
225226-- slicing so that we can just refer to the underlying array memory instead of
@@ -229,32 +230,7 @@ sliceUnsafe offset len (Array cont off1 _) = Array cont (off1 + offset) len
229230-- holds true. Returns a slice of the original array.
230231{-# INLINE strip #-}
231232strip :: (a -> Bool) -> Array a -> Array a
232−strip p arr =
233− let lastIndex = length arr - 1
234− indexR = getIndexR lastIndex -- last predicate failing index
235− in if indexR < 0
236− then nil
237− else
238− let indexL = getIndexL 0 -- first predicate failing index
239− in if indexL == 0 && indexR == lastIndex
240− then arr
241− else
242− let newLen = indexR - indexL + 1
243− in sliceUnsafe indexL newLen arr
244−
245− where
246−
247− getIndexR idx
248− | idx < 0 = idx
249− | otherwise =
250− if p (getIndexUnsafe idx arr)
251− then getIndexR (idx - 1)
252− else idx
253−
254− getIndexL idx =
255− if p (getIndexUnsafe idx arr)
256− then getIndexL (idx + 1)
257− else idx
233+strip p arr = unsafeFreeze $ unsafePerformIO $ MArray.strip p (unsafeThaw arr)
258234
259235-------------------------------------------------------------------------------
260236-- Instances