Copy the code over to http://elm-lang.org/try to try it out.
Last active
November 4, 2016 23:22
-
-
Save szabba/e51e80b77e4bf15becdfb9fccc5822dc to your computer and use it in GitHub Desktop.
This file contains 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
import Html as H exposing (Html) | |
import Html.Attributes as HA | |
import Html.Events as HE | |
import Html.App as App | |
main : Program Never | |
main = | |
App.beginnerProgram | |
{ model = init | |
, view = view | |
, update = update | |
} | |
type alias Model = | |
{ fallstart : Int | |
, initSpeed : Int | |
, speedup : Int | |
, eachDays : Int | |
} | |
init : Model | |
init = | |
{ fallstart = 1600 | |
, initSpeed = 400 | |
, speedup = 200 | |
, eachDays = 4 | |
} | |
type Msg | |
= SetFallstart Int | |
| SetInitSpeed Int | |
| SetSpeedup Int | |
| SetEachDays Int | |
update : Msg -> Model -> Model | |
update msg model = | |
case msg of | |
SetFallstart fallstart -> | |
{ model | fallstart = fallstart } | |
SetInitSpeed initSpeed -> | |
{ model | initSpeed = initSpeed } | |
SetSpeedup speedup -> | |
{ model | speedup = speedup } | |
SetEachDays eachDays -> | |
{ model | eachDays = eachDays } | |
view : Model -> Html Msg | |
view model = | |
H.div [] | |
[ paramsView model | |
, reachWords 40000 model | |
, reachWords 50000 model | |
, afterDays 30 model | |
] | |
paramsView : Model -> Html Msg | |
paramsView model = | |
H.p [] | |
[ H.text "With " | |
, input | |
{ value = model.fallstart | |
, shift = 100 | |
, targetToMsg = SetFallstart | |
} | |
, H.text " words written beforehand, an initial speed of " | |
, input | |
{ value = model.initSpeed | |
, shift = 100 | |
, targetToMsg = SetInitSpeed | |
} | |
, H.text " and speeding up by " | |
, input | |
{ value = model.speedup | |
, shift = 100 | |
, targetToMsg = SetSpeedup | |
} | |
, H.text " words every " | |
, input | |
{ value = model.eachDays | |
, shift = 1 | |
, targetToMsg = SetEachDays | |
} | |
, H.text " days:" | |
] | |
reachWords : Int -> Model -> Html msg | |
reachWords target model = | |
H.p [] | |
[ H.text | |
("You'll reach " | |
++ toString target | |
++ " words after " | |
++ toString (daysToReach target model) | |
++ " days." | |
) | |
] | |
afterDays : Int -> Model -> Html msg | |
afterDays days model = | |
H.p [] | |
[ H.text | |
("After " | |
++ toString days | |
++ " days, you'll have " | |
++ toString (wordsAfter days model) | |
++ " words." | |
) | |
] | |
daysToReach : Int -> Model -> Int | |
daysToReach target model = | |
let | |
loop day = | |
if wordsAfter day model >= target then | |
day | |
else | |
loop (day + 1) | |
in | |
loop 0 | |
wordsAfter : Int -> Model -> Int | |
wordsAfter days model = | |
let | |
speedups = days // model.eachDays | |
in | |
model.fallstart | |
+ days * model.initSpeed | |
+ speedups ^ 2 * model.speedup // 2 | |
input : { value : Int, shift : Int, targetToMsg : Int -> Msg } -> Html Msg | |
input { value, shift, targetToMsg } = | |
let | |
up = abs shift | |
down = -up | |
in | |
H.span [] | |
[ H.text (toString value) | |
, button (value > 0) (toString down) (targetToMsg (value + down)) | |
, button True ("+" ++ toString up) (targetToMsg (value + up)) | |
] | |
button : Bool -> String -> Msg -> Html Msg | |
button active label msg = | |
H.button | |
(if active then [ activeStyle, HE.onClick msg ] else [ inactiveStyle ]) | |
[ H.text label ] | |
activeStyle = | |
HA.style | |
[ ( "background", "#005500" ), ( "color", "#ffffff" ) ] | |
inactiveStyle = | |
HA.style | |
[ ( "background", "#555555" ), ( "color", "#ffffff" ) ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment