on master help sign in
Return an array slice in "strip"
commit8d9a164f2ba4c74ab9b9e739eb5b69b95dcf572f authorHarendra Kumar <harendra@composewell.com> dateFri 10 Mar 2023, 19:35 (4 years ago) parent0bd3d4e
1 file changed+13−11
core/src/Streamly/Internal/Data/Array/Generic.hs+13−11
core/src/Streamly/Internal/Data/Array/Generic.hs+13−11
@@ -223,34 +223,36 @@ sliceUnsafe offset len (Array cont off1 _) = Array cont (off1 + offset) len
223223-- XXX This is not efficient as it copies the array. We should support array
224224-- slicing so that we can just refer to the underlying array memory instead of
225225-- copying.
226−--
226+
227227-- | Truncate the array at the beginning and end as long as the predicate
228−-- holds true.
228+-- holds true. Returns a slice of the original array.
229229strip :: (a -> Bool) -> Array a -> Array a
230230strip p arr =
231231 let lastIndex = length arr - 1
232232 indexR = getIndexR lastIndex -- last predicate failing index
233− in if indexR == -1
233+ in if indexR < 0
234234 then nil
235235 else
236236 let indexL = getIndexL 0 -- first predicate failing index
237237 in if indexL == 0 && indexR == lastIndex
238238 then arr
239− else unsafeFreeze $ unsafePerformIO $ do
239+ else
240240 let newLen = indexR - indexL + 1
241− arrThawed = unsafeThaw (sliceUnsafe indexL newLen arr)
242− MArray.clone arrThawed
241+ in sliceUnsafe indexL newLen arr
243242
244243 where
245244
246245 getIndexR idx
247246 | idx < 0 = idx
248247 | otherwise =
249− if p (getIndexUnsafe idx arr) then getIndexR (idx - 1) else idx
250−
251− getIndexL idx = if p (getIndexUnsafe idx arr)
252− then getIndexL (idx + 1)
253− else idx
248+ if p (getIndexUnsafe idx arr)
249+ then getIndexR (idx - 1)
250+ else idx
251+
252+ getIndexL idx =
253+ if p (getIndexUnsafe idx arr)
254+ then getIndexL (idx + 1)
255+ else idx
254256
255257-------------------------------------------------------------------------------
256258-- Instances