Deep stack maybe with Yesod

I am trying to establish an authorization scheme, where I verify that 1. the user is registered 2. the user has access to a specific object. To do this, I first call maybeAuthId, then try to get the current object and "join" to another table that lists the permissions. There are two levels of possible cases and one level of case with an empty list. I thought about using MaybeT, but either I'm too tired to make it work, or non-force-transformer-manipulator transformers cannot be used with MaybeT. Is there a good way to deal with deep maya?

Edit:

I was a bit unclear, it seems. I meant that I have something like this:

case foo of
   Nothing -> something
   Just foo' -> do
      bar <- somethingelse
      case bar of
         Nothing -> ...
         Just bar' -> ...
+5
3

MaybeT Yesod. :

runMaybeT $ do
  uid <- MaybeT maybeAuthID
  car <- MaybeT . runDB . getBy $ UniqueCarOwner uid
  location <- MaybeT . liftIO . ciaLocateLicensePlate . licensePlate $ car
  country <- MaybeT . findCountry $ location
  return (car, country)

, Yesod. , - Monad m => m (Maybe a), MaybeT, Monad m => Maybe (m a).

+6

, , :

Maybe [Maybe r]

... join Maybe, . sequence :

sequence :: (Monad m) => [m r] -> m [r]

, sequence Maybe, :

sequence :: [Maybe r] -> Maybe [r]

sequence Nothing, Nothing, Just s, Just.

sequence Maybe:

fmap sequence :: Maybe [Maybe r] -> Maybe (Maybe [r])

, :

join . fmap sequence :: Maybe [Maybe r] -> Maybe [r]

, , , , Maybe Just, Maybe Just, Just . , Maybe ( ) Nothing, Nothing.

, , , , , . , .

+2

It's not entirely clear what you mean by “deep mayback processing”, but you can use monadic join(from Control.Monad) to remove one level of nesting at a time.

ghci> :m +Control.Monad
ghci> join (Just (Just 3))
Just 3
ghci> join (Just Nothing)
Nothing
ghci> join Nothing
Nothing

Using MaybeT is probably best suited to your problem. If you clarify what you are trying to do, we could help you formulate it using MaybeT.

+1
source

All Articles