on master help sign in
rename some unfold combinators
commita0df5303060a0918dbd01e4853cce018d022c0ab authorHarendra Kumar <harendra.kumar@gmail.com> dateFri 11 Oct 2019, 14:41 (7 years ago) parent9b4272e
5 files changed+44−26
src/Streamly/Benchmark/FileIO/Array.hs+2−2 src/Streamly/Internal/Data/Unfold.hs+34−16 src/Streamly/Internal/FileSystem/Handle.hs+2−2 src/Streamly/Internal/Network/Server.hs+4−4 src/Streamly/Internal/Network/Socket.hs+2−2
src/Streamly/Benchmark/FileIO/Array.hs+2−2
@@ -136,7 +136,7 @@ inspect $ 'cat `hasNoType` ''Step
136136catBracket :: Handle -> Handle -> IO ()
137137catBracket devNull inh =
138138 let readEx = IUF.bracket return (\_ -> hClose inh)
139− (IUF.first FH.readArraysOf (256*1024))
139+ (IUF.supplyFirst FH.readArraysOf (256*1024))
140140 in IUF.fold readEx (IFH.writeArrays devNull) inh
141141
142142#ifdef INSPECTION
@@ -162,7 +162,7 @@ inspect $ hasNoTypeClasses 'catBracketStream
162162catOnException :: Handle -> Handle -> IO ()
163163catOnException devNull inh =
164164 let readEx = IUF.onException (\_ -> hClose inh)
165− (IUF.first FH.readArraysOf (256*1024))
165+ (IUF.supplyFirst FH.readArraysOf (256*1024))
166166 in IUF.fold readEx (IFH.writeArrays devNull) inh
167167
168168#ifdef INSPECTION
src/Streamly/Internal/Data/Unfold.hs+34−16
@@ -61,9 +61,9 @@ module Streamly.Internal.Data.Unfold
6161
6262 -- * Operations on Input
6363 , lmap
64− , close
65− , first
66− , second
64+ , supply
65+ , supplyFirst
66+ , supplySecond
6767 , discardFirst
6868 , discardSecond
6969 , swap
@@ -138,6 +138,9 @@ import qualified Streamly.Streams.StreamD as D
138138-- Input operations
139139-------------------------------------------------------------------------------
140140
141+-- | Map a function on the input argument of the 'Unfold'.
142+--
143+-- /Internal/
141144{-# INLINE_NORMAL lmap #-}
142145lmap :: (a -> c) -> Unfold m c b -> Unfold m a b
143146lmap f (Unfold ustep uinject) = Unfold ustep (uinject . f)
@@ -146,21 +149,19 @@ lmap f (Unfold ustep uinject) = Unfold ustep (uinject . f)
146149--
147150-- /Internal/
148151--
149−{-# INLINE_NORMAL close #-}
150−close :: Unfold m a b -> a -> Unfold m Void b
151−close unf a = lmap (const a) unf
152+{-# INLINE_NORMAL supply #-}
153+supply :: Unfold m a b -> a -> Unfold m Void b
154+supply unf a = lmap (const a) unf
152155
153−-- XXX rename to closeFst/closeSnd
154−--
155156-- | Supply the first component of the tuple to an unfold that accepts a tuple
156157-- as a seed resulting in a fold that accepts the second component of the tuple
157158-- as a seed.
158159--
159160-- /Internal/
160161--
161−{-# INLINE_NORMAL first #-}
162−first :: Unfold m (a, b) c -> a -> Unfold m b c
163−first unf a = lmap (a, ) unf
162+{-# INLINE_NORMAL supplyFirst #-}
163+supplyFirst :: Unfold m (a, b) c -> a -> Unfold m b c
164+supplyFirst unf a = lmap (a, ) unf
164165
165166-- | Supply the second component of the tuple to an unfold that accepts a tuple
166167-- as a seed resulting in a fold that accepts the first component of the tuple
@@ -168,18 +169,35 @@ first unf a = lmap (a, ) unf
168169--
169170-- /Internal/
170171--
171−{-# INLINE_NORMAL second #-}
172−second :: Unfold m (a, b) c -> b -> Unfold m a c
173−second unf b = lmap (, b) unf
172+{-# INLINE_NORMAL supplySecond #-}
173+supplySecond :: Unfold m (a, b) c -> b -> Unfold m a c
174+supplySecond unf b = lmap (, b) unf
174175
176+-- | Convert an 'Unfold' into an unfold accepting a tuple as an argument,
177+-- using the argument of the original fold as the second element of tuple and
178+-- discarding the first element of the tuple.
179+--
180+-- /Internal/
181+--
175182{-# INLINE_NORMAL discardFirst #-}
176183discardFirst :: Unfold m a b -> Unfold m (c, a) b
177184discardFirst = lmap snd
178185
186+-- | Convert an 'Unfold' into an unfold accepting a tuple as an argument,
187+-- using the argument of the original fold as the first element of tuple and
188+-- discarding the second element of the tuple.
189+--
190+-- /Internal/
191+--
179192{-# INLINE_NORMAL discardSecond #-}
180193discardSecond :: Unfold m a b -> Unfold m (a, c) b
181194discardSecond = lmap fst
182195
196+-- | Convert an 'Unfold' that accepts a tuple as an argument into an unfold
197+-- that accepts a tuple with elements swapped.
198+--
199+-- /Internal/
200+--
183201{-# INLINE_NORMAL swap #-}
184202swap :: Unfold m (a, c) b -> Unfold m (c, a) b
185203swap = lmap Tuple.swap
@@ -433,7 +451,7 @@ enumerateFromStepIntegral = Unfold step inject
433451{-# INLINE enumerateFromToIntegral #-}
434452enumerateFromToIntegral :: (Monad m, Integral a) => a -> Unfold m a a
435453enumerateFromToIntegral to =
436− takeWhile (<= to) $ second enumerateFromStepIntegral 1
454+ takeWhile (<= to) $ supplySecond enumerateFromStepIntegral 1
437455
438456{-# INLINE enumerateFromIntegral #-}
439457enumerateFromIntegral :: (Monad m, Integral a, Bounded a) => Unfold m a a
@@ -719,7 +737,7 @@ bracket bef aft (Unfold step1 inject1) = Unfold step inject
719737 Stop -> aft v >> return Stop
720738
721739-- | When unfolding if an exception occurs, unfold the exception using the
722−-- exception unfold.
740+-- exception unfold supplied as the first argument to 'handle'.
723741--
724742-- /Internal/
725743{-# INLINE_NORMAL handle #-}
src/Streamly/Internal/FileSystem/Handle.hs+2−2
@@ -214,7 +214,7 @@ toStreamArrays = toStreamArraysOf defaultChunkSize
214214-- @since 0.7.0
215215{-# INLINE readArrays #-}
216216readArrays :: MonadIO m => Unfold m Handle (Array Word8)
217−readArrays = UF.first readArraysOf defaultChunkSize
217+readArrays = UF.supplyFirst readArraysOf defaultChunkSize
218218
219219-------------------------------------------------------------------------------
220220-- Read File to Stream
@@ -250,7 +250,7 @@ toStreamInChunksOf chunkSize h = AS.concat $ toStreamArraysOf chunkSize h
250250-- @since 0.7.0
251251{-# INLINE read #-}
252252read :: MonadIO m => Unfold m Handle Word8
253−read = UF.first readInChunksOf defaultChunkSize
253+read = UF.supplyFirst readInChunksOf defaultChunkSize
254254
255255-- | Generate a byte stream from a file 'Handle'.
256256--
src/Streamly/Internal/Network/Server.hs+4−4
@@ -153,23 +153,23 @@ listenOnAddr = UF.map fst listenTuplesOnAddr
153153-- IPv4 addresses/interfaces of the machine and listens for TCP connections on
154154-- the specified port.
155155--
156−-- > listenOnPort = UF.first listenOnAddr (0,0,0,0)
156+-- > listenOnPort = UF.supplyFirst listenOnAddr (0,0,0,0)
157157--
158158-- @since 0.7.0
159159{-# INLINE listenOnPort #-}
160160listenOnPort :: MonadIO m => Unfold m PortNumber Socket
161−listenOnPort = UF.first listenOnAddr (0,0,0,0)
161+listenOnPort = UF.supplyFirst listenOnAddr (0,0,0,0)
162162
163163-- | Like 'listenOnAddr' but binds on the localhost IPv4 address @127.0.0.1@.
164164-- The server can only be accessed from the local host, it cannot be accessed
165165-- from other hosts on the network.
166166--
167−-- > listenOnPortLocal = UF.first listenOnAddr (127,0,0,1)
167+-- > listenOnPortLocal = UF.supplyFirst listenOnAddr (127,0,0,1)
168168--
169169-- @since 0.7.0
170170{-# INLINE listenOnPortLocal #-}
171171listenOnPortLocal :: MonadIO m => Unfold m PortNumber Socket
172−listenOnPortLocal = UF.first listenOnAddr (127,0,0,1)
172+listenOnPortLocal = UF.supplyFirst listenOnAddr (127,0,0,1)
173173
174174-------------------------------------------------------------------------------
175175-- Listen
src/Streamly/Internal/Network/Socket.hs+2−2
@@ -245,7 +245,7 @@ readArraysOf = Unfold step return
245245-- @since 0.7.0
246246{-# INLINE readArrays #-}
247247readArrays :: MonadIO m => Unfold m Socket (Array Word8)
248−readArrays = UF.first readArraysOf A.defaultChunkSize
248+readArrays = UF.supplyFirst readArraysOf A.defaultChunkSize
249249
250250-------------------------------------------------------------------------------
251251-- Read File to Stream
@@ -290,7 +290,7 @@ readInChunksOf = UF.concat readArraysOf A.read
290290-- @since 0.7.0
291291{-# INLINE read #-}
292292read :: MonadIO m => Unfold m Socket Word8
293−read = UF.first readInChunksOf A.defaultChunkSize
293+read = UF.supplyFirst readInChunksOf A.defaultChunkSize
294294
295295-------------------------------------------------------------------------------
296296-- Writing