-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathReader.purs
34 lines (29 loc) · 945 Bytes
/
Reader.purs
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
module Example.Reader where
import Prelude
import Control.Monad.Reader (Reader, asks, runReader)
import Data.Either (Either(..))
import Data.List.Lazy (replicateM)
import Data.Maybe (Maybe, fromMaybe)
import Effect (Effect)
import Effect.Console (log)
import Node.Process (getEnv)
import Type.Proxy (Proxy(..))
import TypedEnv (fromEnv) as TypedEnv
import TypedEnv (printEnvError)
type Config =
{ "USERNAME" :: Maybe String
, "REPEAT" :: Maybe Int
}
main :: Effect Unit
main = do
env <- TypedEnv.fromEnv (Proxy :: Proxy Config) <$> getEnv
case env of
Left error ->
log $ "ERROR: " <> printEnvError error
Right config@{ "REPEAT": repeat } -> do
_ <- replicateM (1 + fromMaybe 0 repeat) $ log $ runReader greeting config
pure unit
greeting :: forall r. Reader { "USERNAME" :: Maybe String | r } String
greeting = asks _."USERNAME" >>= \username -> pure $ "Hello, "
<> fromMaybe "Sailor" username
<> "!"