Permalink
Browse files

Add initial set of examples

  • Loading branch information...
process-bot committed Jan 11, 2015
0 parents commit 77eb76dd64c2d21b31f53e014ea598afd381ebcf
Showing with 560 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +59 −0 1/Counter.elm
  3. +14 −0 1/elm-package.json
  4. +49 −0 2/Counter.elm
  5. +75 −0 2/CounterPair.elm
  6. +15 −0 2/elm-package.json
  7. +49 −0 3/Counter.elm
  8. +91 −0 3/CounterList.elm
  9. +15 −0 3/elm-package.json
  10. +56 −0 4/Counter.elm
  11. +93 −0 4/CounterList.elm
  12. +15 −0 4/elm-package.json
  13. +27 −0 LICENSE
@@ -0,0 +1,2 @@
+elm-stuff
+elm.js
@@ -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
@@ -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"
+ }
+}
@@ -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")
+ ]
@@ -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
@@ -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,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")
+ ]
@@ -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
@@ -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"
+ }
+}
Oops, something went wrong.

0 comments on commit 77eb76d

Please sign in to comment.