Skip to content

Commit 77eb76d

Browse files
committed
Add initial set of examples
0 parents  commit 77eb76d

13 files changed

+560
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
elm-stuff
2+
elm.js

1/Counter.elm

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
module Counter where
2+
3+
import Html (..)
4+
import Html.Attributes (..)
5+
import Html.Events (..)
6+
import Signal
7+
8+
9+
-- MODEL
10+
11+
type alias Model = Int
12+
13+
14+
-- UPDATE
15+
16+
type Action = Increment | Decrement
17+
18+
update : Action -> Model -> Model
19+
update action model =
20+
case action of
21+
Increment -> model + 1
22+
Decrement -> model - 1
23+
24+
25+
-- VIEW
26+
27+
view : Model -> Html
28+
view model =
29+
div []
30+
[ button [ onClick (Signal.send actionChannel Decrement) ] [ text "-" ]
31+
, div [ countStyle ] [ text (toString model) ]
32+
, button [ onClick (Signal.send actionChannel Increment) ] [ text "+" ]
33+
]
34+
35+
36+
countStyle : Attribute
37+
countStyle =
38+
style
39+
[ ("font-size", "20px")
40+
, ("font-family", "monospace")
41+
, ("display", "inline-block")
42+
, ("width", "50px")
43+
, ("text-align", "center")
44+
]
45+
46+
47+
-- SIGNALS
48+
49+
main : Signal Html
50+
main =
51+
Signal.map view model
52+
53+
model : Signal Model
54+
model =
55+
Signal.foldp update 0 (Signal.subscribe actionChannel)
56+
57+
actionChannel : Signal.Channel Action
58+
actionChannel =
59+
Signal.channel Increment

1/elm-package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"version": "1.0.0",
3+
"summary": "helpful summary of your project, less than 80 characters",
4+
"repository": "https://github.com/USER/PROJECT.git",
5+
"license": "BSD3",
6+
"source-directories": [
7+
"."
8+
],
9+
"exposed-modules": [],
10+
"dependencies": {
11+
"elm-lang/core": "1.1.0 <= v < 2.0.0",
12+
"evancz/elm-html": "1.1.0 <= v < 2.0.0"
13+
}
14+
}

2/Counter.elm

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
module Counter (Model, init, Action, update, view) where
2+
3+
import Html (..)
4+
import Html.Attributes (..)
5+
import Html.Events (..)
6+
import LocalChannel (..)
7+
8+
9+
-- MODEL
10+
11+
type alias Model = Int
12+
13+
14+
init : Int -> Model
15+
init count = count
16+
17+
18+
-- UPDATE
19+
20+
type Action = Increment | Decrement
21+
22+
23+
update : Action -> Model -> Model
24+
update action model =
25+
case action of
26+
Increment -> model + 1
27+
Decrement -> model - 1
28+
29+
30+
-- VIEW
31+
32+
view : LocalChannel Action -> Model -> Html
33+
view channel model =
34+
div []
35+
[ button [ onClick (send channel Decrement) ] [ text "-" ]
36+
, div [ countStyle ] [ text (toString model) ]
37+
, button [ onClick (send channel Increment) ] [ text "+" ]
38+
]
39+
40+
41+
countStyle : Attribute
42+
countStyle =
43+
style
44+
[ ("font-size", "20px")
45+
, ("font-family", "monospace")
46+
, ("display", "inline-block")
47+
, ("width", "50px")
48+
, ("text-align", "center")
49+
]

2/CounterPair.elm

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
module CounterPair where
2+
3+
import Counter
4+
import Html (..)
5+
import Html.Attributes (..)
6+
import Html.Events (..)
7+
import LocalChannel as LC
8+
import Signal
9+
10+
11+
-- MODEL
12+
13+
type alias Model =
14+
{ topCounter : Counter.Model
15+
, bottomCounter : Counter.Model
16+
}
17+
18+
19+
init : Int -> Int -> Model
20+
init top bottom =
21+
{ topCounter = Counter.init top
22+
, bottomCounter = Counter.init bottom
23+
}
24+
25+
26+
-- UPDATE
27+
28+
type Action
29+
= Reset
30+
| Top Counter.Action
31+
| Bottom Counter.Action
32+
33+
34+
update : Action -> Model -> Model
35+
update action model =
36+
case action of
37+
Reset -> init 0 0
38+
39+
Top act ->
40+
{ model |
41+
topCounter <- Counter.update act model.topCounter
42+
}
43+
44+
Bottom act ->
45+
{ model |
46+
bottomCounter <- Counter.update act model.bottomCounter
47+
}
48+
49+
50+
-- VIEW
51+
52+
view : Model -> Html
53+
view model =
54+
div []
55+
[ Counter.view (LC.create Top actionChannel) model.topCounter
56+
, Counter.view (LC.create Bottom actionChannel) model.bottomCounter
57+
, button [ onClick (Signal.send actionChannel Reset) ] [ text "RESET" ]
58+
]
59+
60+
61+
-- SIGNALS
62+
63+
main : Signal Html
64+
main =
65+
Signal.map view model
66+
67+
68+
model : Signal Model
69+
model =
70+
Signal.foldp update (init 0 0) (Signal.subscribe actionChannel)
71+
72+
73+
actionChannel : Signal.Channel Action
74+
actionChannel =
75+
Signal.channel Reset

