SlideShare a Scribd company logo
SoutheastMichiganJavaScript–PlantingSeedswithElm
December11,2017 11
Planting Seeds
with Elm
An introduction to the Elm programming language
SoutheastMichiganJavaScript–PlantingSeedswithElm
December11,2017 2
What is Elm?
SoutheastMichiganJavaScript–PlantingSeedswithElm
December11,2017 3
 Functional language for the browser
 Strongly and statically typed
 Compiles to JavaScript
 ML style language, written in Haskell
 Declarative syntax
What is Elm?
SoutheastMichiganJavaScript–PlantingSeedswithElm
December11,2017 4
What makes Elm special?
SoutheastMichiganJavaScript–PlantingSeedswithElm
December11,2017 5
 A beginner friendly language with a minimal but powerful set of
features
 A quick and kind compiler that provides type inference
 Advertises “No” runtime exceptions
 An architecture designed for the language
 Fully interoperable with JavaScript
 Enforces semantic versioning
 A helpful and friendly community
 A really nice formatter that eliminates the need for a linter
What makes Elm special?
SoutheastMichiganJavaScript–PlantingSeedswithElm
December11,2017 6
 A quick and kind compiler that provides type inference
What makes Elm special?
SoutheastMichiganJavaScript–PlantingSeedswithElm
December11,2017 7
 A quick and kind compiler that provides type inference
What makes Elm special?
SoutheastMichiganJavaScript–PlantingSeedswithElm
December11,2017 8
 A note on type inference:
 Type inference allows the compiler to “infer” the types in your app
without you explicitly defining them.
 For instance, the following function definition examples are both
valid:
 In spite of this, it’s a good practice to provide the type annotations
to improve readability and general code quality.
What makes Elm special?
or
SoutheastMichiganJavaScript–PlantingSeedswithElm
December11,2017 9
 A quick and kind compiler that provides type inference
 There are a few helpful files here that may be useful to read before
you run into compiler errors:
https://github.com/elm-lang/elm-compiler/tree/master/hints
What makes Elm special?
SoutheastMichiganJavaScript–PlantingSeedswithElm
December11,201710
 Advertises “No” runtime exceptions
From the Elm website:
“Unlike hand-written JavaScript, Elm code does not produce runtime
exceptions in practice. Instead, Elm uses type inference to detect
problems during compilation and give friendly hints. This way
problems never make it to your users. NoRedInk has 80k+ lines of
Elm, and after more than a year in production, it still has not
produced a single runtime exception.”
This article has more insight on the subject:
http://elm-lang.org/blog/compilers-as-assistants
What makes Elm special?
SoutheastMichiganJavaScript–PlantingSeedswithElm
December11,201711
 Advertises “No” runtime exceptions
From personal and professional experience, I have experienced this firsthand.
Clarity has had 5 projects in production over the past 18 months and these stats still hold true.
What makes Elm special?
This article has more insight on the subject:
http://elm-lang.org/blog/compilers-as-assistants
SoutheastMichiganJavaScript–PlantingSeedswithElm
December11,201712
The Elm Architecture
or “TEA” as it’s sometimes called
SoutheastMichiganJavaScript–PlantingSeedswithElm
December11,201713
 The logic of every Elm program will break up into three cleanly
separated parts:
 Model — the state of your application (a single state atom)
 Update — a way to update your state
 View — a way to view your state as HTML
The Elm Architecture
View
Model Update
SoutheastMichiganJavaScript–PlantingSeedswithElm
December11,201714
A simple example…
SoutheastMichiganJavaScript–PlantingSeedswithElm
December11,201715
A simple example…
SoutheastMichiganJavaScript–PlantingSeedswithElm
December11,201716
 After compilation, the code below produces a simple counter app
A simple example…
SoutheastMichiganJavaScript–PlantingSeedswithElm
December11,201717
Building something more complex
but not too complex…
SoutheastMichiganJavaScript–PlantingSeedswithElm
December11,201718
 This is a walkthrough of code I modified from the original HTTP
example from the Elm site:
https://ellie-app.com/4fgzBGyFzcYa1/0
 Original example: http://elm-lang.org/examples/http
An example of The Elm Architecture
SoutheastMichiganJavaScript–PlantingSeedswithElm
December11,201719
 Elm uses modules as method to split up code
 The code (..) exposes everything to the consuming module.
 Consider exposed functions your “public” API for a module
 Anything not exposed is “private” to the module.
 Next, we move on to our application’s imports
A module system
SoutheastMichiganJavaScript–PlantingSeedswithElm
December11,201720
 The import statement tells the compiler that we want to bring something into our
module to use. An app without imports is extremely rare, as a matter of fact, even the
most basic “Hello, World!” example has an import:
For our application, we need to use some standard libraries (ex: Html*) as well as
some non-standard ones (ex: RemoteData) :
Similar to how module’s are defined, imports let us choose which functions to expose to
our application via the exposing keyword. Again (..) here means we’re bringing
everything the imported module expose into the local namespace. You can also alias
imported modules using the as keyword
A module system
SoutheastMichiganJavaScript–PlantingSeedswithElm
December11,201721
 Code that is similar to what you’ll see in most Elm applications:
 The program’s execution will start in the main function and continue from there. Record value
