Skip to content

Instantly share code, notes, and snippets.

@serradura
Last active June 22, 2017 20:55
Show Gist options
  • Save serradura/1ed09ce6c4ae93c30ee357db4bb79683 to your computer and use it in GitHub Desktop.
Save serradura/1ed09ce6c4ae93c30ee357db4bb79683 to your computer and use it in GitHub Desktop.
My firsts (basic) Elm-Lang applications. | http://elm-lang.org/try
module Main exposing (..)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
-- update
type Msg
= Incr
| Decr
update : Msg -> Int -> Int
update msg model =
case msg of
Incr ->
model + 1
Decr ->
model - 1
view : Int -> Html Msg
view model =
div []
[ button
[ type_ "button", onClick Incr ]
[ text "+" ]
, h1 [] [ text (toString model) ]
, button
[ type_ "button", onClick Decr ]
[ text "-" ]
]
main : Program Never Int Msg
main =
Html.beginnerProgram
{ model = 0
, view = view
, update = update
}
module Main exposing (..)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
-- model
type alias Model =
{ current : String
, tasks : List String
}
initModel : Model
initModel =
{ current = "", tasks = [] }
-- update
type Msg
= Add
| Input String
isAPresentString : String -> Bool
isAPresentString val =
((String.trim >> String.length) val) > 0
update : Msg -> Model -> Model
update msg model =
case msg of
Add ->
if isAPresentString model.current then
{ model
| tasks = model.tasks ++ [ model.current ]
, current = ""
}
else
model
Input val ->
{ model
| current = val
}
-- view
view : Model -> Html Msg
view model =
div []
[ h1 [] [ text ("To-dos: " ++ (toString (List.length model.tasks))) ]
, input [ type_ "text", onInput Input, value model.current ] []
, button [ type_ "button", onClick Add ] [ text "Add" ]
, ul [] (List.map (\entry -> li [] [ text entry ]) model.tasks)
]
main : Program Never Model Msg
main =
Html.beginnerProgram
{ model = initModel
, view = view
, update = update
}
module Main exposing (..)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
-- model
type alias Model =
{ length : Int
, error : Maybe String
}
initModel : Model
initModel =
{ length = 0
, error = Nothing
}
-- update
type Msg
= Count String
isABlankString : String -> Bool
isABlankString val =
((String.trim >> String.length) val) == 0
wordsCount : String -> Int
wordsCount val =
if isABlankString val then
0
else
((String.words >> List.length) val)
update : Msg -> Model -> Model
update msg model =
case msg of
Count val ->
{ model
| length = wordsCount val
, error = Nothing
}
view : Model -> Html Msg
view model =
div []
[ h1 [] [ text ("Number of words: " ++ (toString model.length)) ]
, input [ type_ "text", onInput Count ] []
]
main : Program Never Model Msg
main =
Html.beginnerProgram
{ model = initModel
, view = view
, update = update
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment