mirror of
https://github.com/ilyakooo0/streamly.git
synced 2024-09-11 08:25:40 +03:00
Rename some unfold APIs
This commit is contained in:
parent
4f70b0d4e6
commit
d31f3329f1
@ -56,7 +56,7 @@ benchIO name f = bench name $ nfIO $ randomRIO (1,1) >>= f
|
||||
-- generate numbers up to the argument value
|
||||
{-# INLINE source #-}
|
||||
source :: Monad m => Int -> Unfold m Int Int
|
||||
source n = UF.supplySecond n UF.enumerateFromToIntegral
|
||||
source n = UF.second n UF.enumerateFromToIntegral
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
-- Benchmark helpers
|
||||
@ -75,7 +75,7 @@ drainTransformation unf f seed = drainGeneration (f unf) seed
|
||||
drainTransformationDefault ::
|
||||
Monad m => Int -> (Unfold m Int Int -> Unfold m c d) -> c -> m ()
|
||||
drainTransformationDefault to =
|
||||
drainTransformation (UF.supplySecond to UF.enumerateFromToIntegral)
|
||||
drainTransformation (UF.second to UF.enumerateFromToIntegral)
|
||||
|
||||
{-# INLINE drainProduct #-}
|
||||
drainProduct ::
|
||||
@ -98,7 +98,7 @@ drainProductDefault to = drainProduct src src
|
||||
|
||||
where
|
||||
|
||||
src = UF.supplySecond to UF.enumerateFromToIntegral
|
||||
src = UF.second to UF.enumerateFromToIntegral
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
-- Operations on input
|
||||
@ -114,26 +114,26 @@ lmapM :: Monad m => Int -> Int -> m ()
|
||||
lmapM size start =
|
||||
drainTransformationDefault (size + start) (UF.lmapM (return . (+) 1)) start
|
||||
|
||||
{-# INLINE supply #-}
|
||||
supply :: Monad m => Int -> Int -> m ()
|
||||
supply size start =
|
||||
drainTransformationDefault (size + start) (UF.supply start) undefined
|
||||
{-# INLINE both #-}
|
||||
both :: Monad m => Int -> Int -> m ()
|
||||
both size start =
|
||||
drainTransformationDefault (size + start) (UF.both start) undefined
|
||||
|
||||
|
||||
{-# INLINE supplyFirst #-}
|
||||
supplyFirst :: Monad m => Int -> Int -> m ()
|
||||
supplyFirst size start =
|
||||
{-# INLINE first #-}
|
||||
first :: Monad m => Int -> Int -> m ()
|
||||
first size start =
|
||||
drainTransformation
|
||||
(UF.take size UF.enumerateFromThenIntegral)
|
||||
(UF.supplyFirst start)
|
||||
(UF.first start)
|
||||
1
|
||||
|
||||
{-# INLINE supplySecond #-}
|
||||
supplySecond :: Monad m => Int -> Int -> m ()
|
||||
supplySecond size start =
|
||||
{-# INLINE second #-}
|
||||
second :: Monad m => Int -> Int -> m ()
|
||||
second size start =
|
||||
drainTransformation
|
||||
(UF.take size UF.enumerateFromThenIntegral)
|
||||
(UF.supplySecond 1)
|
||||
(UF.second 1)
|
||||
start
|
||||
|
||||
{-# INLINE discardFirst #-}
|
||||
@ -256,7 +256,7 @@ enumerateFromThenIntegral size start =
|
||||
enumerateFromToIntegral :: Monad m => Int -> Int -> m ()
|
||||
enumerateFromToIntegral size start =
|
||||
drainGeneration
|
||||
( UF.supplySecond
|
||||
( UF.second
|
||||
(size + start)
|
||||
UF.enumerateFromToIntegral
|
||||
) start
|
||||
@ -280,7 +280,7 @@ enumerateFromToFractional :: Monad m => Int -> Int -> m ()
|
||||
enumerateFromToFractional size start =
|
||||
let intToDouble x = (fromInteger (fromIntegral x)) :: Double
|
||||
in drainGeneration
|
||||
( UF.supplySecond
|
||||
( UF.second
|
||||
(intToDouble $ start + size)
|
||||
UF.enumerateFromToFractional
|
||||
)
|
||||
@ -448,8 +448,8 @@ concatMapM value start =
|
||||
where
|
||||
|
||||
val = nthRoot 2 value
|
||||
unfoldInGen i = return (UF.supplySecond (i + val) UF.enumerateFromToIntegral)
|
||||
unfoldOut = UF.supplySecond (start + val) UF.enumerateFromToIntegral
|
||||
unfoldInGen i = return (UF.second (i + val) UF.enumerateFromToIntegral)
|
||||
unfoldOut = UF.second (start + val) UF.enumerateFromToIntegral
|
||||
|
||||
{-# INLINE toNull #-}
|
||||
toNull :: Monad m => Int -> Int -> m ()
|
||||
@ -619,9 +619,9 @@ o_1_space_transformation_input size =
|
||||
"transformation/input"
|
||||
[ benchIO "lmap" $ lmap size
|
||||
, benchIO "lmapM" $ lmapM size
|
||||
, benchIO "supply" $ supply size
|
||||
, benchIO "supplyFirst" $ supplyFirst size
|
||||
, benchIO "supplySecond" $ supplySecond size
|
||||
, benchIO "both" $ both size
|
||||
, benchIO "first" $ first size
|
||||
, benchIO "second" $ second size
|
||||
, benchIO "discardFirst" $ discardFirst size
|
||||
, benchIO "discardSecond" $ discardSecond size
|
||||
, benchIO "swap" $ swap size
|
||||
|
@ -185,9 +185,9 @@ module Streamly.Internal.Data.Unfold
|
||||
-- ** Mapping on Input
|
||||
, lmap
|
||||
, lmapM
|
||||
, supply
|
||||
, supplyFirst
|
||||
, supplySecond
|
||||
, both
|
||||
, first
|
||||
, second
|
||||
, discardFirst
|
||||
, discardSecond
|
||||
, swap
|
||||
|
@ -226,7 +226,7 @@ enumerateFromThenToIntegral =
|
||||
{-# INLINE enumerateFromIntegralBounded #-}
|
||||
enumerateFromIntegralBounded :: (Monad m, Integral a, Bounded a) =>
|
||||
Unfold m a a
|
||||
enumerateFromIntegralBounded = supplySecond maxBound enumerateFromToIntegral
|
||||
enumerateFromIntegralBounded = second maxBound enumerateFromToIntegral
|
||||
|
||||
{-# INLINE enumerateFromThenIntegralBounded #-}
|
||||
enumerateFromThenIntegralBounded :: (Monad m, Integral a, Bounded a ) =>
|
||||
@ -351,7 +351,7 @@ enumerateFromThenToSmall =
|
||||
--
|
||||
{-# INLINE enumerateFromSmallBounded #-}
|
||||
enumerateFromSmallBounded :: (Monad m, Enum a, Bounded a) => Unfold m a a
|
||||
enumerateFromSmallBounded = supplySecond maxBound enumerateFromToSmall
|
||||
enumerateFromSmallBounded = second maxBound enumerateFromToSmall
|
||||
|
||||
-- | Enumerate from given starting Enum value 'from' and next Enum value 'next'
|
||||
-- with stride of (fromEnum next - fromEnum from) till maxBound.
|
||||
|
@ -32,9 +32,9 @@ module Streamly.Internal.Data.Unfold.Type
|
||||
, lmap
|
||||
, lmapM
|
||||
, map
|
||||
, supply
|
||||
, supplyFirst
|
||||
, supplySecond
|
||||
, both
|
||||
, first
|
||||
, second
|
||||
|
||||
-- * Trimming
|
||||
, takeWhileMWithInput
|
||||
@ -190,42 +190,42 @@ lmapM f (Unfold ustep uinject) = Unfold ustep (f >=> uinject)
|
||||
-- | Supply the seed to an unfold closing the input end of the unfold.
|
||||
--
|
||||
-- @
|
||||
-- supply a = Unfold.lmap (Prelude.const a)
|
||||
-- both a = Unfold.lmap (Prelude.const a)
|
||||
-- @
|
||||
--
|
||||
-- /Pre-release/
|
||||
--
|
||||
{-# INLINE_NORMAL supply #-}
|
||||
supply :: a -> Unfold m a b -> Unfold m Void b
|
||||
supply a = lmap (Prelude.const a)
|
||||
{-# INLINE_NORMAL both #-}
|
||||
both :: a -> Unfold m a b -> Unfold m Void b
|
||||
both a = lmap (Prelude.const a)
|
||||
|
||||
-- | Supply the first component of the tuple to an unfold that accepts a tuple
|
||||
-- as a seed resulting in a fold that accepts the second component of the tuple
|
||||
-- as a seed.
|
||||
--
|
||||
-- @
|
||||
-- supplyFirst a = Unfold.lmap (a, )
|
||||
-- first a = Unfold.lmap (a, )
|
||||
-- @
|
||||
--
|
||||
-- /Pre-release/
|
||||
--
|
||||
{-# INLINE_NORMAL supplyFirst #-}
|
||||
supplyFirst :: a -> Unfold m (a, b) c -> Unfold m b c
|
||||
supplyFirst a = lmap (a, )
|
||||
{-# INLINE_NORMAL first #-}
|
||||
first :: a -> Unfold m (a, b) c -> Unfold m b c
|
||||
first a = lmap (a, )
|
||||
|
||||
-- | Supply the second component of the tuple to an unfold that accepts a tuple
|
||||
-- as a seed resulting in a fold that accepts the first component of the tuple
|
||||
-- as a seed.
|
||||
--
|
||||
-- @
|
||||
-- supplySecond b = Unfold.lmap (, b)
|
||||
-- second b = Unfold.lmap (, b)
|
||||
-- @
|
||||
--
|
||||
-- /Pre-release/
|
||||
--
|
||||
{-# INLINE_NORMAL supplySecond #-}
|
||||
supplySecond :: b -> Unfold m (a, b) c -> Unfold m a c
|
||||
supplySecond b = lmap (, b)
|
||||
{-# INLINE_NORMAL second #-}
|
||||
second :: b -> Unfold m (a, b) c -> Unfold m a c
|
||||
second b = lmap (, b)
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Filter input
|
||||
|
@ -172,7 +172,7 @@ toChunks = toChunksWithBufferOf defaultChunkSize
|
||||
-- @since 0.7.0
|
||||
{-# INLINE readChunks #-}
|
||||
readChunks :: MonadIO m => Unfold m Handle (Array Word8)
|
||||
readChunks = UF.supplyFirst readChunksWithBufferOf defaultChunkSize
|
||||
readChunks = UF.first readChunksWithBufferOf defaultChunkSize
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
-- Read File to Stream
|
||||
|
@ -152,7 +152,7 @@ import qualified Streamly.Internal.Data.Unfold as UF
|
||||
-- >>> import qualified Streamly.Prelude as Stream
|
||||
--
|
||||
-- >>> import qualified Streamly.Internal.Data.Stream.IsStream as Stream
|
||||
-- >>> import qualified Streamly.Internal.Data.Unfold as Unfold (supplyFirst)
|
||||
-- >>> import qualified Streamly.Internal.Data.Unfold as Unfold (first)
|
||||
-- >>> import qualified Streamly.Internal.FileSystem.Handle as Handle
|
||||
-- >>> import qualified Streamly.Internal.System.IO as IO (defaultChunkSize)
|
||||
|
||||
@ -323,12 +323,12 @@ getChunks = getChunksWith defaultChunkSize
|
||||
-- size of arrays in the resulting stream are therefore less than or equal to
|
||||
-- 'Streamly.Internal.Data.Array.Foreign.Type.defaultChunkSize'.
|
||||
--
|
||||
-- >>> readChunks = Unfold.supplyFirst IO.defaultChunkSize Handle.readChunksWith
|
||||
-- >>> readChunks = Unfold.first IO.defaultChunkSize Handle.readChunksWith
|
||||
--
|
||||
-- @since 0.7.0
|
||||
{-# INLINE readChunks #-}
|
||||
readChunks :: MonadIO m => Unfold m Handle (Array Word8)
|
||||
readChunks = UF.supplyFirst defaultChunkSize readChunksWith
|
||||
readChunks = UF.first defaultChunkSize readChunksWith
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
-- Read File to Stream
|
||||
|
@ -161,29 +161,29 @@ acceptOnAddr = acceptOnAddrWith []
|
||||
acceptOnPortWith :: MonadIO m
|
||||
=> [(SocketOption, Int)]
|
||||
-> Unfold m PortNumber Socket
|
||||
acceptOnPortWith opts = UF.supplyFirst (0,0,0,0) (acceptOnAddrWith opts)
|
||||
acceptOnPortWith opts = UF.first (0,0,0,0) (acceptOnAddrWith opts)
|
||||
|
||||
-- | Like 'acceptOnAddr' but binds on the IPv4 address @0.0.0.0@ i.e. on all
|
||||
-- IPv4 addresses/interfaces of the machine and listens for TCP connections on
|
||||
-- the specified port.
|
||||
--
|
||||
-- > acceptOnPort = UF.supplyFirst acceptOnAddr (0,0,0,0)
|
||||
-- > acceptOnPort = UF.first acceptOnAddr (0,0,0,0)
|
||||
--
|
||||
-- @since 0.7.0
|
||||
{-# INLINE acceptOnPort #-}
|
||||
acceptOnPort :: MonadIO m => Unfold m PortNumber Socket
|
||||
acceptOnPort = UF.supplyFirst (0,0,0,0) acceptOnAddr
|
||||
acceptOnPort = UF.first (0,0,0,0) acceptOnAddr
|
||||
|
||||
-- | Like 'acceptOnAddr' but binds on the localhost IPv4 address @127.0.0.1@.
|
||||
-- The server can only be accessed from the local host, it cannot be accessed
|
||||
-- from other hosts on the network.
|
||||
--
|
||||
-- > acceptOnPortLocal = UF.supplyFirst acceptOnAddr (127,0,0,1)
|
||||
-- > acceptOnPortLocal = UF.first acceptOnAddr (127,0,0,1)
|
||||
--
|
||||
-- @since 0.7.0
|
||||
{-# INLINE acceptOnPortLocal #-}
|
||||
acceptOnPortLocal :: MonadIO m => Unfold m PortNumber Socket
|
||||
acceptOnPortLocal = UF.supplyFirst (127,0,0,1) acceptOnAddr
|
||||
acceptOnPortLocal = UF.first (127,0,0,1) acceptOnAddr
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
-- Accept (streams)
|
||||
|
@ -406,7 +406,7 @@ readChunksWithBufferOf = readChunksWith
|
||||
-- @since 0.7.0
|
||||
{-# INLINE readChunks #-}
|
||||
readChunks :: MonadIO m => Unfold m Socket (Array Word8)
|
||||
readChunks = UF.supplyFirst defaultChunkSize readChunksWith
|
||||
readChunks = UF.first defaultChunkSize readChunksWith
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
-- Read File to Stream
|
||||
@ -461,7 +461,7 @@ readWithBufferOf = readWith
|
||||
-- @since 0.7.0
|
||||
{-# INLINE read #-}
|
||||
read :: MonadIO m => Unfold m Socket Word8
|
||||
read = UF.supplyFirst defaultChunkSize readWith
|
||||
read = UF.first defaultChunkSize readWith
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
-- Writing
|
||||
|
@ -71,19 +71,19 @@ lmapM =
|
||||
let unf = UF.lmapM (\x -> modify (+ 1) >> return x) (UF.function id)
|
||||
in testUnfoldMD unf 1 0 1 [1]
|
||||
|
||||
supply :: Bool
|
||||
supply =
|
||||
let unf = UF.supply 1 (UF.function id)
|
||||
both :: Bool
|
||||
both =
|
||||
let unf = UF.both 1 (UF.function id)
|
||||
in testUnfold unf undefined ([1] :: [Int])
|
||||
|
||||
supplyFirst :: Bool
|
||||
supplyFirst =
|
||||
let unf = UF.supplyFirst 1 (UF.function id)
|
||||
first :: Bool
|
||||
first =
|
||||
let unf = UF.first 1 (UF.function id)
|
||||
in testUnfold unf 2 ([(1, 2)] :: [(Int, Int)])
|
||||
|
||||
supplySecond :: Bool
|
||||
supplySecond =
|
||||
let unf = UF.supplySecond 1 (UF.function id)
|
||||
second :: Bool
|
||||
second =
|
||||
let unf = UF.second 1 (UF.function id)
|
||||
in testUnfold unf 2 ([(2, 1)] :: [(Int, Int)])
|
||||
|
||||
discardFirst :: Bool
|
||||
@ -589,9 +589,9 @@ testInputOps =
|
||||
$ do
|
||||
-- prop "lmap" lmap
|
||||
prop "lmapM" lmapM
|
||||
prop "supply" supply
|
||||
prop "supplyFirst" supplyFirst
|
||||
prop "supplySecond" supplySecond
|
||||
prop "both" both
|
||||
prop "first" first
|
||||
prop "second" second
|
||||
prop "discardFirst" discardFirst
|
||||
prop "discardSecond" discardSecond
|
||||
prop "swap" swap
|
||||
|
@ -449,7 +449,7 @@ unfold :: Property
|
||||
unfold = monadicIO $ do
|
||||
a <- pick $ choose (0, max_length `div` 2)
|
||||
b <- pick $ choose (0, max_length)
|
||||
let unf = UF.supplySecond b UF.enumerateFromToIntegral
|
||||
let unf = UF.second b UF.enumerateFromToIntegral
|
||||
ls <- S.toList $ S.unfold unf a
|
||||
return $ ls == [a..b]
|
||||
|
||||
@ -457,7 +457,7 @@ unfold0 :: Property
|
||||
unfold0 = monadicIO $ do
|
||||
a <- pick $ choose (0, max_length `div` 2)
|
||||
b <- pick $ choose (0, max_length)
|
||||
let unf = UF.supply a (UF.supplySecond b UF.enumerateFromToIntegral)
|
||||
let unf = UF.both a (UF.second b UF.enumerateFromToIntegral)
|
||||
ls <- S.toList $ IS.unfold0 unf
|
||||
return $ ls == [a..b]
|
||||
|
||||
|
@ -1208,7 +1208,7 @@ transformCombineOpsCommon constr desc eq t = do
|
||||
forAll (choose (0, 100)) $ \n ->
|
||||
transform (concatMap (const [1..n]))
|
||||
t (S.unfoldMany (UF.lmap (const undefined)
|
||||
$ UF.supply [1..n] UF.fromList))
|
||||
$ UF.both [1..n] UF.fromList))
|
||||
|
||||
toListFL :: Monad m => FL.Fold m a [a]
|
||||
toListFL = FL.toList
|
||||
|
Loading…
Reference in New Issue
Block a user