Skip to content

Instantly share code, notes, and snippets.

@pharpend
Created September 17, 2019 00:48
Show Gist options
  • Save pharpend/2f5a45712cddc59d9322d70443ab8482 to your computer and use it in GitHub Desktop.
Save pharpend/2f5a45712cddc59d9322d70443ab8482 to your computer and use it in GitHub Desktop.
Stack repl script
#!/usr/bin/env stack
{- stack script
--resolver lts-14.6
--package aeson
--package bytestring
-}
{-# OPTIONS_GHC -Wall #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
module Main where
-- import qualified Conduit
-- import qualified Data.Conduit.Attoparsec as Conduit
import Data.Aeson (ToJSON, FromJSON)
import qualified Data.Aeson as Aeson
import qualified Data.ByteString as ByteString
import qualified Data.ByteString.Char8 as ByteString.Char8
import qualified Data.ByteString.Lazy.Char8 as ByteString.Lazy.Char8
import qualified System.IO
class (FromJSON i, ToJSON o) => Jsrv i o where
jsrvEval :: i -> o
-- eitherFail :: Monad m => Either String s => m s
-- eitherFail (Left s) = fail s
-- eitherFail (Right s) = return s
repl :: (FromJSON i, ToJSON o) => (i -> o) -> IO ()
repl eval = do
i' <- readStdin
case i' of
Left err -> do
System.IO.hPutStrLn System.IO.stderr err
repl eval
Right i -> do
let o = eval i
ByteString.Lazy.Char8.putStrLn $ Aeson.encode o
repl eval
where
readStdin :: FromJSON x => IO (Either String x)
readStdin = do
ln <- ByteString.getLine
return $ Aeson.eitherDecodeStrict ln
main :: IO ()
main = repl (id :: Aeson.Value -> Aeson.Value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment