Skip to content

Instantly share code, notes, and snippets.

@ClathomasPrime
Created April 16, 2019 22:21
Show Gist options
  • Save ClathomasPrime/25c28df822353a1bd8d0131825986f5f to your computer and use it in GitHub Desktop.
Save ClathomasPrime/25c28df822353a1bd8d0131825986f5f to your computer and use it in GitHub Desktop.
MonadState code which only "actually" alters state if some subcomputation succedes
performIfSuccedes :: MonadState s m => m (Maybe a) -> m (Maybe a)
performIfSuccedes m = do
s <- get
res <- m
case res of
Nothing -> put s >> return Nothing
Just a -> return (Just a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment