SlideShare a Scribd company logo
1 of 31
Download to read offline
Rethinking
FrontEnd
Development
With Elm
Brian Hogan
Aboutme
» I build web things.
» I teach people.
» I make music.
» I write books.
Elm is a functional programming lnaguage like
Haskell, but more friendly, and aimed at front-end
web development.
We use Elm to make our user interface and give it
behavior.
Example
module Hello where
import Html exposing(p, text)
import Html.Attributes exposing(style)
elementStyle =
style
[ ("color", "red")
, ("font-size", "2em")
]
main =
p [elementStyle] [text "Hello World"]
Elm compilestoJavaScript
Yes. We just wrote a bunch of code that gets injected
into an HTML page.
Feel gross yet?
That'swhatReactdoestoo.
var HelloMessage = React.createClass({
render: function () {
return <h1>Hello {this.props.message}!</h1>;
}
});
React.render(<HelloMessage message="World" />, document.body);
Okay,WhyElm?
» Same concepts as React
» Pure functions
» Immutable State
» Static Typing
Main
Every Elm app calls a main function when we run it.
main =
-- something goes here
We indent the definitions of functions.
Functions
hello =
"Hello there"
Call it as
hello
Arguments
Functions can have arguments
square number =
number * number
Call it as
square 2
They have explicit returns.
MultipleArguments
Multiple arguments use spaces:
add number1 number2 =
number1 + number2
Call it as
add 1 2
Typeannotations
We can enforce data types for our functions so Elm
can help us out.
The annotation is simply a flow of types, with the
last being the return type.
functionName: TypeOfArg1-> TypeOfArg2 -> TypeOfArg3 -> ReturnType
Example:
add: Float -> Float -> Float
add number1 number2 =
number1 + number2
Htmlfunctions
elm-html module exposes many functions for building
up virtual DOM nodes.
The main function can render HTML if the HTML module
is included.
import Html exposing(p, text)
main =
p [] [text "Hello World"]
pandtext
p [] [text "Hello World"]
p and text are two functions from elm-html
p takes two lists
» a list of attributes (can be empty)
» a list of child elements
text takes a string of text to display.
HTMLfunctionsare uniform.
Each takes attributes and elements. So we can nest
them like HTML.
div [class "foo", id "bar" ] [
h1 [] [text "Hello"],
p [] [text "World"]
]
There's a function for every element. Just be sure to
expose what you use.
Composability
main =
view
view: Html
view =
div [] [
p [
text "Hello ",
em "world"
]
]
Resuability
main =
div [] [
view "Hello",
view "Goodbye"
]
view: String -> Html
view word =
div [] [
p [
text (word ++ " "),
em "world"
]
]
ElmArchitecture
View
Function that fires when model changes. Transofms a
model into the UI that people see.
Model
State of the app. No behavior. Just the state.
Update
Function that fires when state changes. Always
returns a new model.
Signalsand Mailboxes
Signals
Signals route messages around the application.
Pressing a button is a signal. We can send data along
signals.
Mailboxes
Mailboxes receive signals and send signals. A mailbox
has an address and a signal to respond to.
Basic Flow
» Model is initialied
» View is displayed with model
» Events send Signals to Mailboxes
» Mailboxes trigger updates
» New model is created
» New view is rendered
Elm StartApp.Simple
Simple pattern we use to hide much of the details.
Like Flux, without all the code.
Example
type Action = Increment | Decrement
main =
StartApp.start { model = model, view = view, update = update }
model = 0
view address model =
div []
[ button [ onClick address Decrement ] [ text "-" ]
, div [] [ text (toString model) ]
, button [ onClick address Increment ] [ text "+" ]
]
update action model =
case action of
Increment -> model + 1
Decrement -> model - 1
Calculator
Compound Interest Calculator
“Write a program to compute the value of an
investment compounded over time. The program should
ask for the starting amount, the number of years to
invest, the interest rate, and the number of periods
per year to compound.”
Whatyou need
» Node.js http://nodejs.org
$ npm install -g elm
» The elm package for Node
» Your favorite text editor
Workingwith HTML
Elm's virtual DOM support is separate:
$ elm package install evancz/elm-html
Make browser reload when we save
$ npm install -g elm-live
Projectsetup
Create folder and file to work in
$ mkdir calculator && cd calculator
$ touch calculator.elm
Init the project
$ elm package install
Install HTML and StartApp
$ elm package install evancz/elm-html
$ elm package install evancz/start-app
Demo
» Create the basic app
» Build the form
» Perform calculations
Issues
1.Tons of code to do simple
thins
2.Integration with external
services is complex.
3.Must re-learn a lot of
things about web
development
4.Small community
Benefits
1.Small community
2.Benefits of React with a
clear opinionated approach
3.Fantastic error messages
4.Types ensure data integrity
and flow
Write code
» Elm website: http://elm-
lang.org/
» Try Elm http://elm-
lang.org/try
» Package system: http://
package.elm-lang.org/
» Documentation http://elm-
lang.org/docs
Whereto go next?
Book: http://pragprog.com/
titles/bhwb
Twitter: @bphogan
Material: http://bphogan.com/
presentations/elm2016/
Thankyou!

