Skip to content

Instantly share code, notes, and snippets.

@dysinger
Last active February 9, 2022 18:07
Show Gist options
  • Save dysinger/a096e08d78c5308a1c82a3434e8b6619 to your computer and use it in GitHub Desktop.
Save dysinger/a096e08d78c5308a1c82a3434e8b6619 to your computer and use it in GitHub Desktop.
Single-file executable literate Haskell with Stack
#!/usr/bin/env stack
> -- stack --resolver lts-6 --install-ghc runghc --package classy-prelude --package lens --package wreq
Blah Blah words about this single file executable README goes here.
> {-# LANGUAGE DeriveAnyClass #-}
> {-# LANGUAGE DeriveGeneric #-}
> {-# LANGUAGE NoImplicitPrelude #-}
> {-# LANGUAGE OverloadedStrings #-}
explain explain
> import ClassyPrelude
> import Control.Lens
> import Network.Wreq
> import Data.Aeson
more explaining
> data IPInfo = IPInfo
> { ip :: Text
> , hostname :: Text
> , city :: Text
> , region :: Text
> , country :: Text
> , loc :: Text
> , org :: Text
> , postal :: Text
> } deriving (Show,Generic,ToJSON,FromJSON)
you are probably confused right now. let me explain
> main = do
> let opts = defaults & header "User-Agent" .~ ["curl/7.47.0"]
> eInfo <- eitherDecode . view responseBody <$> getWith opts "http://ipinfo.io"
> either print (putStrLn . city) (eInfo :: Either String IPInfo)
when I run it like this:
user@computer:~/src% ./README.lhs
Portland
you can see that I'm in Portland
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment