on master help sign in
Merge pull request #257 from composewell/misc-refactorings
6c4abc8Harendra Kumar
Changelog.md+5−5
@@ -22,12 +22,12 @@
22 * `runN` has been replaced by `drainN`
22 * `runN` has been replaced by `drainN`
23 * `runWhile` has been replaced by `drainWhile`
23 * `runWhile` has been replaced by `drainWhile`
24 * `fromHandle` has been deprecated. Please use
24 * `fromHandle` has been deprecated. Please use
25− `Streamly.FileSystem.Handle.read`, `Streamly.String.decodeUtf8` and
25+ `Streamly.FileSystem.Handle.read`, `Streamly.Data.String.decodeUtf8` and
26− `Streamly.String.lines` to split the stream into lines. Use
26+ `Streamly.Data.String.lines` to split the stream into lines. Use
27− `Streamly.String.foldLine` with `Streamly.Fold.toList` if you want to
27+ `Streamly.Data.String.foldLine` with `Streamly.Fold.toList` if you want to
28 convert the stream to a stream of `String`.
28 convert the stream to a stream of `String`.
29 * `toHandle` has been deprecated. Please use `intersperse` and `concat` to
29 * `toHandle` has been deprecated. Please use `intersperse` and `concat` to
30− add newlines to a stream, `Streamly.String.encodeUtf8` for encoding and
30+ add newlines to a stream, `Streamly.Data.String.encodeUtf8` for encoding and
31 `Streamly.FileSystem.Handle.write` for writing to a file handle.
31 `Streamly.FileSystem.Handle.write` for writing to a file handle.
32 * Deprecate `scanx`, `foldx`, `foldxM`, `foldr1`
32 * Deprecate `scanx`, `foldx`, `foldxM`, `foldr1`
33 * Remove deprecated APIs `scan`, `foldl`, `foldlM`
33 * Remove deprecated APIs `scan`, `foldl`, `foldlM`
@@ -79,7 +79,7 @@ example, concat streams concurrently using this.
79### Other Enhancements
79### Other Enhancements
80
80
81* Add the following new features/modules:
81* Add the following new features/modules:
82− * _Unicode Strings_: `Streamly.String` module provides encoding/decoding of
82+ * _Unicode Strings_: `Streamly.Data.String` module provides encoding/decoding of
83 character streams and other character stream operations.
83 character streams and other character stream operations.
84 * _Arrays_: `Streamly.Memory.Array` module provides arrays for efficient
84 * _Arrays_: `Streamly.Memory.Array` module provides arrays for efficient
85 in-memory buffering and efficient interfacing with IO.
85 in-memory buffering and efficient interfacing with IO.
benchmark/FileIO.hs+4−4
@@ -20,7 +20,7 @@ import qualified Streamly.Memory.Array as A
20import qualified Streamly.Memory.ArrayStream as AS
20import qualified Streamly.Memory.ArrayStream as AS
21import qualified Streamly.Prelude as S
21import qualified Streamly.Prelude as S
22import qualified Streamly.Fold as FL
22import qualified Streamly.Fold as FL
23−import qualified Streamly.String as SS
23+import qualified Streamly.Data.String as SS
24import qualified Streamly.Internal as Internal
24import qualified Streamly.Internal as Internal
25
25
26#ifdef DEVBUILD
26#ifdef DEVBUILD
@@ -99,8 +99,8 @@ main = do
99 Handles inh _ <- readIORef href
99 Handles inh _ <- readIORef href
100 let s = FH.readArrays inh
100 let s = FH.readArrays inh
101 larr <- S.last s
101 larr <- S.last s
102− case larr of
102+ return $ case larr of
103− Nothing -> return Nothing
103+ Nothing -> Nothing
104 Just arr -> A.readIndex arr (A.length arr - 1)
104 Just arr -> A.readIndex arr (A.length arr - 1)
105 -- Note: this cannot be fairly compared with GNU wc -c or wc -m as
105 -- Note: this cannot be fairly compared with GNU wc -c or wc -m as
106 -- wc uses lseek to just determine the file size rather than reading
106 -- wc uses lseek to just determine the file size rather than reading
@@ -131,7 +131,7 @@ main = do
131 , mkBench "linecount" href $ do
131 , mkBench "linecount" href $ do
132 Handles inh _ <- readIORef href
132 Handles inh _ <- readIORef href
133 S.length
133 S.length
134− $ flip SS.foldLines FL.drain
134+ $ SS.foldLines FL.drain
135 $ SS.decodeChar8
135 $ SS.decodeChar8
136 $ FH.read inh
136 $ FH.read inh
137 , mkBench "sum" href $ do
137 , mkBench "sum" href $ do
examples/FileSinkServer.hs+1−1
@@ -8,7 +8,7 @@
8import System.Environment (getArgs)
8import System.Environment (getArgs)
9
9
10import Streamly
10import Streamly
11−import Streamly.String
11+import Streamly.Data.String
12import qualified Streamly.FileSystem.Handle as FH
12import qualified Streamly.FileSystem.Handle as FH
13import qualified Streamly.Memory.Array as A
13import qualified Streamly.Memory.Array as A
14import qualified Streamly.Network.Socket as NS
14import qualified Streamly.Network.Socket as NS
src/Streamly/String.hs → src/Streamly/Data/String.hsrenamed+8−8
@@ -1,6 +1,6 @@
1{-# LANGUAGE FlexibleContexts #-}
1{-# LANGUAGE FlexibleContexts #-}
2-- |
2-- |
3−-- Module : Streamly.String
3+-- Module : Streamly.Data.String
4-- Copyright : (c) 2018 Composewell Technologies
4-- Copyright : (c) 2018 Composewell Technologies
5--
5--
6-- License : BSD3
6-- License : BSD3
@@ -31,7 +31,7 @@
31-- TBD write a note on performance comparison with text, bytestrings, lists and
31-- TBD write a note on performance comparison with text, bytestrings, lists and
32-- vector.
32-- vector.
33--
33--
34−module Streamly.String
34+module Streamly.Data.String
35 (
35 (
36 -- String
36 -- String
37
37
@@ -167,20 +167,20 @@ stripStart = S.dropWhile isSpace
167-- | Fold each line of the stream using the supplied Fold
167-- | Fold each line of the stream using the supplied Fold
168-- and stream the result.
168-- and stream the result.
169--
169--
170−-- >>> S.toList $ foldLines (S.fromList "lines\nthis\nstring\n\n\n") FL.toList
170+-- >>> S.toList $ foldLines FL.toList (S.fromList "lines\nthis\nstring\n\n\n")
171-- ["lines", "this", "string", "", ""]
171-- ["lines", "this", "string", "", ""]
172{-# INLINE foldLines #-}
172{-# INLINE foldLines #-}
173−foldLines :: (Monad m, IsStream t) => t m Char -> Fold m Char b -> t m b
173+foldLines :: (Monad m, IsStream t) => Fold m Char b -> t m Char -> t m b
174−foldLines = flip (S.splitOnSuffix (== '\n'))
174+foldLines = S.splitOnSuffix (== '\n')
175
175
176-- | Fold each word of the stream using the supplied Fold
176-- | Fold each word of the stream using the supplied Fold
177-- and stream the result.
177-- and stream the result.
178--
178--
179−-- >>> S.toList $ foldWords (S.fromList "fold these words") FL.toList
179+-- >>> S.toList $ foldWords FL.toList (S.fromList "fold these words")
180-- ["fold", "these", "words"]
180-- ["fold", "these", "words"]
181{-# INLINE foldWords #-}
181{-# INLINE foldWords #-}
182−foldWords :: (Monad m, IsStream t) => t m Char -> Fold m Char b -> t m b
182+foldWords :: (Monad m, IsStream t) => Fold m Char b -> t m Char -> t m b
183−foldWords = flip (S.wordsBy isSpace)
183+foldWords = S.wordsBy isSpace
184
184
185foreign import ccall unsafe "u_iswspace"
185foreign import ccall unsafe "u_iswspace"
186 iswspace :: Int -> Int
186 iswspace :: Int -> Int
src/Streamly/Fold/Types.hs+4−1
@@ -362,7 +362,10 @@ lchunksOf n (Fold step1 initial1 extract1) (Fold step2 initial2 extract2) =
362 i1 <- initial1
362 i1 <- initial1
363 acc1 <- step1 i1 a
363 acc1 <- step1 i1 a
364 return $ Tuple3' 1 acc1 acc2
364 return $ Tuple3' 1 acc1 acc2
365− extract' (Tuple3' _ _ r) = extract2 r
365+ extract' (Tuple3' _ r1 r2) = do
366+ res <- extract1 r1
367+ acc2 <- step2 r2 res
368+ extract2 acc2
366
369
367-- | Group the input stream into windows of n second each and then fold each
370-- | Group the input stream into windows of n second each and then fold each
368-- group using the provided fold function.
371-- group using the provided fold function.
src/Streamly/Memory/Array.hs+6−6
@@ -240,7 +240,7 @@ _null :: Storable a => Array a -> Bool
240_null arr = length arr == 0
240_null arr = length arr == 0
241
241
242{-# INLINE _last #-}
242{-# INLINE _last #-}
243−_last :: (MonadIO m, Storable a) => Array a -> m (Maybe a)
243+_last :: Storable a => Array a -> Maybe a
244_last arr = readIndex arr (length arr - 1)
244_last arr = readIndex arr (length arr - 1)
245
245
246-------------------------------------------------------------------------------
246-------------------------------------------------------------------------------
@@ -343,16 +343,16 @@ foldbWith level f = undefined
343-- Random reads and writes
343-- Random reads and writes
344-------------------------------------------------------------------------------
344-------------------------------------------------------------------------------
345
345
346−-- | /O(1)/ Lookup the element at the given index.
346+-- | /O(1)/ Lookup the element at the given index, starting from 0.
347--
347--
348-- @since 0.7.0
348-- @since 0.7.0
349{-# INLINE readIndex #-}
349{-# INLINE readIndex #-}
350−readIndex :: (MonadIO m, Storable a) => Array a -> Int -> m (Maybe a)
350+readIndex :: Storable a => Array a -> Int -> Maybe a
351readIndex arr i =
351readIndex arr i =
352 if i < 0 || i > length arr - 1
352 if i < 0 || i > length arr - 1
353− then return Nothing
353+ then Nothing
354− else liftIO $ withForeignPtr (aStart arr) $ \p ->
354+ else A.unsafeInlineIO $
355− fmap Just $ peekElemOff p i
355+ withForeignPtr (aStart arr) $ \p -> fmap Just $ peekElemOff p i
356
356
357{-
357{-
358-- | @readSlice arr i count@ streams a slice of the array @arr@ starting
358-- | @readSlice arr i count@ streams a slice of the array @arr@ starting
streamly.cabal+1−1
@@ -230,7 +230,7 @@ library
230 , Streamly
230 , Streamly
231 , Streamly.Fold
231 , Streamly.Fold
232
232
233− , Streamly.String
233+ , Streamly.Data.String
234
234
235 -- IO devices
235 -- IO devices
236 , Streamly.Memory.Array
236 , Streamly.Memory.Array
test/String.hs+6−6
@@ -15,7 +15,7 @@ import Test.Hspec as H
15import qualified Streamly.Memory.Array as A
15import qualified Streamly.Memory.Array as A
16import qualified Streamly.Memory.ArrayStream as AS
16import qualified Streamly.Memory.ArrayStream as AS
17import qualified Streamly.Prelude as S
17import qualified Streamly.Prelude as S
18−import qualified Streamly.String as SS
18+import qualified Streamly.Data.String as SS
19
19
20-- Coverage build takes too long with default number of tests
20-- Coverage build takes too long with default number of tests
21{-
21{-
@@ -101,12 +101,12 @@ main = hspec
101 $ do
101 $ do
102 describe "UTF8 - Encoding / Decoding" $ do
102 describe "UTF8 - Encoding / Decoding" $ do
103 prop "decodeUtf8 . encodeUtf8 == id" $ propDecodeEncodeId
103 prop "decodeUtf8 . encodeUtf8 == id" $ propDecodeEncodeId
104− prop "Streamly.String.lines == Prelude.lines" $ testLines
104+ prop "Streamly.Data.String.lines == Prelude.lines" $ testLines
105− prop "Arrays Streamly.String.lines == Prelude.lines" $ testLinesArray
105+ prop "Arrays Streamly.Data.String.lines == Prelude.lines" $ testLinesArray
106− prop "Streamly.String.words == Prelude.words" $ testWords
106+ prop "Streamly.Data.String.words == Prelude.words" $ testWords
107 prop
107 prop
108− "Streamly.String.unlines . Streamly.String.lines == unlines . lines"
108+ "Streamly.Data.String.unlines . Streamly.Data.String.lines == unlines . lines"
109 $ testUnlines
109 $ testUnlines
110 prop
110 prop
111− "Streamly.String.unwords . Streamly.String.words == unwords . words"
111+ "Streamly.Data.String.unwords . Streamly.Data.String.words == unwords . words"
112 $ testUnwords
112 $ testUnwords