beyond react
TRANSCRIPT
JS Elm
Bools
Numbers
Strings
Arrays
Objects
true, false True, False
42, 3.14 42, 3.14
'abc', "abc", `abc` 'a', "abc", """abc"""
[1, 2, 3] [1, 2, 3], [1..4]
{ x: 0, y: 1 } { x = 0, y = 1 }
JS Elm
if
switch
if (key === 40) { n += 1 } else if (key === 38) { n -= 1 } else {}
(n > 10) ? 'a' : 'b'
if key == 40 then n + 1 else if key == 38 then n - 1 else n
if n > 10 then "a" else "b"
switch (msg) { case "greeting": return true; break; default: return false; break; }
case msg of
"greeting" -> True
_ -> False
JS Elm
var / let
comments
var twenty = 2 * 10; var sixteen = 4 * 4; var r = twenty + sixteen
let twenty = 2 * 10 sixteen = 4 ^ 2 in twenty + sixteen
// single line
/* multiline */
-- single line
{- multiline -}
JS Elm
Functions
Lambdas
Invocation
function add(a, b) { return a + b; }
add a b = a + b
(a, b) => a + b
\a b -> a + b
add(2, 3)
true && !(true || false)
add 2 3
True && not (True || False)
ImmutabilitypointA = { x = 3, y = 4 }
-- can read fields
pointA.x
-- but can't set them
pointA.x = pointA.x + 1 -- ERROR
-- create a new point instead!
pointB = { pointA | x = pointA.x + 1 }
Types-- values 25 : Int'a' : Char "abc" : String
(1, 'a') : (Int, Char)
[1, 2, 3] : List Int { x = 1, y = 2 } : { x: Int, y: Int }
-- aliases type alias Point = { x: Int, y: Int }
No null exceptions, evertype Maybe a = Just a | Nothing
case maybeUser of
Just user -> "welcome " ++ user.name
Nothing -> "please login"
Elegant Code* with guarantees
length : List a -> Int length list = case list of
[] -> 0
first :: rest -> 1 + length rest
Modeltype alias Model = { tasks : List Task , count: Int }
type alias Task = { description : String , completed : Bool , id : Int }
Updateupdate : TodoMsg -> Model -> Model update msg model = case msg of
CreateTask description -> let newId = model.count newTask = Task description False newId in { model | tasks = model.tasks ++ [newTask], count = model.count + 1 }
-- etc
ViewtaskView : Task -> Html Msg
taskView task =
div
[ class "task"
]
[ input
[ type "checkbox", onClick CompleteTask ]
[]
, text task.description
]
Toolingcompiler with friendly error messagespackage manager enforces semverone command, zero build setupelm-formatjs interop
Communityactive, inclusive and beginner friendlygreat documentation with heaps of examplesfocus on building things
I'm sold! How do I get started?
Elm Website - elm-lang.org
The Elm Guide - guide.elm-lang.org
Sydney Elm Meetup
meetup.com/Sydney-Elm-Meetup/