-
Notifications
You must be signed in to change notification settings - Fork 2
/
Util.hs
24 lines (21 loc) · 894 Bytes
/
Util.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
module Util where
-- dunai
import Control.Monad.Trans.MSF.Except
import Data.MonadicStreamFunction
import Data.MonadicStreamFunction.InternalCore
-- | Accumulates inputs and starts an MSF for each of them
pool :: Monad m => (a -> MSF m () b) -> MSF m [a] [b]
pool f = pool' f []
where
pool' :: Monad m => (a -> MSF m () b) -> [MSF m () b] -> MSF m [a] [b]
pool' f msfs = MSF $ \as -> do
let moremsfs = msfs ++ map f as
(bs, msfs') <- unzip <$> sequence (map (flip unMSF ()) moremsfs)
return (bs, pool' f msfs')
-- | Remembers and indefinitely outputs the first input value.
keepFirst :: Monad m => MSF m a a
keepFirst = safely $ do
a <- try throwS
safe $ arr $ const a
-- TODO This would somewhere in dunai, but putting it in Data.MonadicStreamFunction.Util forms an import cycle
-- => break the cycle by making all the Trans.MSF modules depend on Core only?