Skip to content

Commit 874dc90

Browse files
committed
Merge pull request #2 from rofrol/master
More cleaning and simplifying
2 parents b302b2e + 821e0f6 commit 874dc90

File tree

5 files changed

+158
-114
lines changed

5 files changed

+158
-114
lines changed

.editorconfig

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# http://editorconfig.org
2+
root = true
3+
4+
[*]
5+
indent_style = space
6+
indent_size = 2
7+
end_of_line = lf
8+
charset = utf-8
9+
trim_trailing_whitespace = true
10+
insert_final_newline = true
11+
12+
[*.elm]
13+
indent_size = 4

src/Counter.elm

+36-25
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,59 @@
1-
module Counter exposing (Model, update, init, view, Msg(SetNum), getValue)
1+
module Counter exposing (Model, update, init, view, Msg(SetNum), getNum)
22

33
import Html exposing (..)
44
import Html.Attributes exposing (..)
55
import Html.Events exposing (onClick)
66

7+
78
(=>) : a -> b -> ( a, b )
8-
(=>) = (,)
9+
(=>) =
10+
(,)
11+
912

1013
type alias Model =
11-
{ num : Int
12-
, btnClicks : Int
13-
}
14+
{ num : Int
15+
, btnClicks : Int
16+
}
17+
1418

1519
init : Int -> Model
1620
init num =
17-
Model num 0
21+
Model num 0
22+
1823

1924
view : String -> Model -> Html Msg
2025
view color model =
21-
div [ style ["display" => "inline-block", "margin-right" => "1rem"] ]
22-
[ button [ onClick Increment ] [ text "+" ]
23-
, div [ style ["color" => color]] [ text <| toString model.num ]
24-
, button [ onClick Decrement ] [ text "-" ]
25-
, div [ ] [ text <| "btn click: " ++ (toString model.btnClicks) ]
26-
]
26+
div [ style [ "display" => "inline-block", "margin-right" => "1rem" ] ]
27+
[ button [ onClick Increment ] [ text "+" ]
28+
, div [ style [ "color" => color ] ] [ text <| toString model.num ]
29+
, button [ onClick Decrement ] [ text "-" ]
30+
, div [] [ text <| "btn click: " ++ (toString model.btnClicks) ]
31+
]
32+
2733

2834
type Msg
29-
= Increment
30-
| Decrement
31-
| SetNum Int
35+
= Increment
36+
| Decrement
37+
| SetNum Int
38+
3239

3340
update : Msg -> Model -> Model
3441
update msg model =
35-
case msg of
36-
Increment ->
37-
{ model | num = model.num + 1, btnClicks = model.btnClicks + 1 }
42+
case msg of
43+
Increment ->
44+
Model (model.num + 1) (model.btnClicks + 1)
45+
46+
Decrement ->
47+
Model (model.num - 1) (model.btnClicks + 1)
48+
49+
SetNum num ->
50+
{ model | num = num }
3851

39-
Decrement ->
40-
{ model | num = model.num - 1, btnClicks = model.btnClicks + 1 }
4152

42-
SetNum num ->
43-
{model | num = num}
4453

4554
------- INTEFACE HELPERS
46-
getValue : Model -> Int
47-
getValue model =
48-
model.num
55+
56+
57+
getNum : Model -> Int
58+
getNum model =
59+
model.num

src/Main.elm

+52-34
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,71 @@
1+
module Main exposing (..)
2+
13
import Pair
24
import Totals
3-
45
import Html exposing (..)
56
import Html.App as App exposing (beginnerProgram)
67

8+
79
main : Program Never
810
main =
9-
beginnerProgram { model = init, view = view, update = update }
11+
beginnerProgram { model = init, view = view, update = update }
12+
1013

1114
type alias Model =
12-
{ pair1 : Pair.Model
13-
, pair2 : Pair.Model
14-
, totals: Totals.Model
15-
}
15+
{ pair1 : Pair.Model
16+
, pair2 : Pair.Model
17+
, totals : Totals.Model
18+
}
19+
1620

