on master help sign in
rename some unfold combinators
a0df530Harendra Kumar
src/Streamly/Benchmark/FileIO/Array.hs+2−2
@@ -136,7 +136,7 @@ inspect $ 'cat `hasNoType` ''Step
136catBracket :: Handle -> Handle -> IO ()
136catBracket :: Handle -> Handle -> IO ()
137catBracket devNull inh =
137catBracket devNull inh =
138 let readEx = IUF.bracket return (\_ -> hClose inh)
138 let readEx = IUF.bracket return (\_ -> hClose inh)
139− (IUF.first FH.readArraysOf (256*1024))
139+ (IUF.supplyFirst FH.readArraysOf (256*1024))
140 in IUF.fold readEx (IFH.writeArrays devNull) inh
140 in IUF.fold readEx (IFH.writeArrays devNull) inh
141
141
142#ifdef INSPECTION
142#ifdef INSPECTION
@@ -162,7 +162,7 @@ inspect $ hasNoTypeClasses 'catBracketStream
162catOnException :: Handle -> Handle -> IO ()
162catOnException :: Handle -> Handle -> IO ()
163catOnException devNull inh =
163catOnException devNull inh =
164 let readEx = IUF.onException (\_ -> hClose inh)
164 let readEx = IUF.onException (\_ -> hClose inh)
165− (IUF.first FH.readArraysOf (256*1024))
165+ (IUF.supplyFirst FH.readArraysOf (256*1024))
166 in IUF.fold readEx (IFH.writeArrays devNull) inh
166 in IUF.fold readEx (IFH.writeArrays devNull) inh
167
167
168#ifdef INSPECTION
168#ifdef INSPECTION
src/Streamly/Internal/Data/Unfold.hs+34−16
@@ -61,9 +61,9 @@ module Streamly.Internal.Data.Unfold
61
61
62 -- * Operations on Input
62 -- * Operations on Input
63 , lmap
63 , lmap
64− , close
64+ , supply
65− , first
65+ , supplyFirst
66− , second
66+ , supplySecond
67 , discardFirst
67 , discardFirst
68 , discardSecond
68 , discardSecond
69 , swap
69 , swap
@@ -138,6 +138,9 @@ import qualified Streamly.Streams.StreamD as D
138-- Input operations
138-- Input operations
139-------------------------------------------------------------------------------
139-------------------------------------------------------------------------------
140
140
141+-- | Map a function on the input argument of the 'Unfold'.
142+--
143+-- /Internal/
141{-# INLINE_NORMAL lmap #-}
144{-# INLINE_NORMAL lmap #-}
142lmap :: (a -> c) -> Unfold m c b -> Unfold m a b
145lmap :: (a -> c) -> Unfold m c b -> Unfold m a b
143lmap f (Unfold ustep uinject) = Unfold ustep (uinject . f)
146lmap f (Unfold ustep uinject) = Unfold ustep (uinject . f)
@@ -146,21 +149,19 @@ lmap f (Unfold ustep uinject) = Unfold ustep (uinject . f)
146--
149--
147-- /Internal/
150-- /Internal/
148--
151--
149−{-# INLINE_NORMAL close #-}
152+{-# INLINE_NORMAL supply #-}
150−close :: Unfold m a b -> a -> Unfold m Void b
153+supply :: Unfold m a b -> a -> Unfold m Void b
151−close unf a = lmap (const a) unf
154+supply unf a = lmap (const a) unf
152
155
153−-- XXX rename to closeFst/closeSnd
154−--
155-- | Supply the first component of the tuple to an unfold that accepts a tuple
156-- | Supply the first component of the tuple to an unfold that accepts a tuple
156-- as a seed resulting in a fold that accepts the second component of the tuple
157-- as a seed resulting in a fold that accepts the second component of the tuple
157-- as a seed.
158-- as a seed.
158--
159--
159-- /Internal/
160-- /Internal/
160--
161--
161−{-# INLINE_NORMAL first #-}
162+{-# INLINE_NORMAL supplyFirst #-}
162−first :: Unfold m (a, b) c -> a -> Unfold m b c
163+supplyFirst :: Unfold m (a, b) c -> a -> Unfold m b c
163−first unf a = lmap (a, ) unf
164+supplyFirst unf a = lmap (a, ) unf
164
165
165-- | Supply the second component of the tuple to an unfold that accepts a tuple
166-- | Supply the second component of the tuple to an unfold that accepts a tuple
166-- as a seed resulting in a fold that accepts the first component of the tuple
167-- 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
168--
169--
169-- /Internal/
170-- /Internal/
170--
171--
171−{-# INLINE_NORMAL second #-}
172+{-# INLINE_NORMAL supplySecond #-}
172−second :: Unfold m (a, b) c -> b -> Unfold m a c
173+supplySecond :: Unfold m (a, b) c -> b -> Unfold m a c
173−second unf b = lmap (, b) unf
174+supplySecond unf b = lmap (, b) unf
174
175
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+--
175{-# INLINE_NORMAL discardFirst #-}
182{-# INLINE_NORMAL discardFirst #-}
176discardFirst :: Unfold m a b -> Unfold m (c, a) b
183discardFirst :: Unfold m a b -> Unfold m (c, a) b
177discardFirst = lmap snd
184discardFirst = lmap snd
178
185
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+--
179{-# INLINE_NORMAL discardSecond #-}
192{-# INLINE_NORMAL discardSecond #-}
180discardSecond :: Unfold m a b -> Unfold m (a, c) b
193discardSecond :: Unfold m a b -> Unfold m (a, c) b
181discardSecond = lmap fst
194discardSecond = lmap fst
182
195
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+--
183{-# INLINE_NORMAL swap #-}
201{-# INLINE_NORMAL swap #-}
184swap :: Unfold m (a, c) b -> Unfold m (c, a) b
202swap :: Unfold m (a, c) b -> Unfold m (c, a) b
185swap = lmap Tuple.swap
203swap = lmap Tuple.swap
@@ -433,7 +451,7 @@ enumerateFromStepIntegral = Unfold step inject
433{-# INLINE enumerateFromToIntegral #-}
451{-# INLINE enumerateFromToIntegral #-}
434enumerateFromToIntegral :: (Monad m, Integral a) => a -> Unfold m a a
452enumerateFromToIntegral :: (Monad m, Integral a) => a -> Unfold m a a
435enumerateFromToIntegral to =
453enumerateFromToIntegral to =
436− takeWhile (<= to) $ second enumerateFromStepIntegral 1
454+ takeWhile (<= to) $ supplySecond enumerateFromStepIntegral 1
437
455
438{-# INLINE enumerateFromIntegral #-}
456{-# INLINE enumerateFromIntegral #-}
439enumerateFromIntegral :: (Monad m, Integral a, Bounded a) => Unfold m a a
457enumerateFromIntegral :: (Monad m, Integral a, Bounded a) => Unfold m a a
@@ -719,7 +737,7 @@ bracket bef aft (Unfold step1 inject1) = Unfold step inject
719 Stop -> aft v >> return Stop
737 Stop -> aft v >> return Stop
720
738
721-- | When unfolding if an exception occurs, unfold the exception using the
739-- | When unfolding if an exception occurs, unfold the exception using the
722−-- exception unfold.
740+-- exception unfold supplied as the first argument to 'handle'.
723--
741--
724-- /Internal/
742-- /Internal/
725{-# INLINE_NORMAL handle #-}
743{-# INLINE_NORMAL handle #-}
src/Streamly/Internal/FileSystem/Handle.hs+2−2
@@ -214,7 +214,7 @@ toStreamArrays = toStreamArraysOf defaultChunkSize
214-- @since 0.7.0
214-- @since 0.7.0
215{-# INLINE readArrays #-}
215{-# INLINE readArrays #-}
216readArrays :: MonadIO m => Unfold m Handle (Array Word8)
216readArrays :: MonadIO m => Unfold m Handle (Array Word8)
217−readArrays = UF.first readArraysOf defaultChunkSize
217+readArrays = UF.supplyFirst readArraysOf defaultChunkSize
218
218
219-------------------------------------------------------------------------------
219-------------------------------------------------------------------------------
220-- Read File to Stream
220-- Read File to Stream
@@ -250,7 +250,7 @@ toStreamInChunksOf chunkSize h = AS.concat $ toStreamArraysOf chunkSize h
250-- @since 0.7.0
250-- @since 0.7.0
251{-# INLINE read #-}
251{-# INLINE read #-}
252read :: MonadIO m => Unfold m Handle Word8
252read :: MonadIO m => Unfold m Handle Word8
253−read = UF.first readInChunksOf defaultChunkSize
253+read = UF.supplyFirst readInChunksOf defaultChunkSize
254
254
255-- | Generate a byte stream from a file 'Handle'.
255-- | Generate a byte stream from a file 'Handle'.
256--
256--
src/Streamly/Internal/Network/Server.hs+4−4
@@ -153,23 +153,23 @@ listenOnAddr = UF.map fst listenTuplesOnAddr
153-- IPv4 addresses/interfaces of the machine and listens for TCP connections on
153-- IPv4 addresses/interfaces of the machine and listens for TCP connections on
154-- the specified port.
154-- the specified port.
155--
155--
156−-- > listenOnPort = UF.first listenOnAddr (0,0,0,0)
156+-- > listenOnPort = UF.supplyFirst listenOnAddr (0,0,0,0)
157--
157--
158-- @since 0.7.0
158-- @since 0.7.0
159{-# INLINE listenOnPort #-}
159{-# INLINE listenOnPort #-}
160listenOnPort :: MonadIO m => Unfold m PortNumber Socket
160listenOnPort :: MonadIO m => Unfold m PortNumber Socket
161−listenOnPort = UF.first listenOnAddr (0,0,0,0)
161+listenOnPort = UF.supplyFirst listenOnAddr (0,0,0,0)
162
162
163-- | Like 'listenOnAddr' but binds on the localhost IPv4 address @127.0.0.1@.
163-- | Like 'listenOnAddr' but binds on the localhost IPv4 address @127.0.0.1@.
164-- The server can only be accessed from the local host, it cannot be accessed
164-- The server can only be accessed from the local host, it cannot be accessed
165-- from other hosts on the network.
165-- from other hosts on the network.
166--
166--
167−-- > listenOnPortLocal = UF.first listenOnAddr (127,0,0,1)
167+-- > listenOnPortLocal = UF.supplyFirst listenOnAddr (127,0,0,1)
168--
168--
169-- @since 0.7.0
169-- @since 0.7.0
170{-# INLINE listenOnPortLocal #-}
170{-# INLINE listenOnPortLocal #-}
171listenOnPortLocal :: MonadIO m => Unfold m PortNumber Socket
171listenOnPortLocal :: MonadIO m => Unfold m PortNumber Socket
172−listenOnPortLocal = UF.first listenOnAddr (127,0,0,1)
172+listenOnPortLocal = UF.supplyFirst listenOnAddr (127,0,0,1)
173
173
174-------------------------------------------------------------------------------
174-------------------------------------------------------------------------------
175-- Listen
175-- Listen
src/Streamly/Internal/Network/Socket.hs+2−2
@@ -245,7 +245,7 @@ readArraysOf = Unfold step return
245-- @since 0.7.0
245-- @since 0.7.0
246{-# INLINE readArrays #-}
246{-# INLINE readArrays #-}
247readArrays :: MonadIO m => Unfold m Socket (Array Word8)
247readArrays :: MonadIO m => Unfold m Socket (Array Word8)
248−readArrays = UF.first readArraysOf A.defaultChunkSize
248+readArrays = UF.supplyFirst readArraysOf A.defaultChunkSize
249
249
250-------------------------------------------------------------------------------
250-------------------------------------------------------------------------------
251-- Read File to Stream
251-- Read File to Stream
@@ -290,7 +290,7 @@ readInChunksOf = UF.concat readArraysOf A.read
290-- @since 0.7.0
290-- @since 0.7.0
291{-# INLINE read #-}
291{-# INLINE read #-}
292read :: MonadIO m => Unfold m Socket Word8
292read :: MonadIO m => Unfold m Socket Word8
293−read = UF.first readInChunksOf A.defaultChunkSize
293+read = UF.supplyFirst readInChunksOf A.defaultChunkSize
294
294
295-------------------------------------------------------------------------------
295-------------------------------------------------------------------------------
296-- Writing
296-- Writing