2/elm-package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"version": "1.0.0",
3+
"summary": "helpful summary of your project, less than 80 characters",
4+
"repository": "https://github.com/USER/PROJECT.git",
5+
"license": "BSD3",
6+
"source-directories": [
7+
"."
8+
],
9+
"exposed-modules": [],
10+
"dependencies": {
11+
"elm-lang/core": "1.1.0 <= v < 2.0.0",
12+
"evancz/elm-html": "1.1.0 <= v < 2.0.0",
13+
"evancz/local-channel": "1.0.0 <= v < 2.0.0"
14+
}
15+
}

3/Counter.elm

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
module Counter (Model, init, Action, update, view) where
2+
3+
import Html (..)
4+
import Html.Attributes (..)
5+
import Html.Events (..)
6+
import LocalChannel (..)
7+
8+
9+
-- MODEL
10+
11+
type alias Model = Int
12+
13+
14+
init : Int -> Model
15+
init count = count
16+
17+
18+
-- UPDATE
19+
20+
type Action = Increment | Decrement
21+
22+
23+
update : Action -> Model -> Model
24+
update action model =
25+
case action of
26+
Increment -> model + 1
27+
Decrement -> model - 1
28+
29+
30+
-- VIEW
31+
32+
view : LocalChannel Action -> Model -> Html
33+
view channel model =
34+
div []
35+
[ button [ onClick (send channel Decrement) ] [ text "-" ]
36+
, div [ countStyle ] [ text (toString model) ]
37+
, button [ onClick (send channel Increment) ] [ text "+" ]
38+
]
39+
40+
41+
countStyle : Attribute
42+
countStyle =
43+
style
44+
[ ("font-size", "20px")
45+
, ("font-family", "monospace")
46+
, ("display", "inline-block")
47+
, ("width", "50px")
48+
, ("text-align", "center")
49+
]

3/CounterList.elm

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
module CounterList where
2+
3+
import Counter
4+
import Html (..)
5+
import Html.Attributes (..)
6+
import Html.Events (..)
7+
import List
8+
import LocalChannel as LC
9+
import Signal
10+
11+
12+
-- MODEL
13+
14+
type alias Model =
15+
{ counters : List ( ID, Counter.Model )
16+
, nextID : ID
17+
}
18+
19+
type alias ID = Int
20+
21+
22+
init : Model
23+
init =
24+
{ counters = []
25+
, nextID = 0
26+
}
27+
28+
29+
-- UPDATE
30+
31+
type Action
32+
= Insert
33+
| Remove
34+
| Modify ID Counter.Action
35+
36+
37+
update : Action -> Model -> Model
38+
update action model =
39+
case action of
40+
Insert ->
41+
let newCounter = ( model.nextID, Counter.init 0 )
42+
newCounters = model.counters ++ [ newCounter ]
43+
in
44+
{ model |
45+
counters <- newCounters,
46+
nextID <- model.nextID + 1
47+
}
48+
49+
Remove ->
50+
{ model | counters <- List.drop 1 model.counters }
51+
52+
Modify id counterAction ->
53+
let updateCounter (counterID, counterModel) =
54+
if counterID == id
55+
then (counterID, Counter.update counterAction counterModel)
56+
else (counterID, counterModel)
57+
in
58+
{ model | counters <- List.map updateCounter model.counters }
59+
60+
61+
-- VIEW
62+
63+
view : Model -> Html
64+
view model =
65+
let counters = List.map viewCounter model.counters
66+
remove = button [ onClick (Signal.send actionChannel Remove) ] [ text "Remove" ]
67+
insert = button [ onClick (Signal.send actionChannel Insert) ] [ text "Add" ]
68+
in
69+
div [] ([remove, insert] ++ counters)
70+
71+
72+
viewCounter : (ID, Counter.Model) -> Html
73+
viewCounter (id, model) =
74+
Counter.view (LC.create (Modify id) actionChannel) model
75+
76+
77+
-- SIGNALS
78+
79+
main : Signal Html
80+
main =
81+
Signal.map view model
82+
83+
84+
model : Signal Model
85+
model =
86+
Signal.foldp update init (Signal.subscribe actionChannel)
87+
88+
89+
actionChannel : Signal.Channel Action
90+
actionChannel =
91+
Signal.channel Insert

3/elm-package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"version": "1.0.0",
3+
"summary": "helpful summary of your project, less than 80 characters",
4+
"repository": "https://github.com/USER/PROJECT.git",
5+
"license": "BSD3",
6+
"source-directories": [
7+
"."
8+
],
9+
"exposed-modules": [],
10+
"dependencies": {
11+
"elm-lang/core": "1.1.0 <= v < 2.0.0",
12+
"evancz/elm-html": "1.1.0 <= v < 2.0.0",
13+
"evancz/local-channel": "1.0.0 <= v < 2.0.0"
14+
}
15+
}

0 commit comments

Comments
 (0)