Skip to content

Instantly share code, notes, and snippets.

@napcs
Created December 16, 2015 04:38
Show Gist options
  • Save napcs/dded00197f3753f0556e to your computer and use it in GitHub Desktop.
Save napcs/dded00197f3753f0556e to your computer and use it in GitHub Desktop.
Exercises For Programmers #13 in Elm.
import Html exposing (Html, text, div, p, button, input, label, span)
import Html.Attributes exposing (..)
import Html.Events exposing (onClick, on, targetValue)
import Signal exposing (Address)
import StartApp.Simple as StartApp
import String exposing (..)
type alias Model =
{
principle: String,
rate: String,
years: String,
periods: String,
newAmount: Float
}
type Action
= NoOp
| SetPrinciple String
| SetPeriods String
| SetYears String
| SetRate String
| Calculate
model : Model
model =
{
principle = "1500.0",
rate = "4.3",
years = "6",
periods = "4",
newAmount = 0.0
}
main =
StartApp.start { model = model, view = view, update = update }
view : Address Action -> Model -> Html
view address model =
div [] [
calculatorForm address model,
button [onClick address Calculate ] [text "Calculate"],
outputArea model
]
outputArea: Model -> Html
outputArea model =
p [] [
span [] [text "Amount: "],
span [] [text (formatCurrency model.newAmount) ]
]
calculatorForm: Address Action -> Model -> Html
calculatorForm address model =
div [] [
div [] [
label [for "principle"] [text "Principle"],
input [
id "principle",
on "input" targetValue (\value -> Signal.message address (SetPrinciple value)),
value model.principle ] []
],
div [] [
label [for "years"] [text "Years"],
input [
id "principle",
on "input" targetValue (\value -> Signal.message address (SetYears value)),
value model.years ] []
],
div [] [
label [for "periods"] [text "Periods"],
input [
id "periods",
on "input" targetValue (\value -> Signal.message address (SetPeriods value)),
value model.periods ] []
],
div [] [
label [for "rate"] [text "Rate"],
input [
id "rate",
on "input" targetValue (\value -> Signal.message address (SetRate value)),
value model.rate ] []
]
]
update: Action -> Model -> Model
update action model =
case action of
NoOp ->
model
SetPrinciple p ->
{model | principle = p}
SetRate r ->
{model | rate = r}
SetYears y ->
{model | years = y}
SetPeriods p ->
{model | periods = p}
Calculate ->
calculateNewAmount model
calculateNewAmount: Model -> Model
calculateNewAmount model =
let
rate = convertToFloat model.rate / 100
years = convertToFloat model.years
principle = convertToFloat model.principle
periods = convertToFloat model.periods
in
{model | newAmount = (compoundInterest principle rate periods years) }
compoundInterest: Float -> Float -> Float -> Float -> Float
compoundInterest principle rate periods years =
(principle * (1 + (rate / periods ) ) ^ (years * periods) )
formatCurrency: Float -> String
formatCurrency value =
let result =
value * 100
|> round
|> Basics.toFloat
in
"$" ++ toString (result / 100.0)
convertToFloat: String -> Float
convertToFloat string =
case String.toFloat string of
Ok n -> n
Err _ -> 0.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment