Skip to content

Instantly share code, notes, and snippets.

@chrisbuttery
Created May 18, 2016 06:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisbuttery/c7712077272547f247c96f7f5a28da87 to your computer and use it in GitHub Desktop.
Save chrisbuttery/c7712077272547f247c96f7f5a28da87 to your computer and use it in GitHub Desktop.
Elm 0.17. A simple filtering digits from Keyboard.presses example
"version": "1.0.0",
"summary": "let people do a cool thing in a fun way",
"repository": "https://github.com/user/project.git",
"license": "BSD3",
"source-directories": [
"."
],
"exposed-modules": [],
"dependencies": {
"elm-lang/core": "4.0.0 <= v < 5.0.0",
"elm-lang/html": "1.0.0 <= v < 2.0.0",
"elm-lang/keyboard": "1.0.0 <= v < 2.0.0",
},
"elm-version": "0.17.0 <= v < 0.18.0"
}
import Html exposing (Html, text, div)
import Html.App as Html
import Keyboard exposing (..)
import Char exposing (..)
main =
Html.program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
-- MODEL
type alias Model = Char
init : (Model, Cmd Msg)
init =
(' ', Cmd.none)
-- UPDATE
type Msg
= Presses Char
update: Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
Presses code ->
if Char.isDigit code == True
then
(code, Cmd.none)
else
(model, Cmd.none)
-- SUBSCRIPTIONS
subscriptions: Model -> Sub Msg
subscriptions model =
Keyboard.presses (\code -> Presses (fromCode code))
-- VIEW
view: Model -> Html Msg
view model =
let
str =
if model == ' '
then "Press a key"
else "You pressed: " ++ (toString model)
in
Html.text str
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment