Created
December 22, 2014 08:17
-
-
Save carymrobbins/426756910f39020a5b63 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Control.Monad.Reader | |
| import System.IO | |
| import System.Process | |
| data NodeJS = NodeJS | |
| { nodeIn :: Handle | |
| , nodeOut :: Handle | |
| , nodeErr :: Handle | |
| , nodePid :: ProcessHandle | |
| } | |
| type NodeT = ReaderT NodeJS | |
| runNodeJS :: MonadIO m => String -> NodeT m a -> m a | |
| runNodeJS command action = do | |
| (inp, out, err, pid) <- liftIO $ runInteractiveCommand command | |
| forM_ [inp, out, err] $ \h -> liftIO $ hSetBinaryMode h False | |
| runReaderT action $ NodeJS inp out err pid | |
| node :: MonadIO m => String -> NodeT m String | |
| node expr = do | |
| n <- ask | |
| liftIO $ hPutStrLn (nodeIn n) $ "console.log(" ++ expr ++ ");" | |
| liftIO $ hGetLine (nodeOut n) | |
| good :: IO String | |
| good = runNodeJS "node" $ node "1+1" | |
| good' :: IO Bool | |
| good' = runNodeJS "node" $ fmap (== "2") (node "1+1") | |
| bad :: IO Bool | |
| bad = runNodeJS "node" $ do | |
| x <- node "1+1" | |
| return $ x == "2" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment