on master help sign in
improve doc and add named combinators for parallel apply
a3c6ad3Harendra Kumar
src/Streamly/Internal/Prelude.hs+54−17
@@ -78,6 +78,11 @@ module Streamly.Internal.Prelude
78 , foldl1'
78 , foldl1'
79 , foldlM'
79 , foldlM'
80
80
81+ -- ** Concurrent Folds
82+ , foldAsync
83+ , (|$.)
84+ , (|&.)
85+
81 -- ** Full Folds
86 -- ** Full Folds
82
87
83 -- -- ** To Summary (Full Folds)
88 -- -- ** To Summary (Full Folds)
@@ -132,12 +137,6 @@ module Streamly.Internal.Prelude
132 , toStream -- XXX rename to write?
137 , toStream -- XXX rename to write?
133 , toStreamRev -- XXX rename to writeRev?
138 , toStreamRev -- XXX rename to writeRev?
134
139
135− -- * Function application
136− , (|$)
137− , (|&)
138− , (|$.)
139− , (|&.)
140−
141 -- * Transformation
140 -- * Transformation
142 , transform
141 , transform
143
142
@@ -172,6 +171,11 @@ module Streamly.Internal.Prelude
172 -- , lprescanl'
171 -- , lprescanl'
173 -- , lprescanlM'
172 -- , lprescanlM'
174
173
174+ -- ** Concurrent Transformation
175+ , applyAsync
176+ , (|$)
177+ , (|&)
178+
175 -- ** Indexing
179 -- ** Indexing
176 , indexed
180 , indexed
177 , indexedR
181 , indexedR
@@ -1556,10 +1560,19 @@ infixr 0 |$.
1556infixl 1 |&
1560infixl 1 |&
1557infixl 1 |&.
1561infixl 1 |&.
1558
1562
1559−-- | Parallel function application operator for streams; just like the regular
1563+-- | Parallel transform application operator; applies a stream transformation
1564+-- function @t m a -> t m b@ to a stream @t m a@ concurrently; the input stream
1565+-- is evaluated asynchronously in an independent thread yielding elements to a
1566+-- buffer and the transformation function runs in another thread consuming the
1567+-- input from the buffer. '|$' is just like regular function application
1560−-- function application operator '$' except that it is concurrent. The
1568+-- operator '$' except that it is concurrent.
1569+--
1570+-- If you read the signature as @(t m a -> t m b) -> (t m a -> t m b)@ you can
1571+-- look at it as a transformation that converts a transform function to a
1572+-- buffered concurrent transform function.
1573+--
1561−-- following code prints a value every second even though each stage adds a 1
1574+-- The following code prints a value every second even though each stage adds a
1562−-- second delay.
1575+-- 1 second delay.
1563--
1576--
1564--
1577--
1565-- @
1578-- @
@@ -1572,11 +1585,20 @@ infixl 1 |&.
1572--
1585--
1573-- @since 0.3.0
1586-- @since 0.3.0
1574{-# INLINE (|$) #-}
1587{-# INLINE (|$) #-}
1575−(|$) :: (IsStream t, MonadAsync m) => (t m a -> t m b) -> t m a -> t m b
1588+(|$) :: (IsStream t, MonadAsync m) => (t m a -> t m b) -> (t m a -> t m b)
1576f |$ x = D.fromStreamD $
1589f |$ x = D.fromStreamD $
1577 D.applyParallel (D.toStreamD . f . D.fromStreamD) (D.toStreamD x)
1590 D.applyParallel (D.toStreamD . f . D.fromStreamD) (D.toStreamD x)
1578-- (|$) = Par.applyParallel
1591-- (|$) = Par.applyParallel
1579
1592
1593+-- | Same as (|$).
1594+--
1595+-- /Internal/
1596+--
1597+{-# INLINE applyAsync #-}
1598+applyAsync :: (IsStream t, MonadAsync m)
1599+ => (t m a -> t m b) -> (t m a -> t m b)
1600+applyAsync = (|$)
1601+
1580-- | Parallel reverse function application operator for streams; just like the
1602-- | Parallel reverse function application operator for streams; just like the
1581-- regular reverse function application operator '&' except that it is
1603-- regular reverse function application operator '&' except that it is
1582-- concurrent.
1604-- concurrent.
@@ -1594,11 +1616,18 @@ f |$ x = D.fromStreamD $
1594(|&) :: (IsStream t, MonadAsync m) => t m a -> (t m a -> t m b) -> t m b
1616(|&) :: (IsStream t, MonadAsync m) => t m a -> (t m a -> t m b) -> t m b
1595x |& f = f |$ x
1617x |& f = f |$ x
1596
1618
1597−-- | Parallel function application operator; applies a @run@ or @fold@ function
1619+-- | Parallel fold application operator; applies a fold function @t m a -> m b@
1598−-- to a stream such that the fold consumer and the stream producer run in
1620+-- to a stream @t m a@ concurrently; The the input stream is evaluated
1599−-- parallel. A @run@ or @fold@ function reduces the stream to a value in the
1621+-- asynchronously in an independent thread yielding elements to a buffer and
1622+-- the folding action runs in another thread consuming the input from the
1623+-- buffer.
1624+--
1625+-- If you read the signature as @(t m a -> m b) -> (t m a -> m b)@ you can look
1626+-- at it as a transformation that converts a fold function to a buffered
1627+-- concurrent fold function.
1628+--
1600−-- underlying monad. The @.@ at the end of the operator is a mnemonic for
1629+-- The @.@ at the end of the operator is a mnemonic for termination of the
1601−-- termination of the stream.
1630+-- stream.
1602--
1631--
1603-- @
1632-- @
1604-- S.foldlM' (\\_ a -> threadDelay 1000000 >> print a) ()
1633-- S.foldlM' (\\_ a -> threadDelay 1000000 >> print a) ()
@@ -1609,10 +1638,18 @@ x |& f = f |$ x
1609--
1638--
1610-- @since 0.3.0
1639-- @since 0.3.0
1611{-# INLINE (|$.) #-}
1640{-# INLINE (|$.) #-}
1612−(|$.) :: (IsStream t, MonadAsync m) => (t m a -> m b) -> t m a -> m b
1641+(|$.) :: (IsStream t, MonadAsync m) => (t m a -> m b) -> (t m a -> m b)
1613(|$.) = D.foldParallel
1642(|$.) = D.foldParallel
1614-- (|$.) = Par.foldParallel
1643-- (|$.) = Par.foldParallel
1615
1644
1645+-- | Same as '(|$.)'.
1646+--
1647+-- /Internal/
1648+--
1649+{-# INLINE foldAsync #-}
1650+foldAsync :: (IsStream t, MonadAsync m) => (t m a -> m b) -> (t m a -> m b)
1651+foldAsync = (|$.)
1652+
1616-- | Parallel reverse function application operator for applying a run or fold
1653-- | Parallel reverse function application operator for applying a run or fold
1617-- functions to a stream. Just like '|$.' except that the operands are reversed.
1654-- functions to a stream. Just like '|$.' except that the operands are reversed.
1618--
1655--