From 6ea3baee362c1c68c499e876d0b6b4fe3388138a Mon Sep 17 00:00:00 2001 From: dullbananas Date: Sun, 3 Jul 2022 20:25:51 -0700 Subject: [PATCH 1/2] impure_elm_function/index.html --- impure_elm_function/index.html | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 impure_elm_function/index.html diff --git a/impure_elm_function/index.html b/impure_elm_function/index.html new file mode 100644 index 0000000..017bde2 --- /dev/null +++ b/impure_elm_function/index.html @@ -0,0 +1,17 @@ + + + +
+ + + From 0ae171bae83afbd69bd254fe480a7a9864391e48 Mon Sep 17 00:00:00 2001 From: dullbananas Date: Sun, 3 Jul 2022 20:26:56 -0700 Subject: [PATCH 2/2] impure_elm_function/Main.elm --- impure_elm_function/Main.elm | 53 ++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 impure_elm_function/Main.elm diff --git a/impure_elm_function/Main.elm b/impure_elm_function/Main.elm new file mode 100644 index 0000000..dc4c344 --- /dev/null +++ b/impure_elm_function/Main.elm @@ -0,0 +1,53 @@ +module Main exposing (main) + +import Browser +import Html exposing (Html, button, div, text) +import Html.Events exposing (onClick) +import Json.Decode as D + + +type alias Model = + { num : Float, obj : D.Value } + + +init : D.Value -> ( Model,Cmd Msg ) +init flags = + ({ num = 0, obj = flags }, Cmd.none) + + +type Msg + = Clicked + + +-- The impure function +generate : D.Value -> Float +generate = + D.decodeValue (D.field "n" D.float) >> Result.withDefault 0 + + +update : Msg -> Model -> (Model,Cmd Msg) +update msg model = + case msg of + Clicked -> + ( { model + | num = generate model.obj + } + ,Cmd.none) + + +view : Model -> Html Msg +view model = + div [] + [ button [ onClick Clicked ] [ text "generate" ] + , div [] [ text <| String.fromFloat model.num ] + ] + + +main : Program D.Value Model Msg +main = + Browser.element + { init = init + , view = view + , update = update + , subscriptions = always Sub.none + }