Thread the dict through the interaction loop.

Like a total newbie I put the call to the interpreter in the view
function instead of the update function!  I thought it was weird having
to specify the HTML twice, but I figured I was just doing it wrong.  I
was, but not in the way I suspected.

In any event, I like how this make it clear that errors can't affect the
dictionary.
This commit is contained in:
sforman 2023-08-02 21:19:43 -07:00
parent 2fe886afba
commit ee4c3bc9b6
1 changed files with 8 additions and 10 deletions

View File

@ -22,13 +22,14 @@ main =
type alias Model = type alias Model =
{ content : String { content : String
, evaluated : String
, dictionary : JoyDict , dictionary : JoyDict
} }
init : Model init : Model
init = init =
{ content = "", dictionary = initialize Dict.empty } { content = "", evaluated = "", dictionary = initialize Dict.empty }
@ -43,7 +44,11 @@ update : Msg -> Model -> Model
update msg model = update msg model =
case msg of case msg of
Change newContent -> Change newContent ->
{ model | content = newContent } case doit newContent model.dictionary of
Err err ->
{ model | content = newContent, evaluated = err}
Ok (output, dict) ->
{ content = newContent, evaluated = output, dictionary = dict }
@ -52,15 +57,8 @@ update msg model =
view : Model -> Html Msg view : Model -> Html Msg
view model = view model =
case doit model.content model.dictionary of
Err msg ->
div [] div []
[ input [ placeholder "Text to reverse", value model.content, onInput Change ] [] [ input [ placeholder "Text to reverse", value model.content, onInput Change ] []
, div [] [ text msg ] , div [] [ text model.evaluated ]
]
Ok (message, dict0) ->
div []
[ input [ placeholder "Text to reverse", value model.content, onInput Change ] []
, div [] [ text message ]
] ]