public
Created

Possible Monad instance for Source?

  • Download Gist
gistfile1.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
sourceJoin :: Resource m => Source m (Source m a) -> Source m a
sourceJoin s = Source $ do
ps <- prepareSource s
innerSource <- newRef Nothing
let pull = do inner <- readRef innerSource
case inner of
Just x -> do st <- sourcePull x
case st of
Closed -> do pullFrom Nothing
Open vs -> return $ Open vs
Nothing -> do st <- sourcePull ps
case st of
Open xs -> do inner' <- prepareSource xs
pullFrom (Just inner')
Closed -> return Closed
pullFrom x = writeRef innerSource x >> pull
close = do maybe (return ()) sourceClose =<< readRef innerSource
sourceClose ps
return $ PreparedSource {
sourcePull = pull
, sourceClose = close
}
 
sourceReturn :: Resource m => a -> Source m a
sourceReturn x = sourceState True
(\s -> case s of
False -> return (False, Closed)
True -> return (False, Open x))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.