forked from simonmar/haskell-eXchange-2015
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathS.hs
45 lines (33 loc) · 1015 Bytes
/
S.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
{-# LANGUAGE OverloadedStrings #-}
module S where
import Control.Monad.Trans.Reader
import qualified Data.Text as T
import Data.Text.Read as R ( decimal )
import Data.Either ( fromRight )
import Data.Text.IO as TIO
import Text.Printf ( printf )
getHost' :: T.Text -> T.Text
getHost' = T.takeWhile (/= ':')
getPort' :: T.Text -> Int
getPort' hp = either (const 0) fst (decimal port)
where port = T.takeWhileEnd (/= ':') hp
-- getPort' hp = fromRight 0 $ fmap fst (decimal hp)
getHost :: Reader T.Text T.Text
getHost = getHost' <$> ask
getPort :: Reader T.Text Int
getPort = getPort' <$> ask
myHost :: T.Text
myHost = "myHost:8080"
printConfig :: Reader T.Text (IO ())
printConfig = do
-- host <- (reader . runReader) getHost
-- host <- getHost
host <- asks getHost'
-- port <- getPort
printf "Host: '%s' Port: '%i'\n" host <$> getPort
printHost :: T.Text -> IO ()
printHost = runReader printConfig
h1 :: IO ()
h1 = printHost myHost
h2 :: IO ()
h2 = printHost "www.nemesis.ch:8091"