Last active
March 22, 2020 07:06
-
-
Save ababup1192/c970acad1509d70968052fdf6af76601 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main exposing (main) | |
import Browser | |
import Html exposing (Html, input, li, text, ul) | |
import Html.Attributes exposing (value) | |
import Html.Events exposing (onInput) | |
-- MAIN | |
main : Program () Model Msg | |
main = | |
Browser.element | |
{ init = init | |
, update = update | |
, view = view | |
, subscriptions = subscriptions | |
} | |
-- MODEL | |
type alias Model = | |
{ p1 : Point, p2 : Point } | |
init : () -> ( Model, Cmd Msg ) | |
init _ = | |
( { p1 = { x = 1, y = 1 }, p2 = { x = 2, y = 3 } }, Cmd.none ) | |
-- UPDATE | |
type Msg | |
= UpdateP1X String | |
| UpdateP1Y String | |
| UpdateP2X String | |
| UpdateP2Y String | |
update : Msg -> Model -> ( Model, Cmd Msg ) | |
update msg model = | |
let | |
{ p1, p2 } = | |
model | |
in | |
case msg of | |
UpdateP1X xStr -> | |
let | |
newP1 = | |
case String.toInt xStr of | |
Just x -> | |
{ p1 | x = x } | |
Nothing -> | |
p1 | |
in | |
( { model | p1 = newP1 }, Cmd.none ) | |
UpdateP1Y yStr -> | |
let | |
newP1 = | |
case String.toInt yStr of | |
Just y -> | |
{ p1 | y = y } | |
Nothing -> | |
p1 | |
in | |
( { model | p1 = newP1 }, Cmd.none ) | |
UpdateP2X xStr -> | |
let | |
newP2 = | |
case String.toInt xStr of | |
Just x -> | |
{ p2 | x = x } | |
Nothing -> | |
p2 | |
in | |
( { model | p2 = newP2 }, Cmd.none ) | |
UpdateP2Y yStr -> | |
let | |
newP2 = | |
case String.toInt yStr of | |
Just y -> | |
{ p2 | y = y } | |
Nothing -> | |
p2 | |
in | |
( { model | p2 = newP2 }, Cmd.none ) | |
-- VIEW | |
boolToString : Bool -> String | |
boolToString bool = | |
if bool then | |
"true" | |
else | |
"false" | |
type alias Point = | |
{ x : Int, y : Int } | |
pointToString : Point -> String | |
pointToString { x, y } = | |
"(" ++ String.fromInt x ++ ", " ++ String.fromInt y ++ ")" | |
plus : Point -> Point -> Point | |
plus p1 p2 = | |
{ x = p1.x + p2.x, y = p1.y + p2.y } | |
distance : Point -> Point -> Float | |
distance p1 p2 = | |
sqrt <| toFloat <| (p1.x - p2.x) ^ 2 + (p1.y - p2.y) ^ 2 | |
view : Model -> Html Msg | |
view model = | |
let | |
{ p1, p2 } = | |
model | |
in | |
ul [] | |
[ li [] [ text <| "p1 = (x = ", input [ value <| String.fromInt p1.x, onInput UpdateP1X ] [], text ",y = ", input [ value <| String.fromInt p1.y, onInput UpdateP1Y ] [], text ")" ] | |
, li [] [ text <| "p2 = (x = ", input [ value <| String.fromInt p2.x, onInput UpdateP2X ] [], text ",y = ", input [ value <| String.fromInt p2.y, onInput UpdateP2Y ] [], text ")" ] | |
, li [] [ text <| "(p1 == p2) == " ++ boolToString (p1 == p2) ] | |
, li [] [ text <| "p1 + p2 = " ++ pointToString (plus p1 p2) ] | |
, li [] [ text <| "distance p1, p2 = " ++ (String.fromFloat <| distance p1 p2) ] | |
] | |
subscriptions : Model -> Sub Msg | |
subscriptions _ = | |
Sub.none |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment