我正在尝试用PureScript将协程包住头,更具体地说,我现在正在尝试将某种过滤器应用于生产者。
请考虑以下代码,它只是连接到记录器(消费者)的随机数生成器(生产者):
producer :: Producer Int Aff Int
producer = produceAff go
where
go emitter = do
n <- liftEffect $ randomInt 0 1000
emit emitter n
when (n > 900) do
close emitter n
delay (Milliseconds 250.0)
go emitter
logNumbers :: Consumer Int Aff Int
logNumbers = forever $ consumer \n -> do
liftEffect $ logShow n
pure Nothing
main :: Effect Unit
main = void
$ runAff (either logShow \lastN -> log ("output: " <> (show lastN)))
$ runProcess
$ (producer $$ logNumbers)
如何不触碰producer
和logNumbers
的代码,如何防止某些数字到达logNumbers
?例如丢弃所有低于500的数字。