Last active
March 29, 2019 09:49
-
-
Save chrisdone/57bd9456e43d3dab84e45e504af57f08 to your computer and use it in GitHub Desktop.
Real Haskell Program Samples
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
#!/usr/bin/env stack | |
-- stack --resolver lts-12.12 script | |
import Options.Applicative.Simple | |
import Data.Semigroup ((<>)) | |
data Sample = | |
Sample | |
{ sampleEnable :: Bool | |
, sampleUrl :: String | |
} deriving (Show) | |
main = do | |
(opts, ()) <- | |
simpleOptions | |
"1.0" | |
"Demo opts program" | |
"This program demonstrates commandline options." | |
(Sample | |
<$> flag False True (long "enable-the-thing" <> short 'e' <> help "Enable it!") | |
<*> strArgument (metavar "URLHERE" <> help "The URL")) | |
empty | |
print opts |
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
#!/usr/bin/env stack | |
-- stack --resolver lts-12.12 script | |
{-# LANGUAGE OverloadedStrings #-} | |
import Data.Aeson | |
main = do | |
print (decode "{\"age\":50,\"name\":\"Sofia\"}" :: Maybe Value) | |
print (encode out) | |
out :: [(Int, String)] | |
out = [(1,"Hello"),(2,"World!")] |
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
#!/usr/bin/env stack | |
-- stack --resolver lts-12.12 script | |
{-# LANGUAGE OverloadedStrings #-} | |
import Network.HTTP.Simple | |
import qualified Data.ByteString.Char8 as B8 | |
main :: IO () | |
main = do | |
response <- httpBS "http://example.com" | |
B8.putStrLn (getResponseBody response) |
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
#!/usr/bin/env stack | |
-- stack --resolver lts-12.12 script | |
{-# LANGUAGE OverloadedStrings #-} | |
import Network.Wai | |
import Network.HTTP.Types | |
import Network.Wai.Handler.Warp (run) | |
app :: Application | |
app _ respond = do | |
putStrLn "I've done some IO here" | |
respond | |
(responseLBS | |
status200 | |
[("Content-Type", "text/plain")] | |
"Hello, Web!") | |
main :: IO () | |
main = do | |
putStrLn "http://localhost:8080/" | |
run 8080 app |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment