-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimmutability.elm
82 lines (64 loc) · 1.55 KB
/
immutability.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (onInput)
main =
Html.beginnerProgram
{ model = model
, view = view
, update = update
}
-- MODEL
type alias Metric =
{ value : Int
, year : Int
}
type alias Model =
{ metrics : List Metric }
model : Model
model =
Model
[ Metric 210394680 2010
, Metric 740276402 2011
, Metric 986737865 2012
, Metric 872356504 2013 ]
-- UPDATE
type Msg
= Value Int
update : Msg -> Model -> Model
update msg model =
model
formatMetric : Metric -> Metric
formatMetric metric =
let
oneThousand = 1000
oneMillion = oneThousand * oneThousand
oneBillion = oneMillion * oneThousand
in
if metric.value > oneBillion then
{ metric | value = (metric.value // oneBillion)}
else if metric.value > oneMillion then
{ metric | value = (metric.value // oneMillion)}
else
metric
formatMetrics : Model -> Model
formatMetrics model =
{ model | metrics = List.map formatMetric model.metrics }
-- VIEW
getTableRow : Metric -> Html Msg
getTableRow metric =
tr []
[ td [] [text (toString metric.value)]
, td [] [text (toString metric.year)]]
getTable : Model -> String -> Html Msg
getTable model cap =
table []
(List.append
[ caption [] [text cap, tr [] [td [] [text "Value"], td [] [text "Year"]]] ]
(List.map getTableRow model.metrics))
view : Model -> Html Msg
view model =
div []
[ getTable model "Original Metrics"
, br [] []
, getTable (formatMetrics model) "Formatted Metrics"
]