on master help sign in
Add fromList to the show instance of unboxed arrays
commita74f291c4d7880e02903abdef444b5b348f2b55d authorAdithya Kumar <adithya@composewell.com> dateFri 4 Nov 2022, 15:51 (4 years ago) parentbb010e4
4 files changed+16−11
core/src/Streamly/Data/Array.hs+2−2 core/src/Streamly/Internal/Data/Array/Type.hs+10−5 core/src/Streamly/Internal/Unicode/Array/Char.hs+2−2 src/Streamly/Data/Array/Foreign.hs+2−2
core/src/Streamly/Data/Array.hs+2−2
@@ -30,14 +30,14 @@
3030-- >>> s = Stream.fromList [1..10] :: Stream Identity Int
3131-- >>> s1 = Stream.hoist (return . runIdentity) s :: Stream IO Int
3232-- >>> Stream.fold Array.write s1 :: IO (Array Int)
33−-- [1,2,3,4,5,6,7,8,9,10]
33+-- fromList [1,2,3,4,5,6,7,8,9,10]
3434--
3535-- 'unsafePerformIO' can be used to get a pure API from IO, as long as you know
3636-- it is safe to do so:
3737--
3838-- >>> import System.IO.Unsafe (unsafePerformIO)
3939-- >>> unsafePerformIO $ Stream.fold Array.write s1 :: Array Int
40−-- [1,2,3,4,5,6,7,8,9,10]
40+-- fromList [1,2,3,4,5,6,7,8,9,10]
4141--
4242-- To apply a transformation to an array use 'read' to unfold the array into a
4343-- stream, apply a transformation on the stream and then use 'write' to fold it
core/src/Streamly/Internal/Data/Array/Type.hs+10−5
@@ -79,6 +79,7 @@ where
7979
8080import Control.Exception (assert)
8181import Control.DeepSeq (NFData(..), NFData1(..))
82+import Control.Monad (replicateM)
8283import Control.Monad.IO.Class (MonadIO(..))
8384import Data.Functor.Identity (Identity(..))
8485import Data.Word (Word8)
@@ -92,7 +93,7 @@ import Streamly.Internal.Data.Fold.Type (Fold(..))
9293import Streamly.Internal.Data.Stream.Type (Stream)
9394import Streamly.Internal.Data.Unboxed (Unbox, peekWith, sizeOf)
9495import Streamly.Internal.Data.Unfold.Type (Unfold(..))
95−import Text.Read (readPrec, readListPrec, readListPrecDefault)
96+import Text.Read (readPrec)
9697
9798import Prelude hiding (length, foldr, read, unlines, splitAt)
9899
@@ -103,6 +104,7 @@ import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
103104import qualified Streamly.Internal.Data.Stream.Type as Stream
104105import qualified Streamly.Internal.Data.Unboxed as Unboxed
105106import qualified Streamly.Internal.Data.Unfold.Type as Unfold
107+import qualified Text.ParserCombinators.ReadPrec as ReadPrec
106108
107109import Streamly.Internal.System.IO (unsafeInlineIO, defaultChunkSize)
108110
@@ -481,13 +483,16 @@ write = fmap unsafeFreeze MA.write
481483-------------------------------------------------------------------------------
482484
483485instance (Show a, Unbox a) => Show (Array a) where
484− {-# INLINE showsPrec #-}
485− showsPrec _ = shows . toList
486+ {-# INLINE show #-}
487+ show arr = "fromList " ++ show (toList arr)
486488
487489instance (Unbox a, Read a, Show a) => Read (Array a) where
488490 {-# INLINE readPrec #-}
489− readPrec = fromList <$> readPrec
490− readListPrec = readListPrecDefault
491+ readPrec = do
492+ fromListWord <- replicateM 9 ReadPrec.get
493+ if fromListWord == "fromList "
494+ then fromList <$> readPrec
495+ else ReadPrec.pfail
491496
492497instance (a ~ Char) => IsString (Array a) where
493498 {-# INLINE fromString #-}
core/src/Streamly/Internal/Unicode/Array/Char.hs+2−2
@@ -45,7 +45,7 @@ import Prelude hiding (String, lines, words, unlines, unwords)
4545-- > lines = S.lines A.write
4646--
4747-- >>> Stream.fold Fold.toList $ Unicode.lines $ Stream.fromList "lines\nthis\nstring\n\n\n"
48−-- ["lines","this","string","",""]
48+-- [fromList "lines",fromList "this",fromList "string",fromList "",fromList ""]
4949--
5050{-# INLINE lines #-}
5151lines :: MonadIO m => Stream m Char -> Stream m (Array Char)
@@ -57,7 +57,7 @@ lines = S.lines A.write
5757-- > words = S.words A.write
5858--
5959-- >>> Stream.fold Fold.toList $ Unicode.words $ Stream.fromList "A newline\nis considered white space?"
60−-- ["A","newline","is","considered","white","space?"]
60+-- [fromList "A",fromList "newline",fromList "is",fromList "considered",fromList "white",fromList "space?"]
6161--
6262{-# INLINE words #-}
6363words :: MonadIO m => Stream m Char -> Stream m (Array Char)
src/Streamly/Data/Array/Foreign.hs+2−2
@@ -32,14 +32,14 @@
3232-- >>> s = Stream.fromList [1..10] :: Stream Identity Int
3333-- >>> s1 = Stream.hoist (return . runIdentity) s :: Stream IO Int
3434-- >>> Stream.fold Array.write s1 :: IO (Array Int)
35−-- [1,2,3,4,5,6,7,8,9,10]
35+-- fromList [1,2,3,4,5,6,7,8,9,10]
3636--
3737-- 'unsafePerformIO' can be used to get a pure API from IO, as long as you know
3838-- it is safe to do so:
3939--
4040-- >>> import System.IO.Unsafe (unsafePerformIO)
4141-- >>> unsafePerformIO $ Stream.fold Array.write s1 :: Array Int
42−-- [1,2,3,4,5,6,7,8,9,10]
42+-- fromList [1,2,3,4,5,6,7,8,9,10]
4343--
4444-- To apply a transformation to an array use 'read' to unfold the array into a
4545-- stream, apply a transformation on the stream and then use 'write' to fold it