1721
init : Model
18-
init = Model Pair.init Pair.init Totals.init
22+
init =
23+
Model Pair.init Pair.init Totals.init
24+
1925

2026
view : Model -> Html Msg
2127
view model =
22-
div []
23-
[ App.map (always NoOp) (Totals.view model.totals)
24-
, App.map Pair1 (Pair.view model.pair1)
25-
, App.map Pair2 (Pair.view model.pair2)
26-
]
28+
div []
29+
[ App.map (always NoOp) (Totals.view model.totals)
30+
, App.map Pair1 (Pair.view model.pair1)
31+
, App.map Pair2 (Pair.view model.pair2)
32+
]
33+
2734

2835
type Msg
29-
= NoOp
30-
| Pair1 Pair.Msg
31-
| Pair2 Pair.Msg
36+
= NoOp
37+
| Pair1 Pair.Msg
38+
| Pair2 Pair.Msg
39+
3240

3341
update : Msg -> Model -> Model
3442
update msg model =
35-
case msg of
36-
NoOp ->
37-
model
38-
39-
Pair1 sub ->
40-
let
41-
(pair1, redVal, greenVal) = Pair.update sub model.pair1
42-
totals = Totals.update (Totals.UpdateRed redVal) model.totals
43-
pair2 = Pair.manualUpdate Pair.Red redVal model.pair2
44-
in
45-
{ model | pair1 = pair1, totals = totals, pair2 = pair2 }
46-
47-
Pair2 sub ->
48-
let
49-
(pair2, redVal, greenVal) = Pair.update sub model.pair2
50-
totals = Totals.update (Totals.UpdateRed redVal) model.totals
51-
pair1 = Pair.manualUpdate Pair.Red redVal model.pair1
52-
in
53-
{ model | pair1 = pair1, totals = totals, pair2 = pair2 }
43+
case msg of
44+
NoOp ->
45+
model
46+
47+
Pair1 subMsg ->
48+
let
49+
pair1 =
50+
Pair.update subMsg model.pair1
51+
52+
totals =
53+
Totals.update (Totals.UpdateRed <| Pair.getRedNum pair1) model.totals
54+
55+
pair2 =
56+
Pair.update (Pair.UpdateRed <| Pair.getRedNum pair1) model.pair2
57+
in
58+
Model pair1 pair2 totals
59+
60+
Pair2 subMsg ->
61+
let
62+
pair2 =
63+
Pair.update subMsg model.pair2
64+
65+
totals =
66+
Totals.update (Totals.UpdateRed <| Pair.getRedNum pair2) model.totals
67+
68+
pair1 =
69+
Pair.update (Pair.UpdateRed <| Pair.getRedNum pair2) model.pair1
70+
in
71+
Model pair1 pair2 totals

src/Pair.elm

+40-46
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,60 @@
1-
module Pair exposing (Model, init, view, Msg, update, ManualUpdateMsg(Red), manualUpdate)
1+
module Pair exposing (Model, init, view, Msg(UpdateRed), update, getRedNum)
22

33
import Counter
4-
54
import Html exposing (..)
65
import Html.Attributes exposing (..)
76
import Html.App as App
87

8+
99
(=>) : a -> b -> ( a, b )
10-
(=>) = (,)
10+
(=>) =
11+
(,)
12+
1113

1214
type alias Model =
13-
{ greenCounter : Counter.Model
14-
, redCounter : Counter.Model
15-
, totalClickCount : Int
16-
}
15+
{ greenCounter : Counter.Model
16+
, redCounter : Counter.Model
17+
, totalClickCount : Int
18+
}
19+
1720

1821
init : Model
1922
init =
20-
Model (Counter.init 0) (Counter.init 0) 0
23+
Model (Counter.init 0) (Counter.init 0) 0
24+
2125

