Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a teensy elm raffler #138

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions rosstuck-elm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Install elm. Run "elm-reactor" and go to localhost:8000
16 changes: 16 additions & 0 deletions rosstuck-elm/elm-package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"version": "1.0.0",
"summary": "DomCode sample raffler",
"repository": "https://github.com/domcode/rafflers.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"
}
60 changes: 60 additions & 0 deletions rosstuck-elm/test.elm
Original file line number Diff line number Diff line change
@@ -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"