@@ -44,6 +44,7 @@ module Streamly.Internal.Data.Array.Type
4444 , fromByteStr#
4545 , fromByteStr
4646 , fromPtrN
47+ , fromArrayStreamK
4748
4849 -- ** Split
4950 , breakOn
@@ -117,6 +118,7 @@ import GHC.Ptr (Ptr(..))
117118import Streamly.Internal.Data.MutArray.Type (MutArray(..), MutByteArray)
118119import Streamly.Internal.Data.Fold.Type (Fold(..))
119120import Streamly.Internal.Data.Stream.Type (Stream)
121+import Streamly.Internal.Data.StreamK.Type (StreamK)
120122import Streamly.Internal.Data.Unbox (Unbox(..))
121123import Streamly.Internal.Data.Unfold.Type (Unfold(..))
122124import Text.Read (readPrec)
@@ -262,9 +264,9 @@ isPinned = MA.isPinned . unsafeThaw
262264-- would make a copy on every splice operation, instead use the
263265-- 'fromArrayStreamK' operation to combine n immutable arrays.
264266{-# INLINE splice #-}
265−splice :: (MonadIO m, Unbox a) => Array a -> Array a -> m (Array a)
267+splice :: MonadIO m => Array a -> Array a -> m (Array a)
266268splice arr1 arr2 =
267− unsafeFreeze <$> MA.splice (unsafeThaw arr1) (unsafeThaw arr2)
269+ unsafeFreeze <$> MA.spliceCopy (unsafeThaw arr1) (unsafeThaw arr2)
268270
269271-- | Create an 'Array' from the first N elements of a list. The array is
270272-- allocated to size N, if the list terminates before N elements then the
@@ -663,6 +665,16 @@ fromByteStr# addr = unsafePerformIO $ fmap unsafeFreeze (MA.fromByteStr# addr)
663665fromByteStr :: Ptr Word8 -> Array Word8
664666fromByteStr (Ptr addr#) = fromByteStr# addr#
665667
668+-- XXX implement fromArrayStream/fromArrayList instead?
669+
670+-- | Convert an array stream to an array. Note that this requires peak memory
671+-- that is double the size of the array stream.
672+--
673+{-# INLINE fromArrayStreamK #-}
674+fromArrayStreamK :: (Unbox a, MonadIO m) => StreamK m (Array a) -> m (Array a)
675+fromArrayStreamK stream =
676+ fmap unsafeFreeze $ MA.fromArrayStreamK $ fmap unsafeThaw stream
677+
666678-------------------------------------------------------------------------------
667679-- Instances
668680-------------------------------------------------------------------------------
@@ -815,6 +827,12 @@ instance Foldable Array where
815827-- Semigroup and Monoid
816828-------------------------------------------------------------------------------
817829
830+-- XXX Deprecate and remove the Semigroup and Monoid instances because of
831+-- potential misuse chances.
832+
833+-- | This should not be used for combining many or N arrays as it would copy
834+-- the two arrays everytime to a new array. For coalescing multiple arrays use
835+-- 'fromArrayStreamK' instead.
818836instance Unbox a => Semigroup (Array a) where
819837 arr1 <> arr2 = unsafePerformIO $ splice arr1 arr2
820838