Skip to content

Instantly share code, notes, and snippets.

@napcs
Last active December 3, 2015 05:01
Show Gist options
  • Save napcs/5b910cfabdd944afedda to your computer and use it in GitHub Desktop.
Save napcs/5b910cfabdd944afedda to your computer and use it in GitHub Desktop.
module PaintCalculator where
import Html exposing (..)
import Html.Attributes exposing(..)
import Html.Events exposing(..)
import String exposing (..)
import Signal exposing (Address)
import StartApp.Simple as StartApp
type alias Model =
{
length: String,
width: String,
gallons: Int
}
initialModel : Model
initialModel =
{
length = "0",
width = "0",
gallons = 0
}
type Action
= NoOp
| SetLength String
| SetWidth String
| Calculate
update action model =
case action of
NoOp ->
model
SetLength length ->
{model | length = length }
SetWidth width ->
{model | width = width }
Calculate ->
{model | gallons = (calculateGallons model.length model.width) }
calculateGallons : String -> String -> Int
calculateGallons length width =
let l =
case String.toFloat length of
Ok n -> n
Err _ -> 0.0
w =
case String.toFloat width of
Ok n -> n
Err _ -> 0.0
in
ceiling ((l * w) / 350)
view : Address Action -> Model -> Html
view address model =
div [] [
h1 [] [text "Ceiling Paint Calculator"],
p [] [text "Enter dimensions of the ceiling to determine gallons of paint needed."],
div [] [
label
[for "length"] [text "Length"],
input
[id "length",
on "input" targetValue (\value -> Signal.message address (SetLength value))
] []
],
div [] [
label
[for "width"] [text "width"],
input
[
id "width",
on "input" targetValue (\value -> Signal.message address (SetWidth value))
] []
],
div [] [
button [onClick address Calculate] [text "Calculate"],
div [] [
span [] [text (toString model.gallons)],
span [] [text " gallons of paint needed."]
]
]
]
main =
StartApp.start { model = initialModel, view = view, update = update }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment