on master help sign in
Add fromList to the show instance of unboxed arrays
a74f291Adithya Kumar
core/src/Streamly/Data/Array.hs+2−2
@@ -30,14 +30,14 @@
30-- >>> s = Stream.fromList [1..10] :: Stream Identity Int
30-- >>> s = Stream.fromList [1..10] :: Stream Identity Int
31-- >>> s1 = Stream.hoist (return . runIdentity) s :: Stream IO Int
31-- >>> s1 = Stream.hoist (return . runIdentity) s :: Stream IO Int
32-- >>> Stream.fold Array.write s1 :: IO (Array Int)
32-- >>> 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]
34--
34--
35-- 'unsafePerformIO' can be used to get a pure API from IO, as long as you know
35-- 'unsafePerformIO' can be used to get a pure API from IO, as long as you know
36-- it is safe to do so:
36-- it is safe to do so:
37--
37--
38-- >>> import System.IO.Unsafe (unsafePerformIO)
38-- >>> import System.IO.Unsafe (unsafePerformIO)
39-- >>> unsafePerformIO $ Stream.fold Array.write s1 :: Array Int
39-- >>> 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]
41--
41--
42-- To apply a transformation to an array use 'read' to unfold the array into a
42-- To apply a transformation to an array use 'read' to unfold the array into a
43-- stream, apply a transformation on the stream and then use 'write' to fold it
43-- 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
79
79
80import Control.Exception (assert)
80import Control.Exception (assert)
81import Control.DeepSeq (NFData(..), NFData1(..))
81import Control.DeepSeq (NFData(..), NFData1(..))
82+import Control.Monad (replicateM)
82import Control.Monad.IO.Class (MonadIO(..))
83import Control.Monad.IO.Class (MonadIO(..))
83import Data.Functor.Identity (Identity(..))
84import Data.Functor.Identity (Identity(..))
84import Data.Word (Word8)
85import Data.Word (Word8)
@@ -92,7 +93,7 @@ import Streamly.Internal.Data.Fold.Type (Fold(..))
92import Streamly.Internal.Data.Stream.Type (Stream)
93import Streamly.Internal.Data.Stream.Type (Stream)
93import Streamly.Internal.Data.Unboxed (Unbox, peekWith, sizeOf)
94import Streamly.Internal.Data.Unboxed (Unbox, peekWith, sizeOf)
94import Streamly.Internal.Data.Unfold.Type (Unfold(..))
95import Streamly.Internal.Data.Unfold.Type (Unfold(..))
95−import Text.Read (readPrec, readListPrec, readListPrecDefault)
96+import Text.Read (readPrec)
96
97
97import Prelude hiding (length, foldr, read, unlines, splitAt)
98import Prelude hiding (length, foldr, read, unlines, splitAt)
98
99
@@ -103,6 +104,7 @@ import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
103import qualified Streamly.Internal.Data.Stream.Type as Stream
104import qualified Streamly.Internal.Data.Stream.Type as Stream
104import qualified Streamly.Internal.Data.Unboxed as Unboxed
105import qualified Streamly.Internal.Data.Unboxed as Unboxed
105import qualified Streamly.Internal.Data.Unfold.Type as Unfold
106import qualified Streamly.Internal.Data.Unfold.Type as Unfold
107+import qualified Text.ParserCombinators.ReadPrec as ReadPrec
106
108
107import Streamly.Internal.System.IO (unsafeInlineIO, defaultChunkSize)
109import Streamly.Internal.System.IO (unsafeInlineIO, defaultChunkSize)
108
110
@@ -481,13 +483,16 @@ write = fmap unsafeFreeze MA.write
481-------------------------------------------------------------------------------
483-------------------------------------------------------------------------------
482
484
483instance (Show a, Unbox a) => Show (Array a) where
485instance (Show a, Unbox a) => Show (Array a) where
484− {-# INLINE showsPrec #-}
486+ {-# INLINE show #-}
485− showsPrec _ = shows . toList
487+ show arr = "fromList " ++ show (toList arr)
486
488
487instance (Unbox a, Read a, Show a) => Read (Array a) where
489instance (Unbox a, Read a, Show a) => Read (Array a) where
488 {-# INLINE readPrec #-}
490 {-# INLINE readPrec #-}
491+ readPrec = do
492+ fromListWord <- replicateM 9 ReadPrec.get
493+ if fromListWord == "fromList "
489− readPrec = fromList <$> readPrec
494+ then fromList <$> readPrec
490− readListPrec = readListPrecDefault
495+ else ReadPrec.pfail
491
496
492instance (a ~ Char) => IsString (Array a) where
497instance (a ~ Char) => IsString (Array a) where
493 {-# INLINE fromString #-}
498 {-# INLINE fromString #-}
core/src/Streamly/Internal/Unicode/Array/Char.hs+2−2
@@ -45,7 +45,7 @@ import Prelude hiding (String, lines, words, unlines, unwords)
45-- > lines = S.lines A.write
45-- > lines = S.lines A.write
46--
46--
47-- >>> Stream.fold Fold.toList $ Unicode.lines $ Stream.fromList "lines\nthis\nstring\n\n\n"
47-- >>> 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 ""]
49--
49--
50{-# INLINE lines #-}
50{-# INLINE lines #-}
51lines :: MonadIO m => Stream m Char -> Stream m (Array Char)
51lines :: MonadIO m => Stream m Char -> Stream m (Array Char)
@@ -57,7 +57,7 @@ lines = S.lines A.write
57-- > words = S.words A.write
57-- > words = S.words A.write
58--
58--
59-- >>> Stream.fold Fold.toList $ Unicode.words $ Stream.fromList "A newline\nis considered white space?"
59-- >>> 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?"]
61--
61--
62{-# INLINE words #-}
62{-# INLINE words #-}
63words :: MonadIO m => Stream m Char -> Stream m (Array Char)
63words :: MonadIO m => Stream m Char -> Stream m (Array Char)
src/Streamly/Data/Array/Foreign.hs+2−2
@@ -32,14 +32,14 @@
32-- >>> s = Stream.fromList [1..10] :: Stream Identity Int
32-- >>> s = Stream.fromList [1..10] :: Stream Identity Int
33-- >>> s1 = Stream.hoist (return . runIdentity) s :: Stream IO Int
33-- >>> s1 = Stream.hoist (return . runIdentity) s :: Stream IO Int
34-- >>> Stream.fold Array.write s1 :: IO (Array Int)
34-- >>> 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]
36--
36--
37-- 'unsafePerformIO' can be used to get a pure API from IO, as long as you know
37-- 'unsafePerformIO' can be used to get a pure API from IO, as long as you know
38-- it is safe to do so:
38-- it is safe to do so:
39--
39--
40-- >>> import System.IO.Unsafe (unsafePerformIO)
40-- >>> import System.IO.Unsafe (unsafePerformIO)
41-- >>> unsafePerformIO $ Stream.fold Array.write s1 :: Array Int
41-- >>> 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]
43--
43--
44-- To apply a transformation to an array use 'read' to unfold the array into a
44-- To apply a transformation to an array use 'read' to unfold the array into a
45-- stream, apply a transformation on the stream and then use 'write' to fold it
45-- stream, apply a transformation on the stream and then use 'write' to fold it