feat: Pipes.Collect

This commit is contained in:
orion 2024-05-10 18:14:05 -05:00
parent 30fbce3a2d
commit f373334f77
Signed by: orion
GPG Key ID: 6D4165AE4C928719
2 changed files with 18 additions and 1 deletions

View File

@ -32,7 +32,6 @@ package:
- strings: ">=6.0.1 <7.0.0" - strings: ">=6.0.1 <7.0.0"
- tailrec: ">=6.1.0 <7.0.0" - tailrec: ">=6.1.0 <7.0.0"
- transformers: ">=6.0.0 <7.0.0" - transformers: ">=6.0.0 <7.0.0"
- tuples: ">=7.0.0 <8.0.0"
- unsafe-coerce: ">=6.0.0 <7.0.0" - unsafe-coerce: ">=6.0.0 <7.0.0"
test: test:
main: Test.Main main: Test.Main

18
src/Pipes.Collect.purs Normal file
View File

@ -0,0 +1,18 @@
module Pipes.Collect where
import Prelude
import Control.Monad.Rec.Class (class MonadRec)
import Control.Monad.ST.Class (liftST)
import Data.Array.ST as Array.ST
import Effect.Class (class MonadEffect, liftEffect)
import Pipes (for) as Pipes
import Pipes.Core (Producer)
import Pipes.Core (runEffect) as Pipes
-- | Traverse a pipe, collecting into a mutable array with constant stack usage
collectArray :: forall a m. MonadRec m => MonadEffect m => Producer a m Unit -> m (Array a)
collectArray p = do
st <- liftEffect $ liftST $ Array.ST.new
Pipes.runEffect $ Pipes.for p \a -> void $ liftEffect $ liftST $ Array.ST.push a st
liftEffect $ liftST $ Array.ST.unsafeFreeze st