Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement try #66

Open
BowenFu opened this issue Aug 8, 2022 · 6 comments
Open

Implement try #66

BowenFu opened this issue Aug 8, 2022 · 6 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@BowenFu
Copy link
Owner

BowenFu commented Aug 8, 2022

try :: Exception e => IO a -> IO (Either e a)

@BowenFu
Copy link
Owner Author

BowenFu commented Aug 8, 2022

Implement throw_, throwIO, and
onException io what
= io catch \e -> do _ <- what
throwIO (e :: SomeException)

@BowenFu
Copy link
Owner Author

BowenFu commented Aug 8, 2022

data Async a = Async (MVar (Either SomeException a)) --
async :: IO a -> IO (Async a) async action = do
var <- newEmptyMVar
forkIO (do r <- try action; putMVar var r) -- return (Async var)
waitCatch :: Async a -> IO (Either SomeException a) -- waitCatch (Async var) = readMVar var
wait :: Async a -> IO a -- wait a = do
r <- waitCatch a case r of
Left e -> throwIO e
Right a -> return a

@BowenFu
Copy link
Owner Author

BowenFu commented Aug 8, 2022

bracket :: IO a -> (a -> IO b) -> (a -> IO c) -> IO c bracket before after during = do
a <- before
c <- during a onException after a after a
return c

finally :: IO a -> IO b -> IO a finally io after = do
io onException after after

@BowenFu
Copy link
Owner Author

BowenFu commented Aug 8, 2022

waitEither :: Async a -> Async b -> IO (Either a b) waitEither a b = do
m <- newEmptyMVar
forkIO $ do r <- try (fmap Left (wait a)); putMVar m r forkIO $ do r <- try (fmap Right (wait b)); putMVar m r wait (Async m)

@BowenFu
Copy link
Owner Author

BowenFu commented Aug 8, 2022

waitAny :: [Async a] -> IO a waitAny as = do
m <- newEmptyMVar
let forkwait a = forkIO $ do r <- try (wait a); putMVar m r mapM_ forkwait as
wait (Async m)

@BowenFu BowenFu added the good first issue Good for newcomers label Aug 27, 2022
@BowenFu BowenFu added the enhancement New feature or request label Sep 4, 2022
@BowenFu
Copy link
Owner Author

BowenFu commented Sep 11, 2022

Either has been implemented now.

@BowenFu BowenFu changed the title Implement Either and try Implement try Sep 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant