Skip to content

Commit

Permalink
Add initial set of examples
Browse files Browse the repository at this point in the history
  • Loading branch information
process-bot committed Jan 11, 2015
0 parents commit 77eb76d
Show file tree
Hide file tree
Showing 13 changed files with 560 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
elm-stuff
elm.js
59 changes: 59 additions & 0 deletions 1/Counter.elm
@@ -0,0 +1,59 @@
module Counter where

import Html (..)
import Html.Attributes (..)
import Html.Events (..)
import Signal


-- MODEL

type alias Model = Int


-- UPDATE

type Action = Increment | Decrement

update : Action -> Model -> Model
update action model =
case action of
Increment -> model + 1
Decrement -> model - 1


-- VIEW

view : Model -> Html
view model =
div []
[ button [ onClick (Signal.send actionChannel Decrement) ] [ text "-" ]
, div [ countStyle ] [ text (toString model) ]
, button [ onClick (Signal.send actionChannel Increment) ] [ text "+" ]
]


countStyle : Attribute
countStyle =
style
[ ("font-size", "20px")
, ("font-family", "monospace")
, ("display", "inline-block")
, ("width", "50px")
, ("text-align", "center")
]


-- SIGNALS

main : Signal Html
main =
Signal.map view model

model : Signal Model
model =
Signal.foldp update 0 (Signal.subscribe actionChannel)

actionChannel : Signal.Channel Action
actionChannel =
Signal.channel Increment
14 changes: 14 additions & 0 deletions 1/elm-package.json
@@ -0,0 +1,14 @@
{
"version": "1.0.0",
"summary": "helpful summary of your project, less than 80 characters",
"repository": "https://github.com/USER/PROJECT.git",
"license": "BSD3",
"source-directories": [
"."
],
"exposed-modules": [],
"dependencies": {
"elm-lang/core": "1.1.0 <= v < 2.0.0",
"evancz/elm-html": "1.1.0 <= v < 2.0.0"
}
}
49 changes: 49 additions & 0 deletions 2/Counter.elm
@@ -0,0 +1,49 @@
module Counter (Model, init, Action, update, view) where

import Html (..)
import Html.Attributes (..)
import Html.Events (..)
import LocalChannel (..)


-- MODEL

type alias Model = Int


init : Int -> Model
init count = count


-- UPDATE

type Action = Increment | Decrement


update : Action -> Model -> Model
update action model =
case action of
Increment -> model + 1
Decrement -> model - 1


-- VIEW

view : LocalChannel Action -> Model -> Html
view channel model =
div []
[ button [ onClick (send channel Decrement) ] [ text "-" ]
, div [ countStyle ] [ text (toString model) ]
, button [ onClick (send channel Increment) ] [ text "+" ]
]


countStyle : Attribute
countStyle =
style
[ ("font-size", "20px")
, ("font-family", "monospace")
, ("display", "inline-block")
, ("width", "50px")
, ("text-align", "center")
]
75 changes: 75 additions & 0 deletions 2/CounterPair.elm
@@ -0,0 +1,75 @@
module CounterPair where

import Counter
import Html (..)
import Html.Attributes (..)
import Html.Events (..)
import LocalChannel as LC
import Signal


-- MODEL

type alias Model =
{ topCounter : Counter.Model
, bottomCounter : Counter.Model
}


init : Int -> Int -> Model
init top bottom =
{ topCounter = Counter.init top
, bottomCounter = Counter.init bottom
}


-- UPDATE

type Action
= Reset
| Top Counter.Action
| Bottom Counter.Action


update : Action -> Model -> Model
update action model =
case action of
Reset -> init 0 0

Top act ->
{ model |
topCounter <- Counter.update act model.topCounter
}

Bottom act ->
{ model |
bottomCounter <- Counter.update act model.bottomCounter
}


-- VIEW

view : Model -> Html
view model =
div []
[ Counter.view (LC.create Top actionChannel) model.topCounter
, Counter.view (LC.create Bottom actionChannel) model.bottomCounter
, button [ onClick (Signal.send actionChannel Reset) ] [ text "RESET" ]
]


-- SIGNALS

main : Signal Html
main =
Signal.map view model


