Creative Commons Licence This work is licensed under a Creative Commons
Attribution-ShareAlike 4.0 International License

Source: github.com/andybalaam/videos-elm

Elm makes me happy

Andy Balaam
artificialworlds.net/blog

Contents

Elm is

Immediate

main = Browser.sandbox { init = "foo" , update = \_ m -> String.reverse m , view = \m -> button [ onClick 0 ] [ text m ] }

Immediate

> 3 + 5 8 : number > "Hello!" "Hello!" : String

Immediate

> x = 3 3 : number > x + 2 5 : number

Immediate

> double y = y * 2 <function> : number -> number > double 4 8 : number

Immediate

> sum_of_squares a b = a^2 + b^2 <function> : number -> number -> number > sum_of_squares 3 4 25 : number

Immediate

type alias Model = { x : Float , y : Float , centreX : Float , centreY : Float }

Immediate

view m = ... svg [ width "100" , height "40" ] [ ellipse [ cx "10", cy "34", rx "12", ry "16" , fill "white", stroke "black", strokeWidth "2px" ] [] ...

Immediate

update (MouseChange x y) model = ( { model | x = x , y = y } , Cmd.none )

Immediate

$ sudo apt-get install nodejs npm $ sudo npm install -g elm $ mkdir hello; cd hello $ echo | elm init $ echo ' import Html main = Html.text "Hello" ' > src/Main.elm $ elm reactor & $ firefox localhost:8000/src/Main.elm &

Clean

Clean

Clean

type alias Model = { email1 : String , email2 : String }

Clean

view model = ... div [] [ button [ submitDisabled model ] [ text "Submit" ] ] ...

Clean

submitDisabled model = disabled ( (validEmail1 model /= Valid) || (validEmail2 model /= Valid) )

Clean

type Msg = Email1Changed String | Email2Changed String update msg model = case msg of Email1Changed s -> { model | email1 = s } Email2Changed s -> { model | email2 = s }

Clean

Frustrating

-- TOO MANY ARGS -------------------------------------- src/FrustrationTypes.elm The `myfn` function expects 1 argument, but it got 2 instead. 10| myfn 3 4 ^^^^ Are there any missing commas? Or missing parentheses? Makefile:8: recipe for target 'type' failed

Frustrating

-- BAD MAIN TYPE ------------------------------------------- src/Frustration.elm I cannot handle this type of `main` value: 2| main = 1 + 1 ^^^^ The type of `main` value I am seeing is: number I only know how to handle Html, Svg, and Programs though. Modify `main` to be one of those types of values!

Frustrating

-- TYPE MISMATCH -------------------------------------------- src/Validation.elm The 1st argument to `sandbox` is not what I expect: 10| Browser.sandbox 11|> { init = init 12|> , update = update 13|> , view = view 14|> } This argument is a record of type: { init : Model, update : String -> Model -> Model, view : Model -> Html Msg } But `sandbox` needs the 1st argument to be: { init : Model , update : String -> Model -> Model , view : Model -> Html String }

Frustrating

Success! Compiled 1 module.

Rewarding

Success! Compiled 1 module.

Rewarding

> List.map double (List.range 1 3) [2,4,6] : List Int > List.map (\x -> x^2) [1,2,3] [1,4,9] : List number

Rewarding

> do_twice fn = \x -> fn ( fn x ) <function> : (a -> a) -> a -> a > do_twice double 4 16 : number

Rewarding

> double_twice = do_twice double <function> : number -> number > double_twice 4 16 : number

Rewarding

More info