More Related Content

Similar to Rethink Frontend Development With Elm

Elm 0.17 at Dublin Elm Meetup May 2016
Elm 0.17 at Dublin Elm Meetup May 2016Elm 0.17 at Dublin Elm Meetup May 2016
Elm 0.17 at Dublin Elm Meetup May 2016Michael Twomey
 
Play framework training by Neelkanth Sachdeva @ Scala traits event , New Delh...
Play framework training by Neelkanth Sachdeva @ Scala traits event , New Delh...Play framework training by Neelkanth Sachdeva @ Scala traits event , New Delh...
Play framework training by Neelkanth Sachdeva @ Scala traits event , New Delh...Knoldus Inc.
 
Play framework training by Neelkanth Sachdeva @ Scala Traits Event , New Delh...
Play framework training by Neelkanth Sachdeva @ Scala Traits Event , New Delh...Play framework training by Neelkanth Sachdeva @ Scala Traits Event , New Delh...
Play framework training by Neelkanth Sachdeva @ Scala Traits Event , New Delh...Neelkanth Sachdeva
 
Scripting languages
Scripting languagesScripting languages
Scripting languagesteach4uin
 
Declarative UIs with Jetpack Compose
Declarative UIs with Jetpack ComposeDeclarative UIs with Jetpack Compose
Declarative UIs with Jetpack ComposeRamon Ribeiro Rabello
 
EPiServer report generation
EPiServer report generationEPiServer report generation
EPiServer report generationPaul Graham
 
Introduction to Shiny for building web apps in R
Introduction to Shiny for building web apps in RIntroduction to Shiny for building web apps in R
Introduction to Shiny for building web apps in RPaul Richards
 
Angular JS2 Training Session #1
Angular JS2 Training Session #1Angular JS2 Training Session #1
Angular JS2 Training Session #1Paras Mendiratta
 
Python Code Camp for Professionals 4/4
Python Code Camp for Professionals 4/4Python Code Camp for Professionals 4/4
Python Code Camp for Professionals 4/4DEVCON
 
Model-View-Update, and Beyond!
Model-View-Update, and Beyond!Model-View-Update, and Beyond!
Model-View-Update, and Beyond!Simon Fowler
 
Introduzione JQuery
Introduzione JQueryIntroduzione JQuery
Introduzione JQueryorestJump
 
JavaScript - Chapter 12 - Document Object Model
  JavaScript - Chapter 12 - Document Object Model  JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object ModelWebStackAcademy
 
eXo SEA - JavaScript Introduction Training
eXo SEA - JavaScript Introduction TrainingeXo SEA - JavaScript Introduction Training
eXo SEA - JavaScript Introduction TrainingHoat Le
 

Similar to Rethink Frontend Development With Elm (20)

Elm 0.17 at Dublin Elm Meetup May 2016
Elm 0.17 at Dublin Elm Meetup May 2016Elm 0.17 at Dublin Elm Meetup May 2016
Elm 0.17 at Dublin Elm Meetup May 2016
 
