From 0df7ba1e4bdb288a1681c988e158eb6ef394980e Mon Sep 17 00:00:00 2001 From: Ross Tuck Date: Thu, 2 Mar 2017 19:03:38 +0100 Subject: [PATCH 1/2] Add a teensy elm raffler --- rosstuck-elm/README.md | 1 + rosstuck-elm/elm-package.json | 16 ++++++++++ rosstuck-elm/test.elm | 60 +++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 rosstuck-elm/README.md create mode 100644 rosstuck-elm/elm-package.json create mode 100644 rosstuck-elm/test.elm diff --git a/rosstuck-elm/README.md b/rosstuck-elm/README.md new file mode 100644 index 0000000..cb7a00a --- /dev/null +++ b/rosstuck-elm/README.md @@ -0,0 +1 @@ +Install elm. Run "elm-reactor" and go to localhost:8000 diff --git a/rosstuck-elm/elm-package.json b/rosstuck-elm/elm-package.json new file mode 100644 index 0000000..6405038 --- /dev/null +++ b/rosstuck-elm/elm-package.json @@ -0,0 +1,16 @@ +{ + "version": "1.0.0", + "summary": "DomCode sample raffler", + "repository": "https://github.com/domcode/raffler.git", + "license": "MIT", + "source-directories": [ + "." + ], + "exposed-modules": [], + "dependencies": { + "elm-community/random-extra": "2.0.0 <= v < 3.0.0", + "elm-lang/core": "5.1.1 <= v < 6.0.0", + "elm-lang/html": "2.0.0 <= v < 3.0.0" + }, + "elm-version": "0.18.0 <= v < 0.19.0" +} diff --git a/rosstuck-elm/test.elm b/rosstuck-elm/test.elm new file mode 100644 index 0000000..88b5cac --- /dev/null +++ b/rosstuck-elm/test.elm @@ -0,0 +1,60 @@ +import Html exposing (h2, textarea, br, button, ul, li, text, div, Html) +import Html.Events exposing (onClick, onInput) +import List exposing (map) +import String exposing (split, trim) +import Random +import Random.Extra exposing (sample) + +main = + Html.program + { init = init + , view = view + , update = update + , subscriptions = \_ -> Sub.none + } + +type alias Name = String +type alias Names = List Name +type alias Model = + { contestants : Names + , winner : Maybe Name + } + +init : (Model, Cmd msg) +init = (Model [] Nothing, Cmd.none) + +type Msg = GatherNames Names | StartRaffle | PickWinner (Maybe Name) + +update : Msg -> Model -> (Model, Cmd Msg) +update msg model = + case msg of + GatherNames allNames -> + ({model | contestants = allNames, winner = Nothing}, Cmd.none) + StartRaffle -> + (model, Random.generate PickWinner (sample model.contestants)) + PickWinner nameOfWinner -> + ({model | winner = nameOfWinner}, Cmd.none) + +view : Model -> Html Msg +view model = + div [] + [ h2 [] [text "DomCode Elm Raffler"] + , textarea [onInput splitNames] [] + , br [] [] + , button [onClick StartRaffle] [text "And the winner is..."] + , br [] [] + , showWinner model + ] + +splitNames : String -> Msg +splitNames input = + if (trim input == "") then + GatherNames [] + else + GatherNames (split "\n" (trim input)) + +showWinner : Model -> Html Msg +showWinner model = + case model.winner of + Just winner -> text winner + Nothing -> text "No Winner yet" From 4406c9fa61376823c4e6ab2f8b1a468b194fa56d Mon Sep 17 00:00:00 2001 From: Ross Tuck Date: Thu, 2 Mar 2017 19:08:20 +0100 Subject: [PATCH 2/2] Update elm-package.json --- rosstuck-elm/elm-package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rosstuck-elm/elm-package.json b/rosstuck-elm/elm-package.json index 6405038..872b990 100644 --- a/rosstuck-elm/elm-package.json +++ b/rosstuck-elm/elm-package.json @@ -1,7 +1,7 @@ { "version": "1.0.0", "summary": "DomCode sample raffler", - "repository": "https://github.com/domcode/raffler.git", + "repository": "https://github.com/domcode/rafflers.git", "license": "MIT", "source-directories": [ "."