assignment is done using the = operator (ex: init = init "cats") as in many
languages. Here we are passing in an initialization function for our program’s init to run upon
startup. This function should transform any inputs (in this case “cats”) into an initial state for
our application.
 In our case, the init function does this by taking a String as an input
and emitting a (Model, Cmd Msg)
Setting up the application
SoutheastMichiganJavaScript–PlantingSeedswithElm
December11,201722
 More on the program module...
 program is a function of the Html module in the Elm language core
(http://package.elm-lang.org/packages/elm-lang/html/2.0.0/Html). This module also
exposes a couple of other handy functions for starting a your program (as well as
others related to Html) that you may want to use based on what your application is
doing.
 The main three types of programs exposed by the Html module are:
 Html.beginnerProgram
 Html.program
 Html.programWithFlags
Setting up the application
SoutheastMichiganJavaScript–PlantingSeedswithElm
December11,201723
 Html.beginnerProgram can be used if your application does not have any
side effects that need to occur outside the input and Dom events. Basically, this
version of Program does not allow for adding asynchronous commands,
subscriptions etc. (this includes Tasks) input from DOM Events
 Html.program can be used if your application needs access to run commands
or use subscriptions (this includes interop with JavaScript). Not every application
needs this but this is likely to be the most common version of Program used.
 Html.programWithFlags allows for all of the functionality of Html.program but
can also consume inputs from the JavaScript environment at startup. Some
common uses are to pull values from localStorage upon loading the page.
Setting up the application
SoutheastMichiganJavaScript–PlantingSeedswithElm
December11,201724
 The Model in Elm is a single state atom of your application’s state. Here is the
Model for the example application:
 This Model encompasses the entire “state” portion of my application. As seen in a
previous slide, the init function is used to setup the initial state of this Model
when the application starts.
 A Model can pretty much be of any type, but as your application grows, most of
the time an Elm “Record” (http://elm-lang.org/docs/records) is the appropriate
storage mechanism for this state.
The Model
SoutheastMichiganJavaScript–PlantingSeedswithElm
December11,201725
 One of the features of Elm are it’s Union Types, these are sometimes called
“tagged unions” or “ADTs” in other languages.
 A common use of Union Types in Elm is to setup the message structure for an
application. Below is example of how Union Types are used in the example
application:
 More on Union Types can be found here:
https://guide.elm-lang.org/types/union_types.html
Union Types and modeling data
SoutheastMichiganJavaScript–PlantingSeedswithElm
December11,201726
 A more complex union type example:
 Referenced from RemoteData package by Kris Jenkins:
http://package.elm-lang.org/packages/krisajenkins/remotedata/latest
Union Types and modeling data
SoutheastMichiganJavaScript–PlantingSeedswithElm
December11,201727
 As I’ve used Elm more, I’ve found that Union Types are effective way to model
pretty much anything. For instance, let’s say you have a toggle button and you
want to represent either on ”On” or “Off” state:
 For something as simple as the last example, you could just use a value of type
Bool which, just so happens to be a union type itself.
 But what’s nice is that, with the custom union type, you get more expressiveness
from the statement, which (generally) makes things easier to read.
Union Types and modeling data
SoutheastMichiganJavaScript–PlantingSeedswithElm
December11,201728
 In practice, union types allow for actions based on a pattern match which we’ll see
in the next slide as we move on to the update function!
Union Types and modeling data
SoutheastMichiganJavaScript–PlantingSeedswithElm
December11,201729
 Now that we’ve established that our Model holds the state for our entire application,
how to we update it? That’s where the update function comes into play.
 Here is the update function for the example application:
 The type definition for update implies that it takes two arguments a Msg and a
Model. It then emits a tuple in the form of (Model, Cmd Msg) based on those inputs.
Updating State
SoutheastMichiganJavaScript–PlantingSeedswithElm
December11,201730
Updating State
In this case, pattern matching is being used to determine what should be returned by the update
function (how the model should change based on the current model) as well as any additional
commands that may need to be run based on the condition. This is a fairly standard thing to see
in an Elm update function
However, as an application scales, this section can grow and it may be beneficial to break out
some of the smaller blocks of execution into their own function (or even their own module)
Update the Model
Schedule a Cmd to be run upon the
next cycle of the Elm runtime
loop (after the View is rendered)
Update the Model
In this case, we don’t need to run
Any additional commands, just
process the response so we use
Cmd.none
SoutheastMichiganJavaScript–PlantingSeedswithElm
December11,201731
 Your applications View code defines what is displayed to the end user at any given time
 A view can also be comprised of other functions that serve as sub views or partial views.
 As with all Elm code, view is just a pure function and starts with a type and function definition
 The type definition here says that this function takes in a Model and emits an Html Msg
The View
SoutheastMichiganJavaScript–PlantingSeedswithElm
December11,201732
 Elm has let statements which allow you to define variables that are local to the scope
between let and in.
 In the code below, let..in is being used to assign multiple variables (ex: waitingGifUrl,
buttonText etc.) at once that will be used in the context below the in keyword.
 The code(firstRun, gifUrl) uses destructuring to assign multiple variables at once based on the
case.
The View
SoutheastMichiganJavaScript–PlantingSeedswithElm
December11,201733
 Last, but not least, the code below the in keyword is used to declaratively state what should be displayed on
the page. Rather than using plain HTML here, Elm uses functions for everything (including HTML).
 Most of the Html functions have the following type signature (h1 example):
The View
SoutheastMichiganJavaScript–PlantingSeedswithElm
December11,201734
 In order to fetch the data from a remote server, the example application has a function called getRandomGif.
This function is in charge of taking in a String as an input and emitting a Cmd Msg
 An example of using the pipe-operator from the official site: http://elm-lang.org/examples/pipes/code
A simple Http request
A new Cmd is scheduled…
ImgResponse is the designated
message endpoint
Using the pipe-operators |> or <| one
can pass through the result to the next
function where it is used as the last
argument for the function it gets piped
into. The data can flow in both
directions, though the usual way of
chaining functions with the pipe operator
is using |> and having a single line per
function call.
SoutheastMichiganJavaScript–PlantingSeedswithElm
December11,201735
 When we schedule the Http request, one of the arguments we pass into the Http.get function is the
Decoder that should be used to parse the resultant JSON.
 When we schedule the Http request, one of the arguments we pass into the Http.get function is the
Decoder that should be used to parse the resultant JSON.
 In this case, we’re just using Decode.at to quickly extract only the data.image_url field from the JSON
data (shown below).
Decoding the data
SoutheastMichiganJavaScript–PlantingSeedswithElm
December11,201736
 Since everything in Elm is a function, we can easily cleanup our codebase as it grows. For instance, the
following three functions were originally part of the let..in block of my view code
 As my code grew, it seems intuitive to break those out into their own bite sized chunks of code. This also lends
itself well to reuse.
Functions everywhere!
SoutheastMichiganJavaScript–PlantingSeedswithElm
December11,201737
 More on scaling and reuse in Elm:
 From the official guide: https://guide.elm-lang.org/reuse/
 An example of scalable SPA in Elm:
https://github.com/rtfeldman/elm-spa-example
Scaling and reuse
SoutheastMichiganJavaScript–PlantingSeedswithElm
December11,201738
 In Elm JavaScript interop is done through a pub/sub system called “Ports”. The notion of a Port is that it is a
pipe between Elm and JavaScript. The basic flow of ports is shown in the diagram below:
 Reference article: https://hackernoon.com/how-elm-ports-work-with-a-picture-just-one-25144ba43cdd
 Ports have either an inbound or an outbound flow. An example of an outbound port in Elm looks like the
following: port myOutboundPort: String -> Cmd msg
 Here we are defining a port that sends one String argument to the native JavaScript code that it can use in
it’s function.
JavaScript Interop
SoutheastMichiganJavaScript–PlantingSeedswithElm
December11,201739
 We can schedule myOutboundPort to run at either startup of the application (in our init function) or in our
update function
 Example of scheduling an outbound port to run upon startup:
 Example of scheduling an outbound port to run at update:
JavaScript Interop
SoutheastMichiganJavaScript–PlantingSeedswithElm
December11,201740
 Inbound ports are handled via subscriptions. To listen for inbound messages, a subscription needs to be setup
(in our app the subscriptions function would handle this). We’ll jump into the subscription code on the next
slide.
 For now, here is the syntax to setup an inbound port:
 In the example above DataStore is a type with the same shape as the data we expect to come in. Let’s say
we are being sent the following JSON payload:
 In this case, DataStore should be defined as follows:
JavaScript Interop
SoutheastMichiganJavaScript–PlantingSeedswithElm
December11,201741
 To subscribe (listen) for messages coming in on this port, we need to add a subscription to our
subscriptions function:
JavaScript Interop
When we receive messages from this
port the update function will be
called with ExecuteInboundPort
as the message
SoutheastMichiganJavaScript–PlantingSeedswithElm
December11,201742
 A few notes on subscriptions (Sub) and commands (Cmd) :
 If you need to subscribe to multiple endpoints (Phoenix channels, Ports etc.), you can
use Sub.batch with a List of subscriptions rather than just listing one.
 Similar to Sub.batch, Cmd.batch allows you to schedule multiple commands to run
 There are other functions for working with subscriptions and commands, their respective
documentation pages are:
 Subscriptions: http://package.elm-lang.org/packages/elm-lang/core/5.1.1/Platform-Sub
 Commands: http://package.elm-lang.org/packages/elm-lang/core/5.1.1/Platform-Cmd
Commands and Subscriptions
SoutheastMichiganJavaScript–PlantingSeedswithElm
December11,201743
A more complete diagram of the
Elm Architecture
SoutheastMichiganJavaScript–PlantingSeedswithElm
December11,201744
 Elm is a generally considered a very easy language to get started with. To get Elm installed,
you can visit https://guide.elm-lang.org/install.html and follow the directions.
 Installers are available for Windows, Mac and Anywhere (via npm)
 Language plugins are available for many popular editors and IDEs including Sublime, VS
Code, Atom and JetBrains (WebStorm etc.)
 It’s highly recommended that you use elm-format to auto format code upon save (via a
watcher). Directions for install and setup are here: https://github.com/avh4/elm-format
Getting started with Elm
SoutheastMichiganJavaScript–PlantingSeedswithElm
December11,201745
 Elm is a compiled language and as uses a tool called “elm-make” to compile it’s code from Elm
to JavaScript.
 One cool feature of the compiler is, by adding the –warn flag, you’ll be alerted to many to
unused imports. Similar to the normal compiler issues that cause a failure, these messages
are helpful and friendly.
Getting started with Elm
SoutheastMichiganJavaScript–PlantingSeedswithElm
December11,201746
 Examples from the official site: http://elm-lang.org/examples
 The Official Guide: https://guide.elm-lang.org/
 Elm and JS data types and structures compared: http://elm-lang.org/docs/from-javascript
 Ellie App (In browser editor): https://ellie-app.com/
 Elm in Action (Book): https://www.manning.com/books/elm-in-action
 Beginner Elm Tutorial: http://elmprogramming.com/
 Elm Town (Podcast): https://elmtown.github.io/
 Podcast focused on learning Elm when coming from a JS background: http://jstoelm.com/
 Awesome Elm (collection of resources and libraries): https://github.com/isRuslan/awesome-elm
Resources
SoutheastMichiganJavaScript–PlantingSeedswithElm
December11,201747
 Elm Slack Team: https://elmlang.herokuapp.com/
 Reddit: https://www.reddit.com/r/elm/
 Twitter: https://twitter.com/elmlang
 Google Mailing List: https://groups.google.com/forum/?fromgroups#!forum/elm-discuss
 Discourse (possibly replacing Google mailing list): https://discourse.elm-lang.org/
Community
SoutheastMichiganJavaScript–PlantingSeedswithElm
December11,201748
Local Elm Meetup (Elm Detroit): https://www.meetup.com/elm-detroit/
We meet on the first Thursday of every month at 7pm. Our next event is on January 4th.
The event starts out with about a half an hour of social time and then offers two main tracks:
 Open space coding/discussions
 A somewhat informal workshop and Q/A session for folks interested in learning more about Elm and how
to build stuff. We use this curriculum https://github.com/Elm-Detroit/elm-workshop for the workshop.
Community
SoutheastMichiganJavaScript–PlantingSeedswithElm
December11,201749
Questions?

More Related Content

PPTX
Elm Detroit 9/7/17 - Planting Seeds with Elm
DOCX
Article laravel 8
PDF
[CB19] API-induced SSRF: How Apple Pay Scattered Vulnerabilities Across the W...
PPTX
Cakephp vs. laravel
PPTX
Functional programming
PDF
Metaprogramming JavaScript
PDF
Redux and context api with react native app introduction, use cases, implemen...
PPTX
Vba Class Level 3
Elm Detroit 9/7/17 - Planting Seeds with Elm
Article laravel 8
[CB19] API-induced SSRF: How Apple Pay Scattered Vulnerabilities Across the W...
Cakephp vs. laravel
Functional programming
Metaprogramming JavaScript
Redux and context api with react native app introduction, use cases, implemen...
Vba Class Level 3

What's hot (14)

PDF
Eclipse and Java 8 - Eclipse Day India 2013
PDF
DEF CON 27 - JOSHUA MADDUX - api induced ssrf
PDF
RxJS: A Beginner & Expert's Perspective - ng-conf 2017
PPTX
Angular4 getting started
PDF
A Practical Approach to React Native at All Things Open Conference
PDF
EMPEX LA 2018 - Inclusion Starts with Docs
PDF
React, GraphQL и Relay - вполне себе нормальный компонентный подход (nodkz)
PDF
JDT Embraces Lambda Expressions - EclipseCon North America 2014
PDF
API Technical Writing
PDF
Pluggable patterns
PPT
Introduction to Java Scripting
PDF
React App State Management with react hooks and context api
PDF
Refactoring: Improve the design of existing code
PPTX
API-first development
Eclipse and Java 8 - Eclipse Day India 2013
DEF CON 27 - JOSHUA MADDUX - api induced ssrf
RxJS: A Beginner & Expert's Perspective - ng-conf 2017
Angular4 getting started
A Practical Approach to React Native at All Things Open Conference
EMPEX LA 2018 - Inclusion Starts with Docs
React, GraphQL и Relay - вполне себе нормальный компонентный подход (nodkz)
JDT Embraces Lambda Expressions - EclipseCon North America 2014
API Technical Writing
Pluggable patterns
Introduction to Java Scripting
React App State Management with react hooks and context api
Refactoring: Improve the design of existing code
API-first development
Ad

Similar to SEMjs - Planting Seeds with Elm (20)

PPTX
Sprouting into the world of Elm
PDF
Beyond JavaScript Frameworks: Writing Reliable Web Apps With Elm - Erik Wende...
PDF
Erik Wendel - Beyond JavaScript Frameworks: Writing Reliable Web Apps With El...
PDF
Play with elm - Choucri fahed, Finstack - Lambadays
PDF
Elm @ DublinJS
PDF
Elm dev front-end
PDF
Elm: frontend code without runtime exceptions
PDF
Rethink Frontend Development With Elm
PDF
What is Elm and Why Should I Care?
PPTX
Your first Elm program
PDF
Play with Elm!
PPTX
Elm - Could this be the Future of Web Dev?
PDF
What About Elm?
PDF
Introduction to elm
PDF
Elm architecture
PDF
Phoenix with Elm
PDF
Rethink Frontend Development With Elm
PDF
Elm a possible future for web frontend
PDF
Forward JS 2017 | SF | Write applications as State Machines
PDF
Elm, the runtime error killer
Sprouting into the world of Elm
Beyond JavaScript Frameworks: Writing Reliable Web Apps With Elm - Erik Wende...
Erik Wendel - Beyond JavaScript Frameworks: Writing Reliable Web Apps With El...
Play with elm - Choucri fahed, Finstack - Lambadays
Elm @ DublinJS
Elm dev front-end
Elm: frontend code without runtime exceptions
Rethink Frontend Development With Elm
What is Elm and Why Should I Care?
Your first Elm program
Play with Elm!
Elm - Could this be the Future of Web Dev?
What About Elm?
Introduction to elm
Elm architecture
Phoenix with Elm
Rethink Frontend Development With Elm
Elm a possible future for web frontend
Forward JS 2017 | SF | Write applications as State Machines
Elm, the runtime error killer
Ad

Recently uploaded (20)

PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
Understanding Forklifts - TECH EHS Solution
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Nekopoi APK 2025 free lastest update
PDF
System and Network Administration Chapter 2
PDF
top salesforce developer skills in 2025.pdf
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PPTX
ai tools demonstartion for schools and inter college
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
AI in Product Development-omnex systems
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PPTX
Transform Your Business with a Software ERP System
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
Digital Strategies for Manufacturing Companies
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Operating system designcfffgfgggggggvggggggggg
Reimagine Home Health with the Power of Agentic AI​
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
wealthsignaloriginal-com-DS-text-... (1).pdf
Understanding Forklifts - TECH EHS Solution
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Upgrade and Innovation Strategies for SAP ERP Customers
How to Choose the Right IT Partner for Your Business in Malaysia
Nekopoi APK 2025 free lastest update
System and Network Administration Chapter 2
top salesforce developer skills in 2025.pdf
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
ai tools demonstartion for schools and inter college
Odoo POS Development Services by CandidRoot Solutions
AI in Product Development-omnex systems
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Transform Your Business with a Software ERP System
VVF-Customer-Presentation2025-Ver1.9.pptx
Digital Strategies for Manufacturing Companies

SEMjs - Planting Seeds with Elm

  • 3. SoutheastMichiganJavaScript–PlantingSeedswithElm December11,2017 3  Functional language for the browser  Strongly and statically typed  Compiles to JavaScript  ML style language, written in Haskell  Declarative syntax What is Elm?
  • 5. SoutheastMichiganJavaScript–PlantingSeedswithElm December11,2017 5  A beginner friendly language with a minimal but powerful set of features  A quick and kind compiler that provides type inference  Advertises “No” runtime exceptions  An architecture designed for the language  Fully interoperable with JavaScript  Enforces semantic versioning  A helpful and friendly community  A really nice formatter that eliminates the need for a linter What makes Elm special?
  • 6. SoutheastMichiganJavaScript–PlantingSeedswithElm December11,2017 6  A quick and kind compiler that provides type inference What makes Elm special?
  • 7. SoutheastMichiganJavaScript–PlantingSeedswithElm December11,2017 7  A quick and kind compiler that provides type inference What makes Elm special?
  • 8. SoutheastMichiganJavaScript–PlantingSeedswithElm December11,2017 8  A note on type inference:  Type inference allows the compiler to “infer” the types in your app without you explicitly defining them.  For instance, the following function definition examples are both valid:  In spite of this, it’s a good practice to provide the type annotations to improve readability and general code quality. What makes Elm special? or
  • 9. SoutheastMichiganJavaScript–PlantingSeedswithElm December11,2017 9  A quick and kind compiler that provides type inference  There are a few helpful files here that may be useful to read before you run into compiler errors: https://github.com/elm-lang/elm-compiler/tree/master/hints What makes Elm special?
  • 10. SoutheastMichiganJavaScript–PlantingSeedswithElm December11,201710  Advertises “No” runtime exceptions From the Elm website: “Unlike hand-written JavaScript, Elm code does not produce runtime exceptions in practice. Instead, Elm uses type inference to detect problems during compilation and give friendly hints. This way problems never make it to your users. NoRedInk has 80k+ lines of Elm, and after more than a year in production, it still has not produced a single runtime exception.” This article has more insight on the subject: http://elm-lang.org/blog/compilers-as-assistants What makes Elm special?
  • 11. SoutheastMichiganJavaScript–PlantingSeedswithElm December11,201711  Advertises “No” runtime exceptions From personal and professional experience, I have experienced this firsthand. Clarity has had 5 projects in production over the past 18 months and these stats still hold true. What makes Elm special? This article has more insight on the subject: http://elm-lang.org/blog/compilers-as-assistants
  • 13. SoutheastMichiganJavaScript–PlantingSeedswithElm December11,201713  The logic of every Elm program will break up into three cleanly separated parts:  Model — the state of your application (a single state atom)  Update — a way to update your state  View — a way to view your state as HTML The Elm Architecture View Model Update
  • 16. SoutheastMichiganJavaScript–PlantingSeedswithElm December11,201716  After compilation, the code below produces a simple counter app A simple example…
  • 18. SoutheastMichiganJavaScript–PlantingSeedswithElm December11,201718  This is a walkthrough of code I modified from the original HTTP example from the Elm site: https://ellie-app.com/4fgzBGyFzcYa1/0  Original example: http://elm-lang.org/examples/http An example of The Elm Architecture
  • 19. SoutheastMichiganJavaScript–PlantingSeedswithElm December11,201719  Elm uses modules as method to split up code  The code (..) exposes everything to the consuming module.  Consider exposed functions your “public” API for a module  Anything not exposed is “private” to the module.  Next, we move on to our application’s imports A module system
  • 20. SoutheastMichiganJavaScript–PlantingSeedswithElm December11,201720  The import statement tells the compiler that we want to bring something into our module to use. An app without imports is extremely rare, as a matter of fact, even the most basic “Hello, World!” example has an import: For our application, we need to use some standard libraries (ex: Html*) as well as some non-standard ones (ex: RemoteData) : Similar to how module’s are defined, imports let us choose which functions to expose to our application via the exposing keyword. Again (..) here means we’re bringing everything the imported module expose into the local namespace. You can also alias imported modules using the as keyword A module system
  • 21. SoutheastMichiganJavaScript–PlantingSeedswithElm December11,201721  Code that is similar to what you’ll see in most Elm applications:  The program’s execution will start in the main function and continue from there. Record value assignment is done using the = operator (ex: init = init "cats") as in many languages. Here we are passing in an initialization function for our program’s init to run upon startup. This function should transform any inputs (in this case “cats”) into an initial state for our application.  In our case, the init function does this by taking a String as an input and emitting a (Model, Cmd Msg) Setting up the application
  • 22. SoutheastMichiganJavaScript–PlantingSeedswithElm December11,201722  More on the program module...  program is a function of the Html module in the Elm language core (http://package.elm-lang.org/packages/elm-lang/html/2.0.0/Html). This module also exposes a couple of other handy functions for starting a your program (as well as others related to Html) that you may want to use based on what your application is doing.  The main three types of programs exposed by the Html module are:  Html.beginnerProgram  Html.program  Html.programWithFlags Setting up the application
  • 23. SoutheastMichiganJavaScript–PlantingSeedswithElm December11,201723  Html.beginnerProgram can be used if your application does not have any side effects that need to occur outside the input and Dom events. Basically, this version of Program does not allow for adding asynchronous commands, subscriptions etc. (this includes Tasks) input from DOM Events  Html.program can be used if your application needs access to run commands or use subscriptions (this includes interop with JavaScript). Not every application needs this but this is likely to be the most common version of Program used.  Html.programWithFlags allows for all of the functionality of Html.program but can also consume inputs from the JavaScript environment at startup. Some common uses are to pull values from localStorage upon loading the page. Setting up the application
  • 24. SoutheastMichiganJavaScript–PlantingSeedswithElm December11,201724  The Model in Elm is a single state atom of your application’s state. Here is the Model for the example application:  This Model encompasses the entire “state” portion of my application. As seen in a previous slide, the init function is used to setup the initial state of this Model when the application starts.  A Model can pretty much be of any type, but as your application grows, most of the time an Elm “Record” (http://elm-lang.org/docs/records) is the appropriate storage mechanism for this state. The Model
  • 25. SoutheastMichiganJavaScript–PlantingSeedswithElm December11,201725  One of the features of Elm are it’s Union Types, these are sometimes called “tagged unions” or “ADTs” in other languages.  A common use of Union Types in Elm is to setup the message structure for an application. Below is example of how Union Types are used in the example application:  More on Union Types can be found here: https://guide.elm-lang.org/types/union_types.html Union Types and modeling data
  • 26. SoutheastMichiganJavaScript–PlantingSeedswithElm December11,201726  A more complex union type example:  Referenced from RemoteData package by Kris Jenkins: http://package.elm-lang.org/packages/krisajenkins/remotedata/latest Union Types and modeling data
  • 27. SoutheastMichiganJavaScript–PlantingSeedswithElm December11,201727  As I’ve used Elm more, I’ve found that Union Types are effective way to model pretty much anything. For instance, let’s say you have a toggle button and you want to represent either on ”On” or “Off” state:  For something as simple as the last example, you could just use a value of type Bool which, just so happens to be a union type itself.  But what’s nice is that, with the custom union type, you get more expressiveness from the statement, which (generally) makes things easier to read. Union Types and modeling data
  • 28. SoutheastMichiganJavaScript–PlantingSeedswithElm December11,201728  In practice, union types allow for actions based on a pattern match which we’ll see in the next slide as we move on to the update function! Union Types and modeling data
  • 29. SoutheastMichiganJavaScript–PlantingSeedswithElm December11,201729  Now that we’ve established that our Model holds the state for our entire application, how to we update it? That’s where the update function comes into play.  Here is the update function for the example application:  The type definition for update implies that it takes two arguments a Msg and a Model. It then emits a tuple in the form of (Model, Cmd Msg) based on those inputs. Updating State
  • 30. SoutheastMichiganJavaScript–PlantingSeedswithElm December11,201730 Updating State In this case, pattern matching is being used to determine what should be returned by the update function (how the model should change based on the current model) as well as any additional commands that may need to be run based on the condition. This is a fairly standard thing to see in an Elm update function However, as an application scales, this section can grow and it may be beneficial to break out some of the smaller blocks of execution into their own function (or even their own module) Update the Model Schedule a Cmd to be run upon the next cycle of the Elm runtime loop (after the View is rendered) Update the Model In this case, we don’t need to run Any additional commands, just process the response so we use Cmd.none
  • 31. SoutheastMichiganJavaScript–PlantingSeedswithElm December11,201731  Your applications View code defines what is displayed to the end user at any given time  A view can also be comprised of other functions that serve as sub views or partial views.  As with all Elm code, view is just a pure function and starts with a type and function definition  The type definition here says that this function takes in a Model and emits an Html Msg The View
  • 32. SoutheastMichiganJavaScript–PlantingSeedswithElm December11,201732  Elm has let statements which allow you to define variables that are local to the scope between let and in.  In the code below, let..in is being used to assign multiple variables (ex: waitingGifUrl, buttonText etc.) at once that will be used in the context below the in keyword.  The code(firstRun, gifUrl) uses destructuring to assign multiple variables at once based on the case. The View
  • 33. SoutheastMichiganJavaScript–PlantingSeedswithElm December11,201733  Last, but not least, the code below the in keyword is used to declaratively state what should be displayed on the page. Rather than using plain HTML here, Elm uses functions for everything (including HTML).  Most of the Html functions have the following type signature (h1 example): The View
  • 34. SoutheastMichiganJavaScript–PlantingSeedswithElm December11,201734  In order to fetch the data from a remote server, the example application has a function called getRandomGif. This function is in charge of taking in a String as an input and emitting a Cmd Msg  An example of using the pipe-operator from the official site: http://elm-lang.org/examples/pipes/code A simple Http request A new Cmd is scheduled… ImgResponse is the designated message endpoint Using the pipe-operators |> or <| one can pass through the result to the next function where it is used as the last argument for the function it gets piped into. The data can flow in both directions, though the usual way of chaining functions with the pipe operator is using |> and having a single line per function call.
  • 35. SoutheastMichiganJavaScript–PlantingSeedswithElm December11,201735  When we schedule the Http request, one of the arguments we pass into the Http.get function is the Decoder that should be used to parse the resultant JSON.  When we schedule the Http request, one of the arguments we pass into the Http.get function is the Decoder that should be used to parse the resultant JSON.  In this case, we’re just using Decode.at to quickly extract only the data.image_url field from the JSON data (shown below). Decoding the data
  • 36. SoutheastMichiganJavaScript–PlantingSeedswithElm December11,201736  Since everything in Elm is a function, we can easily cleanup our codebase as it grows. For instance, the following three functions were originally part of the let..in block of my view code  As my code grew, it seems intuitive to break those out into their own bite sized chunks of code. This also lends itself well to reuse. Functions everywhere!
  • 37. SoutheastMichiganJavaScript–PlantingSeedswithElm December11,201737  More on scaling and reuse in Elm:  From the official guide: https://guide.elm-lang.org/reuse/  An example of scalable SPA in Elm: https://github.com/rtfeldman/elm-spa-example Scaling and reuse
  • 38. SoutheastMichiganJavaScript–PlantingSeedswithElm December11,201738  In Elm JavaScript interop is done through a pub/sub system called “Ports”. The notion of a Port is that it is a pipe between Elm and JavaScript. The basic flow of ports is shown in the diagram below:  Reference article: https://hackernoon.com/how-elm-ports-work-with-a-picture-just-one-25144ba43cdd  Ports have either an inbound or an outbound flow. An example of an outbound port in Elm looks like the following: port myOutboundPort: String -> Cmd msg  Here we are defining a port that sends one String argument to the native JavaScript code that it can use in it’s function. JavaScript Interop
  • 39. SoutheastMichiganJavaScript–PlantingSeedswithElm December11,201739  We can schedule myOutboundPort to run at either startup of the application (in our init function) or in our update function  Example of scheduling an outbound port to run upon startup:  Example of scheduling an outbound port to run at update: JavaScript Interop
  • 40. SoutheastMichiganJavaScript–PlantingSeedswithElm December11,201740  Inbound ports are handled via subscriptions. To listen for inbound messages, a subscription needs to be setup (in our app the subscriptions function would handle this). We’ll jump into the subscription code on the next slide.  For now, here is the syntax to setup an inbound port:  In the example above DataStore is a type with the same shape as the data we expect to come in. Let’s say we are being sent the following JSON payload:  In this case, DataStore should be defined as follows: JavaScript Interop
  • 41. SoutheastMichiganJavaScript–PlantingSeedswithElm December11,201741  To subscribe (listen) for messages coming in on this port, we need to add a subscription to our subscriptions function: JavaScript Interop When we receive messages from this port the update function will be called with ExecuteInboundPort as the message
  • 42. SoutheastMichiganJavaScript–PlantingSeedswithElm December11,201742  A few notes on subscriptions (Sub) and commands (Cmd) :  If you need to subscribe to multiple endpoints (Phoenix channels, Ports etc.), you can use Sub.batch with a List of subscriptions rather than just listing one.  Similar to Sub.batch, Cmd.batch allows you to schedule multiple commands to run  There are other functions for working with subscriptions and commands, their respective documentation pages are:  Subscriptions: http://package.elm-lang.org/packages/elm-lang/core/5.1.1/Platform-Sub  Commands: http://package.elm-lang.org/packages/elm-lang/core/5.1.1/Platform-Cmd Commands and Subscriptions
  • 44. SoutheastMichiganJavaScript–PlantingSeedswithElm December11,201744  Elm is a generally considered a very easy language to get started with. To get Elm installed, you can visit https://guide.elm-lang.org/install.html and follow the directions.  Installers are available for Windows, Mac and Anywhere (via npm)  Language plugins are available for many popular editors and IDEs including Sublime, VS Code, Atom and JetBrains (WebStorm etc.)  It’s highly recommended that you use elm-format to auto format code upon save (via a watcher). Directions for install and setup are here: https://github.com/avh4/elm-format Getting started with Elm
  • 45. SoutheastMichiganJavaScript–PlantingSeedswithElm December11,201745  Elm is a compiled language and as uses a tool called “elm-make” to compile it’s code from Elm to JavaScript.  One cool feature of the compiler is, by adding the –warn flag, you’ll be alerted to many to unused imports. Similar to the normal compiler issues that cause a failure, these messages are helpful and friendly. Getting started with Elm
  • 46. SoutheastMichiganJavaScript–PlantingSeedswithElm December11,201746  Examples from the official site: http://elm-lang.org/examples  The Official Guide: https://guide.elm-lang.org/  Elm and JS data types and structures compared: http://elm-lang.org/docs/from-javascript  Ellie App (In browser editor): https://ellie-app.com/  Elm in Action (Book): https://www.manning.com/books/elm-in-action  Beginner Elm Tutorial: http://elmprogramming.com/  Elm Town (Podcast): https://elmtown.github.io/  Podcast focused on learning Elm when coming from a JS background: http://jstoelm.com/  Awesome Elm (collection of resources and libraries): https://github.com/isRuslan/awesome-elm Resources
  • 47. SoutheastMichiganJavaScript–PlantingSeedswithElm December11,201747  Elm Slack Team: https://elmlang.herokuapp.com/  Reddit: https://www.reddit.com/r/elm/  Twitter: https://twitter.com/elmlang  Google Mailing List: https://groups.google.com/forum/?fromgroups#!forum/elm-discuss  Discourse (possibly replacing Google mailing list): https://discourse.elm-lang.org/ Community
  • 48. SoutheastMichiganJavaScript–PlantingSeedswithElm December11,201748 Local Elm Meetup (Elm Detroit): https://www.meetup.com/elm-detroit/ We meet on the first Thursday of every month at 7pm. Our next event is on January 4th. The event starts out with about a half an hour of social time and then offers two main tracks:  Open space coding/discussions  A somewhat informal workshop and Q/A session for folks interested in learning more about Elm and how to build stuff. We use this curriculum https://github.com/Elm-Detroit/elm-workshop for the workshop. Community