Skip to content

Instantly share code, notes, and snippets.

@carlosmaniero
Last active October 30, 2017 13:19
Show Gist options
  • Save carlosmaniero/cc2144fe693b5b5787aeb0682733c8d1 to your computer and use it in GitHub Desktop.
Save carlosmaniero/cc2144fe693b5b5787aeb0682733c8d1 to your computer and use it in GitHub Desktop.
Elm architecture in Haskell: A study case
data Model = Model
{ name :: String
, gender :: String
, loading :: Bool
, homeworld :: String
} deriving (Show)
data PersonResponse = PersonResponse
{ personName :: String
, personGender :: String
, personLoading :: Bool
, personHomeworldUrl :: String
} deriving (Show)
data RealWorldResponse = RealWorldResponse
{ worldName :: String
} deriving (Show)
data Msg
= PersonResponseMsg PersonResponse
| RealWorldResponseMsg RealWorldResponse
deriving (Show)
update :: Model -> Msg -> (Model, Operation)
update model msg =
case msg of
PersonResponseMsg personResponse ->
(model { name = personName personResponse }, Cmd doRealWorldRequest)
RealWorldResponseMsg realWorldResponse ->
( model { homeworld = worldName realWorldResponse }, CmdNone)
doPersonRequest :: IO Msg
doPersonRequest =
return $ PersonResponseMsg $ PersonResponse
{ personName = "Carlos"
, personGender = "male"
, personLoading = False
, personHomeworldUrl = "https://github.com/carlosmaniero/"
}
doRealWorldRequest :: IO Msg
doRealWorldRequest =
return $ RealWorldResponseMsg $ RealWorldResponse { worldName = "Moon" }
data Operation
= Cmd (IO Msg)
| CmdNone
startApplication :: Model -> IO Msg -> IO Model
startApplication model cmd = do
msg <- cmd
putStrLn $ show msg
putStrLn $ show model
let (model, operation) = update model msg
case operation of
Cmd nextCmd ->
startApplication model nextCmd
CmdNone ->
return model
main = do
let model = Model { name = ""
, gender = ""
, loading = False
, homeworld = ""
}
finalModel <- startApplication model doPersonRequest
putStrLn $ show finalModel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment