Last active
July 28, 2016 20:37
-
-
Save alech/1cdcd3b5d2a5312c48d11aab004de14d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ elm repl | |
---- elm-repl 0.17.1 ----------------------------------------------------------- | |
:help for help, :exit to exit, more at <https://github.com/elm-lang/elm-repl> | |
-------------------------------------------------------------------------------- | |
> import Main | |
> import Combine | |
> Combine.parse Main.comment "foo" | |
TypeError: Cannot read property 'ctor' of undefined |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Combine exposing (..) | |
import Combine.Char exposing (..) | |
import String | |
import Char | |
flattenStringList : List String -> String | |
flattenStringList = | |
List.foldr (++) "" | |
comment : Parser String | |
comment = | |
map flattenStringList <| many cContent | |
foo : Parser String | |
foo = | |
string "foo" | |
cContent : Parser String | |
cContent = | |
(map String.fromChar cText) `or` comment | |
cText : Parser Char | |
cText = | |
char 'a' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Combine exposing (..) | |
import Combine.Char exposing (..) | |
import String | |
import Char | |
import Html exposing (..) | |
import Html.App as Html | |
import Html.Events exposing (onClick, onInput) | |
import Html.Attributes exposing (..) | |
-- MODEL | |
type alias Model = | |
{ email : String -- user input | |
, validity : Maybe Bool -- Nothing before we checked it | |
, validationErrors : List String | |
} | |
model = { | |
email = "" | |
, validity = Nothing | |
, validationErrors = [] | |
} | |
flattenStringList : List String -> String | |
flattenStringList = | |
List.foldr (++) "" | |
-- UPDATE | |
type Msg = SetEmail String | Validate | |
update : Msg -> Model -> Model | |
update msg model = | |
case msg of | |
SetEmail e -> { model | email = e } | |
Validate -> { model | validity = Just (validateEmail model.email) } | |
validateEmail : String -> Bool | |
validateEmail e = | |
case parse cContent e of | |
(Ok _, _) -> True | |
(Err _, _) -> False | |
-- VIEW | |
showMaybeValidity : Maybe Bool -> Html Msg | |
showMaybeValidity mev = | |
case mev of | |
Nothing -> div [] [] | |
Just ev -> | |
case ev of | |
True -> div [] [ text "valid" ] | |
False -> div [] [ text "invalid" ] | |
view : Model -> Html Msg | |
view model = | |
div [] | |
[ | |
input [ type' "text" | |
, placeholder "user@example.org" | |
, onInput SetEmail | |
] [] | |
, p [] [] | |
, button [ onClick Validate ] [ text "validate" ] | |
, showMaybeValidity model.validity ] | |
foo : Parser String | |
foo = | |
string "foo" | |
cContent : Parser String | |
cContent = | |
cText `or` cContent | |
cText : Parser String | |
cText = | |
string "a" | |
main = | |
Html.beginnerProgram { model = model, view = view, update = update} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Combine exposing (..) | |
import Combine.Char exposing (..) | |
import String | |
import Char | |
flattenStringList : List String -> String | |
flattenStringList = | |
List.foldr (++) "" | |
comment : Parser String | |
comment = | |
map flattenStringList <| many cContent | |
foo : Parser String | |
foo = | |
string "foo" | |
cContent : Parser String | |
cContent = | |
(map String.fromChar cText) `or` foo | |
cText : Parser Char | |
cText = | |
char 'a' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment