@@ -96,7 +96,11 @@ foldl' f z arr = runIdentity $ D.foldl' f z $ toStreamD arr
96foldr :: (a -> b -> b) -> b -> SmallArray a -> b
96foldr :: (a -> b -> b) -> b -> SmallArray a -> b
97foldr f z arr = runIdentity $ D.foldr f z $ toStreamD arr
97foldr f z arr = runIdentity $ D.foldr f z $ toStreamD arr
99−-- writeN n = S.evertM (fromStreamDN n)
99+-- | @writeN n@ folds a maximum of @n@ elements from the input stream to an
102+-- Since we are folding to a 'SmallArray' @n@ should be <= 128, for larger number
103+-- of elements use an 'Array' from either "Streamly.Data.Array" or "Streamly.Memory.Array".
100{-# INLINE_NORMAL writeN #-}
104{-# INLINE_NORMAL writeN #-}
101writeN :: MonadIO m => Int -> Fold m a (SmallArray a)
105writeN :: MonadIO m => Int -> Fold m a (SmallArray a)
102writeN limit = Fold step initial extract
106writeN limit = Fold step initial extract
@@ -122,6 +126,13 @@ fromStreamDN limit str = do
122 D.take limit str
126 D.take limit str
123 liftIO $ freezeSmallArray marr 0 i
127 liftIO $ freezeSmallArray marr 0 i
129+-- | Create a 'SmallArray' from the first @n@ elements of a list. The
130+-- array may hold less than @n@ elements if the length of the list <=
133+-- It is recommended to use a value of @n@ <= 128. For larger sized
134+-- arrays, use an 'Array' from "Streamly.Data.Array" or
135+-- "Streamly.Memory.Array"
125{-# INLINABLE fromListN #-}
136{-# INLINABLE fromListN #-}
126fromListN :: Int -> [a] -> SmallArray a
137fromListN :: Int -> [a] -> SmallArray a
127fromListN n xs = unsafePerformIO $ fromStreamDN n $ D.fromList xs
138fromListN n xs = unsafePerformIO $ fromStreamDN n $ D.fromList xs
@@ -130,6 +141,11 @@ instance NFData a => NFData (SmallArray a) where
130 {-# INLINE rnf #-}
141 {-# INLINE rnf #-}
131 rnf = foldl' (\_ x -> rnf x) ()
142 rnf = foldl' (\_ x -> rnf x) ()
144+-- | Create a 'SmallArray' from the first @n@ elements of a stream. The
145+-- array is allocated to size @n@, if the stream terminates before @n@
146+-- elements then the array may hold less than @n@ elements.
148+-- For optimal performance use this with @n@ <= 128.
133{-# INLINE fromStreamN #-}
149{-# INLINE fromStreamN #-}
134fromStreamN :: MonadIO m => Int -> SerialT m a -> m (SmallArray a)
150fromStreamN :: MonadIO m => Int -> SerialT m a -> m (SmallArray a)
135fromStreamN n m = do
151fromStreamN n m = do