How to work with IO (maybe (IO (Maybe t)))?

I am dealing with a library ( ghcjs-dom) in which each function returns IO (Maybe T).

I have a function awith returns IO (Maybe x)and a function bthat takes xas an argument and returns IO (Maybe y).

Is an operator that allows me to do a ??? band get IO (Maybe y). My Google search showed nothing.

I am looking for something like jointhat that works for IO (Maybe (IO (Maybe t)))instead of IO (IO t)or Maybe (Maybe t).

+4
source share
2 answers

From what I understand, you have:

a :: IO (Maybe X)
b :: X -> IO (Maybe Y)

IO (Maybe a) MaybeT IO a, : MaybeT :

MaybeT :: IO (Maybe a) -> MaybeT IO a

- runMaybeT:

runMaybeT :: MaybeT IO a -> IO (MaybeT a)

MaybeT , , :

MaybeT a >>= (\x -> MaybeT (b x)) :: MaybeT IO Y

MaybeT IO Y. IO (Maybe Y), runMaybeT.

"" a b:

andThen :: IO (Maybe a) -> (a -> IO (Maybe b)) -> IO (Maybe b)
andThen a b = runMaybeT $  MaybeT a >>= (\x ->  MaybeT (b x) )

, , , , MaybeT IO monad, >>= runMaybeT .

+10

MaybeT, sequenceA traverse Data.Traversable.

Prelude Data.Traversable Control.Monad> :t fmap join . join . fmap sequenceA 

fmap join . join . fmap sequenceA
  :: (Traversable m, Control.Applicative.Applicative f, Monad m,
      Monad f) =>
      f (m (f (m a))) -> f (m a)

f IO m .

+3

Source: https://habr.com/ru/post/1649952/


All Articles