Play framework training by Neelkanth Sachdeva @ Scala traits event , New Delh...
Play framework training by Neelkanth Sachdeva @ Scala traits event , New Delh...Play framework training by Neelkanth Sachdeva @ Scala traits event , New Delh...
Play framework training by Neelkanth Sachdeva @ Scala traits event , New Delh...
 
Play framework training by Neelkanth Sachdeva @ Scala Traits Event , New Delh...
Play framework training by Neelkanth Sachdeva @ Scala Traits Event , New Delh...Play framework training by Neelkanth Sachdeva @ Scala Traits Event , New Delh...
Play framework training by Neelkanth Sachdeva @ Scala Traits Event , New Delh...
 
Scripting languages
Scripting languagesScripting languages
Scripting languages
 
A proper introduction to Elm
A proper introduction to ElmA proper introduction to Elm
A proper introduction to Elm
 
Declarative UIs with Jetpack Compose
Declarative UIs with Jetpack ComposeDeclarative UIs with Jetpack Compose
Declarative UIs with Jetpack Compose
 
Android L01 - Warm Up
Android L01 - Warm UpAndroid L01 - Warm Up
Android L01 - Warm Up
 
Html css
Html cssHtml css
Html css
 
WEB DEVELOPMENT
WEB DEVELOPMENTWEB DEVELOPMENT
WEB DEVELOPMENT
 
EPiServer report generation
EPiServer report generationEPiServer report generation
EPiServer report generation
 
Introduction to Shiny for building web apps in R
Introduction to Shiny for building web apps in RIntroduction to Shiny for building web apps in R
Introduction to Shiny for building web apps in R
 
Javascript
JavascriptJavascript
Javascript
 
Angular JS2 Training Session #1
Angular JS2 Training Session #1Angular JS2 Training Session #1
Angular JS2 Training Session #1
 
J Query Public
J Query PublicJ Query Public
J Query Public
 
Python Code Camp for Professionals 4/4
Python Code Camp for Professionals 4/4Python Code Camp for Professionals 4/4
Python Code Camp for Professionals 4/4
 
Model-View-Update, and Beyond!
Model-View-Update, and Beyond!Model-View-Update, and Beyond!
Model-View-Update, and Beyond!
 
Introduzione JQuery
Introduzione JQueryIntroduzione JQuery
Introduzione JQuery
 
Elm @ DublinJS
Elm @ DublinJSElm @ DublinJS
Elm @ DublinJS
 
JavaScript - Chapter 12 - Document Object Model
  JavaScript - Chapter 12 - Document Object Model  JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object Model
 
eXo SEA - JavaScript Introduction Training
eXo SEA - JavaScript Introduction TrainingeXo SEA - JavaScript Introduction Training
eXo SEA - JavaScript Introduction Training
 

Recently uploaded

%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...masabamasaba
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...masabamasaba
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationJuha-Pekka Tolvanen
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxAnnaArtyushina1
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfkalichargn70th171
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Bert Jan Schrijver
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...masabamasaba
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareJim McKeeth
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...masabamasaba
 

Recently uploaded (20)

%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 