model : Signal Model
model =
Signal.foldp update (init 0 0) (Signal.subscribe actionChannel)


actionChannel : Signal.Channel Action
actionChannel =
Signal.channel Reset
15 changes: 15 additions & 0 deletions 2/elm-package.json
@@ -0,0 +1,15 @@
{
"version": "1.0.0",
"summary": "helpful summary of your project, less than 80 characters",
"repository": "https://github.com/USER/PROJECT.git",
"license": "BSD3",
"source-directories": [
"."
],
"exposed-modules": [],
"dependencies": {
"elm-lang/core": "1.1.0 <= v < 2.0.0",
"evancz/elm-html": "1.1.0 <= v < 2.0.0",
"evancz/local-channel": "1.0.0 <= v < 2.0.0"
}
}
49 changes: 49 additions & 0 deletions 3/Counter.elm
@@ -0,0 +1,49 @@
module Counter (Model, init, Action, update, view) where

import Html (..)
import Html.Attributes (..)
import Html.Events (..)
import LocalChannel (..)


-- MODEL

type alias Model = Int


init : Int -> Model
init count = count


-- UPDATE

type Action = Increment | Decrement


update : Action -> Model -> Model
update action model =
case action of
Increment -> model + 1
Decrement -> model - 1


-- VIEW

view : LocalChannel Action -> Model -> Html
view channel model =
div []
[ button [ onClick (send channel Decrement) ] [ text "-" ]
, div [ countStyle ] [ text (toString model) ]
, button [ onClick (send channel Increment) ] [ text "+" ]
]


countStyle : Attribute
countStyle =
style
[ ("font-size", "20px")
, ("font-family", "monospace")
, ("display", "inline-block")
, ("width", "50px")
, ("text-align", "center")
]
91 changes: 91 additions & 0 deletions 3/CounterList.elm
@@ -0,0 +1,91 @@
module CounterList where

import Counter
import Html (..)
import Html.Attributes (..)
import Html.Events (..)
import List
import LocalChannel as LC
import Signal


-- MODEL

type alias Model =
{ counters : List ( ID, Counter.Model )
, nextID : ID
}

type alias ID = Int


init : Model
init =
{ counters = []
, nextID = 0
}


-- UPDATE

type Action
= Insert
| Remove
| Modify ID Counter.Action


update : Action -> Model -> Model
update action model =
case action of
Insert ->
let newCounter = ( model.nextID, Counter.init 0 )
newCounters = model.counters ++ [ newCounter ]
in
{ model |
counters <- newCounters,
nextID <- model.nextID + 1
}

Remove ->
{ model | counters <- List.drop 1 model.counters }

Modify id counterAction ->
let updateCounter (counterID, counterModel) =
if counterID == id
then (counterID, Counter.update counterAction counterModel)
else (counterID, counterModel)
in
{ model | counters <- List.map updateCounter model.counters }


-- VIEW

view : Model -> Html
view model =
let counters = List.map viewCounter model.counters
remove = button [ onClick (Signal.send actionChannel Remove) ] [ text "Remove" ]
insert = button [ onClick (Signal.send actionChannel Insert) ] [ text "Add" ]
in
div [] ([remove, insert] ++ counters)


viewCounter : (ID, Counter.Model) -> Html
viewCounter (id, model) =
Counter.view (LC.create (Modify id) actionChannel) model


-- SIGNALS

main : Signal Html
main =
Signal.map view model


model : Signal Model
model =
Signal.foldp update init (Signal.subscribe actionChannel)


actionChannel : Signal.Channel Action
actionChannel =
Signal.channel Insert
15 changes: 15 additions & 0 deletions 3/elm-package.json
@@ -0,0 +1,15 @@
{
"version": "1.0.0",
"summary": "helpful summary of your project, less than 80 characters",
"repository": "https://github.com/USER/PROJECT.git",
"license": "BSD3",
"source-directories": [
"."
],
"exposed-modules": [],
"dependencies": {
"elm-lang/core": "1.1.0 <= v < 2.0.0",
"evancz/elm-html": "1.1.0 <= v < 2.0.0",
"evancz/local-channel": "1.0.0 <= v < 2.0.0"
}
}

0 comments on commit 77eb76d

Please sign in to comment.