@@ -60,7 +60,7 @@ module Streamly.Internal.Data.Array.Foreign
6060 , A.writeN -- drop new
6161 , A.writeNAligned
6262 , A.write -- full buffer
63− -- , writeLastN -- drop old (ring buffer)
63+ , writeLastN
6464
6565 -- * Elimination
6666
@@ -78,7 +78,7 @@ module Streamly.Internal.Data.Array.Foreign
7878 , null
7979 , last
8080 -- , (!!)
81− , readIndex
81+ , getIndex
8282 , A.unsafeIndex
8383 -- , readIndices
8484 -- , readRanges
@@ -130,15 +130,12 @@ module Streamly.Internal.Data.Array.Foreign
130130 , cast
131131 , unsafeCast
132132 , asPtr
133− , asByteArray
133+ , asBytes
134134 , asCString
135135
136136 -- * Folding Arrays
137137 , streamFold
138138 , fold
139−
140− -- * Folds with Array as the container
141− , lastN
142139 )
143140where
144141
@@ -273,34 +270,43 @@ unsafeRead = Unfold step inject
273270null :: Storable a => Array a -> Bool
274271null arr = length arr == 0
275272
276−-- | > last arr = readIndex arr (length arr - 1)
273+-- | > last arr = getIndex arr (length arr - 1)
277274--
278275-- /Pre-release/
279276{-# INLINE last #-}
280277last :: Storable a => Array a -> Maybe a
281−last arr = readIndex arr (length arr - 1)
278+last arr = getIndex arr (length arr - 1)
282279
283280-------------------------------------------------------------------------------
284281-- Folds with Array as the container
285282-------------------------------------------------------------------------------
286283
287−-- | Take last 'n' elements from the stream and discard the rest.
288−{-# INLINE lastN #-}
289−lastN :: (Storable a, MonadIO m) => Int -> Fold m a (Array a)
290−lastN n
284+-- | @writeLastN n@ folds a maximum of @n@ elements from the end of the input
285+-- stream to an 'Array'.
286+--
287+-- @since 0.8.0
288+{-# INLINE writeLastN #-}
289+writeLastN :: (Storable a, MonadIO m) => Int -> Fold m a (Array a)
290+writeLastN n
291291 | n <= 0 = fmap (const mempty) FL.drain
292292 | otherwise = A.unsafeFreeze <$> Fold step initial done
293− where
293+
294+ where
295+
294296 step (Tuple3' rb rh i) a = do
295297 rh1 <- liftIO $ RB.unsafeInsert rb rh a
296298 return $ FL.Partial $ Tuple3' rb rh1 (i + 1)
299+
297300 initial =
298301 let f (a, b) = FL.Partial $ Tuple3' a b (0 :: Int)
299302 in fmap f $ liftIO $ RB.new n
303+
300304 done (Tuple3' rb rh i) = do
301305 arr <- liftIO $ MA.newArray n
302306 foldFunc i rh snoc' arr rb
307+
303308 snoc' b a = liftIO $ MA.unsafeSnoc b a
309+
304310 foldFunc i
305311 | i < n = RB.unsafeFoldRingM
306312 | otherwise = RB.unsafeFoldRingFullM
@@ -425,10 +431,10 @@ foldbWith level f = undefined
425431
426432-- | /O(1)/ Lookup the element at the given index, starting from 0.
427433--
428−-- /Pre-release/
429−{-# INLINE readIndex #-}
430−readIndex :: Storable a => Array a -> Int -> Maybe a
431−readIndex arr i =
434+-- @since 0.8.0
435+{-# INLINE getIndex #-}
436+getIndex :: Storable a => Array a -> Int -> Maybe a
437+getIndex arr i =
432438 if i < 0 || i > length arr - 1
433439 then Nothing
434440 else A.unsafeInlineIO $
@@ -543,18 +549,18 @@ unsafeCast ::
543549 Array a -> Array b
544550unsafeCast (Array start end) = Array (castForeignPtr start) (castPtr end)
545551
546−-- | Cast an array into a Word8 array
552+-- | Cast an @Array a@ into an @Array Word8@.
547553--
548−-- /Pre-release/
554+-- @since 0.8.0
549555--
550−asByteArray :: Array a -> Array Word8
551−asByteArray = unsafeCast
556+asBytes :: Array a -> Array Word8
557+asBytes = unsafeCast
552558
553559-- | Cast an array having elements of type @a@ into an array having elements of
554560-- type @b@. The length of the array should be a multiple of the size of the
555561-- target element otherwise 'Nothing' is returned.
556562--
557−-- /Pre-release/
563+-- @since 0.8.0
558564--
559565cast :: forall a b. (Storable b) => Array a -> Maybe (Array b)
560566cast arr =
@@ -584,7 +590,7 @@ asPtr Array{..} act = do
584590--
585591asCString :: Array a -> (CString -> IO b) -> IO b
586592asCString arr act = do
587− let Array{..} = asByteArray arr <> A.fromList [0]
593+ let Array{..} = asBytes arr <> A.fromList [0]
588594 withForeignPtr aStart $ \ptr -> act (castPtr ptr)
589595
590596-------------------------------------------------------------------------------