Rethink Frontend Development With Elm

  • 2. Aboutme » I build web things. » I teach people. » I make music. » I write books.
  • 3. Elm is a functional programming lnaguage like Haskell, but more friendly, and aimed at front-end web development. We use Elm to make our user interface and give it behavior.
  • 4. Example module Hello where import Html exposing(p, text) import Html.Attributes exposing(style) elementStyle = style [ ("color", "red") , ("font-size", "2em") ] main = p [elementStyle] [text "Hello World"]
  • 5. Elm compilestoJavaScript Yes. We just wrote a bunch of code that gets injected into an HTML page. Feel gross yet?
  • 6. That'swhatReactdoestoo. var HelloMessage = React.createClass({ render: function () { return <h1>Hello {this.props.message}!</h1>; } }); React.render(<HelloMessage message="World" />, document.body);
  • 7. Okay,WhyElm? » Same concepts as React » Pure functions » Immutable State » Static Typing
  • 8. Main Every Elm app calls a main function when we run it. main = -- something goes here We indent the definitions of functions.
  • 10. Arguments Functions can have arguments square number = number * number Call it as square 2 They have explicit returns.
  • 11. MultipleArguments Multiple arguments use spaces: add number1 number2 = number1 + number2 Call it as add 1 2
  • 12. Typeannotations We can enforce data types for our functions so Elm can help us out. The annotation is simply a flow of types, with the last being the return type. functionName: TypeOfArg1-> TypeOfArg2 -> TypeOfArg3 -> ReturnType Example: add: Float -> Float -> Float add number1 number2 = number1 + number2
  • 13. Htmlfunctions elm-html module exposes many functions for building up virtual DOM nodes. The main function can render HTML if the HTML module is included. import Html exposing(p, text) main = p [] [text "Hello World"]
  • 14. pandtext p [] [text "Hello World"] p and text are two functions from elm-html p takes two lists » a list of attributes (can be empty) » a list of child elements text takes a string of text to display.
  • 15. HTMLfunctionsare uniform. Each takes attributes and elements. So we can nest them like HTML. div [class "foo", id "bar" ] [ h1 [] [text "Hello"], p [] [text "World"] ] There's a function for every element. Just be sure to expose what you use.
  • 16. Composability main = view view: Html view = div [] [ p [ text "Hello ", em "world" ] ]
  • 17. Resuability main = div [] [ view "Hello", view "Goodbye" ] view: String -> Html view word = div [] [ p [ text (word ++ " "), em "world" ] ]
  • 18. ElmArchitecture View Function that fires when model changes. Transofms a model into the UI that people see. Model State of the app. No behavior. Just the state. Update Function that fires when state changes. Always returns a new model.
  • 19. Signalsand Mailboxes Signals Signals route messages around the application. Pressing a button is a signal. We can send data along signals. Mailboxes Mailboxes receive signals and send signals. A mailbox has an address and a signal to respond to.
  • 20. Basic Flow » Model is initialied » View is displayed with model » Events send Signals to Mailboxes » Mailboxes trigger updates » New model is created » New view is rendered
  • 21. Elm StartApp.Simple Simple pattern we use to hide much of the details. Like Flux, without all the code.
  • 22. Example type Action = Increment | Decrement main = StartApp.start { model = model, view = view, update = update } model = 0 view address model = div [] [ button [ onClick address Decrement ] [ text "-" ] , div [] [ text (toString model) ] , button [ onClick address Increment ] [ text "+" ] ] update action model = case action of Increment -> model + 1 Decrement -> model - 1
  • 23. Calculator Compound Interest Calculator “Write a program to compute the value of an investment compounded over time. The program should ask for the starting amount, the number of years to invest, the interest rate, and the number of periods per year to compound.”
  • 24. Whatyou need » Node.js http://nodejs.org $ npm install -g elm » The elm package for Node » Your favorite text editor
  • 25. Workingwith HTML Elm's virtual DOM support is separate: $ elm package install evancz/elm-html Make browser reload when we save $ npm install -g elm-live
  • 26. Projectsetup Create folder and file to work in $ mkdir calculator && cd calculator $ touch calculator.elm Init the project $ elm package install Install HTML and StartApp $ elm package install evancz/elm-html $ elm package install evancz/start-app
  • 27. Demo » Create the basic app » Build the form » Perform calculations
  • 28. Issues 1.Tons of code to do simple thins 2.Integration with external services is complex. 3.Must re-learn a lot of things about web development 4.Small community
  • 29. Benefits 1.Small community 2.Benefits of React with a clear opinionated approach 3.Fantastic error messages 4.Types ensure data integrity and flow
  • 30. Write code » Elm website: http://elm- lang.org/ » Try Elm http://elm- lang.org/try » Package system: http:// package.elm-lang.org/ » Documentation http://elm- lang.org/docs
  • 31. Whereto go next? Book: http://pragprog.com/ titles/bhwb Twitter: @bphogan Material: http://bphogan.com/ presentations/elm2016/ Thankyou!