on master help sign in
Merge pull request #257 from composewell/misc-refactorings

Some minor refactorings

commit6c4abc8e204831a922e555f34123252e1fa7a56d authorHarendra Kumar <harendra.kumar@gmail.com> dateWed 14 Aug 2019, 04:12 (7 years ago) parent5d99c94f6c91cb
8 files changed+35−32
Changelog.md+5−5 benchmark/FileIO.hs+4−4 examples/FileSinkServer.hs+1−1 src/Streamly/String.hs → src/Streamly/Data/String.hs+8−8 src/Streamly/Fold/Types.hs+4−1 src/Streamly/Memory/Array.hs+6−6 streamly.cabal+1−1 test/String.hs+6−6
Changelog.md+5−5
@@ -22,12 +22,12 @@
2222 * `runN` has been replaced by `drainN`
2323 * `runWhile` has been replaced by `drainWhile`
2424 * `fromHandle` has been deprecated. Please use
25− `Streamly.FileSystem.Handle.read`, `Streamly.String.decodeUtf8` and
26− `Streamly.String.lines` to split the stream into lines. Use
27− `Streamly.String.foldLine` with `Streamly.Fold.toList` if you want to
25+ `Streamly.FileSystem.Handle.read`, `Streamly.Data.String.decodeUtf8` and
26+ `Streamly.Data.String.lines` to split the stream into lines. Use
27+ `Streamly.Data.String.foldLine` with `Streamly.Fold.toList` if you want to
2828 convert the stream to a stream of `String`.
2929 * `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
3131 `Streamly.FileSystem.Handle.write` for writing to a file handle.
3232 * Deprecate `scanx`, `foldx`, `foldxM`, `foldr1`
3333 * Remove deprecated APIs `scan`, `foldl`, `foldlM`
@@ -79,7 +79,7 @@ example, concat streams concurrently using this.
7979### Other Enhancements
8080
8181* 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
8383 character streams and other character stream operations.
8484 * _Arrays_: `Streamly.Memory.Array` module provides arrays for efficient
8585 in-memory buffering and efficient interfacing with IO.
benchmark/FileIO.hs+4−4
@@ -20,7 +20,7 @@ import qualified Streamly.Memory.Array as A
2020import qualified Streamly.Memory.ArrayStream as AS
2121import qualified Streamly.Prelude as S
2222import qualified Streamly.Fold as FL
23−import qualified Streamly.String as SS
23+import qualified Streamly.Data.String as SS
2424import qualified Streamly.Internal as Internal
2525
2626#ifdef DEVBUILD
@@ -99,8 +99,8 @@ main = do
9999 Handles inh _ <- readIORef href
100100 let s = FH.readArrays inh
101101 larr <- S.last s
102− case larr of
103− Nothing -> return Nothing
102+ return $ case larr of
103+ Nothing -> Nothing
104104 Just arr -> A.readIndex arr (A.length arr - 1)
105105 -- Note: this cannot be fairly compared with GNU wc -c or wc -m as
106106 -- wc uses lseek to just determine the file size rather than reading
@@ -131,7 +131,7 @@ main = do
131131 , mkBench "linecount" href $ do
132132 Handles inh _ <- readIORef href
133133 S.length
134− $ flip SS.foldLines FL.drain
134+ $ SS.foldLines FL.drain
135135 $ SS.decodeChar8
136136 $ FH.read inh
137137 , mkBench "sum" href $ do
examples/FileSinkServer.hs+1−1
@@ -8,7 +8,7 @@
88import System.Environment (getArgs)
99
1010import Streamly
11−import Streamly.String
11+import Streamly.Data.String
1212import qualified Streamly.FileSystem.Handle as FH
1313import qualified Streamly.Memory.Array as A
1414import qualified Streamly.Network.Socket as NS
src/Streamly/String.hs → src/Streamly/Data/String.hsrenamed+8−8
@@ -1,6 +1,6 @@
11{-# LANGUAGE FlexibleContexts #-}
22-- |
3−-- Module : Streamly.String
3+-- Module : Streamly.Data.String
44-- Copyright : (c) 2018 Composewell Technologies
55--
66-- License : BSD3
@@ -31,7 +31,7 @@
3131-- TBD write a note on performance comparison with text, bytestrings, lists and
3232-- vector.
3333--
34−module Streamly.String
34+module Streamly.Data.String
3535 (
3636 -- String
3737
@@ -167,20 +167,20 @@ stripStart = S.dropWhile isSpace
167167-- | Fold each line of the stream using the supplied Fold
168168-- and stream the result.
169169--
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")
171171-- ["lines", "this", "string", "", ""]
172172{-# INLINE foldLines #-}
173−foldLines :: (Monad m, IsStream t) => t m Char -> Fold m Char b -> t m b
174−foldLines = flip (S.splitOnSuffix (== '\n'))
173+foldLines :: (Monad m, IsStream t) => Fold m Char b -> t m Char -> t m b
174+foldLines = S.splitOnSuffix (== '\n')
175175
176176-- | Fold each word of the stream using the supplied Fold
177177-- and stream the result.
178178--
179−-- >>> S.toList $ foldWords (S.fromList "fold these words") FL.toList
179+-- >>> S.toList $ foldWords FL.toList (S.fromList "fold these words")
180180-- ["fold", "these", "words"]
181181{-# INLINE foldWords #-}
182−foldWords :: (Monad m, IsStream t) => t m Char -> Fold m Char b -> t m b
183−foldWords = flip (S.wordsBy isSpace)
182+foldWords :: (Monad m, IsStream t) => Fold m Char b -> t m Char -> t m b
183+foldWords = S.wordsBy isSpace
184184
185185foreign import ccall unsafe "u_iswspace"
186186 iswspace :: Int -> Int
src/Streamly/Fold/Types.hs+4−1
@@ -362,7 +362,10 @@ lchunksOf n (Fold step1 initial1 extract1) (Fold step2 initial2 extract2) =
362362 i1 <- initial1
363363 acc1 <- step1 i1 a
364364 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
366369
367370-- | Group the input stream into windows of n second each and then fold each
368371-- group using the provided fold function.
src/Streamly/Memory/Array.hs+6−6
@@ -240,7 +240,7 @@ _null :: Storable a => Array a -> Bool
240240_null arr = length arr == 0
241241
242242{-# INLINE _last #-}
243−_last :: (MonadIO m, Storable a) => Array a -> m (Maybe a)
243+_last :: Storable a => Array a -> Maybe a
244244_last arr = readIndex arr (length arr - 1)
245245
246246-------------------------------------------------------------------------------
@@ -343,16 +343,16 @@ foldbWith level f = undefined
343343-- Random reads and writes
344344-------------------------------------------------------------------------------
345345
346−-- | /O(1)/ Lookup the element at the given index.
346+-- | /O(1)/ Lookup the element at the given index, starting from 0.
347347--
348348-- @since 0.7.0
349349{-# INLINE readIndex #-}
350−readIndex :: (MonadIO m, Storable a) => Array a -> Int -> m (Maybe a)
350+readIndex :: Storable a => Array a -> Int -> Maybe a
351351readIndex arr i =
352352 if i < 0 || i > length arr - 1
353− then return Nothing
354− else liftIO $ withForeignPtr (aStart arr) $ \p ->
355− fmap Just $ peekElemOff p i
353+ then Nothing
354+ else A.unsafeInlineIO $
355+ withForeignPtr (aStart arr) $ \p -> fmap Just $ peekElemOff p i
356356
357357{-
358358-- | @readSlice arr i count@ streams a slice of the array @arr@ starting
streamly.cabal+1−1
@@ -230,7 +230,7 @@ library
230230 , Streamly
231231 , Streamly.Fold
232232
233− , Streamly.String
233+ , Streamly.Data.String
234234
235235 -- IO devices
236236 , Streamly.Memory.Array
test/String.hs+6−6
@@ -15,7 +15,7 @@ import Test.Hspec as H
1515import qualified Streamly.Memory.Array as A
1616import qualified Streamly.Memory.ArrayStream as AS
1717import qualified Streamly.Prelude as S
18−import qualified Streamly.String as SS
18+import qualified Streamly.Data.String as SS
1919
2020-- Coverage build takes too long with default number of tests
2121{-
@@ -101,12 +101,12 @@ main = hspec
101101 $ do
102102 describe "UTF8 - Encoding / Decoding" $ do
103103 prop "decodeUtf8 . encodeUtf8 == id" $ propDecodeEncodeId
104− prop "Streamly.String.lines == Prelude.lines" $ testLines
105− prop "Arrays Streamly.String.lines == Prelude.lines" $ testLinesArray
106− prop "Streamly.String.words == Prelude.words" $ testWords
104+ prop "Streamly.Data.String.lines == Prelude.lines" $ testLines
105+ prop "Arrays Streamly.Data.String.lines == Prelude.lines" $ testLinesArray
106+ prop "Streamly.Data.String.words == Prelude.words" $ testWords
107107 prop
108− "Streamly.String.unlines . Streamly.String.lines == unlines . lines"
108+ "Streamly.Data.String.unlines . Streamly.Data.String.lines == unlines . lines"
109109 $ testUnlines
110110 prop
111− "Streamly.String.unwords . Streamly.String.words == unwords . words"
111+ "Streamly.Data.String.unwords . Streamly.Data.String.words == unwords . words"
112112 $ testUnwords