module Potoki.Core.Produce where
import Potoki.Core.Prelude
import qualified Potoki.Core.Fetch as A
import qualified Potoki.Core.Transform.Types as B
newtype Produce element =
Produce (IO (A.Fetch element, IO ()))
deriving instance Functor Produce
instance Applicative Produce where
pure x =
list [x]
(<*>) (Produce leftIO) (Produce rightIO) =
Produce $ do
(leftFetch, leftKill) <- leftIO
(rightFetch, rightKill) <- rightIO
return (leftFetch <*> rightFetch, leftKill >> rightKill)
instance Alternative Produce where
empty =
Produce (pure (empty, pure ()))
(<|>) (Produce leftIO) (Produce rightIO) =
Produce $ do
(leftFetch, leftKill) <- leftIO
(rightFetch, rightKill) <- rightIO
return (leftFetch <|> rightFetch, leftKill >> rightKill)
list :: [input] -> Produce input
list list =
Produce $ do
unsentListRef <- newIORef list
return (A.list unsentListRef, return ())
transform :: B.Transform input output -> Produce input -> Produce output
transform (B.Transform transformIO) (Produce produceIO) =
Produce $ do
(fetch, kill) <- produceIO
newFetch <- transformIO fetch
return (newFetch, kill)