2226
view : Model -> Html Msg
2327
view model =
24-
div [ style ["background-color" => "lightgray", "margin-bottom" => "1rem"] ]
25-
[ div [][text <| "Total click count " ++ (toString model.totalClickCount)]
26-
, App.map PairGreen (Counter.view "green" model.greenCounter)
27-
, App.map PairRed (Counter.view "red" model.redCounter)
28-
]
28+
div [ style [ "background-color" => "lightgray", "margin-bottom" => "1rem" ] ]
29+
[ div [] [ text <| "Total click count " ++ (toString model.totalClickCount) ]
30+
, App.map PairGreen (Counter.view "green" model.greenCounter)
31+
, App.map PairRed (Counter.view "red" model.redCounter)
32+
]
33+
2934

3035
type Msg
31-
= PairRed Counter.Msg
32-
| PairGreen Counter.Msg
36+
= PairRed Counter.Msg
37+
| PairGreen Counter.Msg
38+
| UpdateRed Int
3339

34-
type alias RedVal = Int
35-
type alias GreenVal = Int
3640

37-
update : Msg -> Model -> (Model, RedVal, GreenVal)
41+
update : Msg -> Model -> Model
3842
update msg model =
39-
case msg of
40-
PairGreen sub ->
41-
let
42-
greenCounter = Counter.update sub model.greenCounter
43-
model' = { model | greenCounter = greenCounter, totalClickCount = model.totalClickCount + 1 }
44-
in
45-
(model', Counter.getValue model'.redCounter, Counter.getValue model'.greenCounter)
46-
47-
PairRed sub ->
48-
let
49-
redCounter = Counter.update sub model.redCounter
50-
model' = { model | redCounter = redCounter, totalClickCount = model.totalClickCount + 1 }
51-
in
52-
(model', Counter.getValue model'.redCounter, Counter.getValue model'.greenCounter)
53-
54-
----- Interface helper
55-
56-
type ManualUpdateMsg
57-
= Red
58-
59-
manualUpdate: ManualUpdateMsg -> Int -> Model -> Model
60-
manualUpdate msg value model =
61-
case msg of
62-
Red ->
63-
let
64-
redCounter = Counter.update (Counter.SetNum value) model.redCounter
65-
in
66-
{ model | redCounter = redCounter }
43+
case msg of
44+
PairGreen subMsg ->
45+
{ model | greenCounter = Counter.update subMsg model.greenCounter, totalClickCount = model.totalClickCount + 1 }
46+
47+
PairRed subMsg ->
48+
{ model | redCounter = Counter.update subMsg model.redCounter, totalClickCount = model.totalClickCount + 1 }
49+
50+
UpdateRed value ->
51+
{ model | redCounter = Counter.update (Counter.SetNum value) model.redCounter }
52+
53+
54+
55+
------- INTEFACE HELPERS
56+
57+
58+
getRedNum : Model -> Int
59+
getRedNum model =
60+
Counter.getNum model.redCounter

src/Totals.elm

+17-9
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,33 @@ module Totals exposing (Model, init, view, Msg(UpdateRed), update)
33
import Html exposing (..)
44
import Html.Attributes exposing (..)
55

6+
67
(=>) : a -> b -> ( a, b )
7-
(=>) = (,)
8+
(=>) =
9+
(,)
10+
811

912
type alias Model =
10-
{ redNum : Int
11-
}
13+
{ redNum : Int
14+
}
15+
1216

1317
init : Model
1418
init =
15-
Model 0
19+
Model 0
20+
1621

1722
view : Model -> Html Msg
1823
view model =
19-
div [style ["color" => "red"]][text <| "Red val: " ++ (toString model.redNum)]
24+
div [ style [ "color" => "red" ] ] [ text <| "Red val: " ++ (toString model.redNum) ]
25+
26+
27+
type Msg
28+
= UpdateRed Int
2029

21-
type Msg = UpdateRed Int
2230

2331
update : Msg -> Model -> Model
2432
update msg model =
25-
case msg of
26-
UpdateRed redNum ->
27-
{ model | redNum = redNum }
33+
case msg of
34+
UpdateRed redNum ->
35+
Model redNum

0 commit comments

Comments
 (0)