Skip to content

Instantly share code, notes, and snippets.

@JoelQ
Created June 15, 2016 19:55
Show Gist options
  • Save JoelQ/8a4eec67ac29b6d42c9ca019ec5c295c to your computer and use it in GitHub Desktop.
Save JoelQ/8a4eec67ac29b6d42c9ca019ec5c295c to your computer and use it in GitHub Desktop.
Raw speaker notes for Intro to Elm talk. Slides at http://www.slideshare.net/thoughtbot/intro-to-elm

What is Elm

Pure Functions

What does that mean?

  • For a given input, you always get the same output
  • The output cannot depend on any external factors (e.g. File on disk, some external variable)
  • Functions cannot do things, only return data
    • Different from imperative model
    • Command/query separation

Following these ideas leads to further ideas:

  • No globals, local state only
  • Immutable data
    • Read only
    • Duplicates when modifying

Static Types

Languages such as Ruby and Javascript have duck typing

  • Pass anything as an argument to a function, the language doesn’t care
  • Will try to call the methods or fail at runtime
  • Javascript will auto-convert, did you want a string?

Elm has static types

You may have found types constraining in the past, waste of time

Elm’s compiler works with you

  • Types are optional with type inference
  • Compiler detects inconsistent code, don’t rely on tests/coworkers
  • Compiler detects missing conditions
  • Compiler has some of the friendliest error messages

Best of all

  • No stack trace
  • No runtime errors!

Better Modeling with Custom Static Types

Not just type safety but also better modeling

How do you model in a dynamic language?

  • Three state boolean with null? (antipattern)
  • String? What if other string is passed in?

Easy to read and understand Elm compiler ensures only one of these three values Elm compiler checks case incomplete case statements

In the Browser

Compiles to javascript and:

  • Possibly CSS
  • Possibly HTML
  • Possibly Canvas

Javascript targeted for now, plans to support web assembly

User Friendly

User-friendlyness is a core value

  • Underpinned by some awesome CS and functional theory
  • Try to be user accessible
  • Avoid “scary” jargon like “Monad”
  • Great compiler messages
  • Keeps simplifying language
  • Avoids custom operators

Handling Nothing

Maybe

Anything can be nil/null in dynamic languages

  • Need to check all the time
  • In practice we don’t
  • Runtime errors

In elm we use Maybe to explicitly tag values that are optional

  • Compiler forces you to check for nothing case
  • Values not wrapped in maybe are guaranteed to be safe
  • No runtime errors!

Side Effects

Effects as Data

  • Functional restrictions
  • All actions can be returned as “command objects” that elm will execute at a later time
  • Handled by the runtime

Random

  • Inherently not pure
  • Random method returns command objects
  • Asynchronous
  • Triggers event you can listen to with result

HTTP

  • HTTP is inherently impure
  • Various HTTP methods return command objects
  • Asyncronous
  • Triggers event you can listen to

Time

  • Current time is inherently non pure
  • We don’t want a command here
  • Subscriptions
  • Functions deal with pure values

Websockets

  • Inherently non pure
  • Sending messages is a command
  • Receiving responses is a subscription

The Elm Architecture

Model

Defining types

View

  • Convert a model to markup
  • Trigger Msg based on DOM events

Update

  • “Steps” model forward in response to a Msg
  • FRP
  • Like React + Redux
  • Time traveling debugger

Components

  • This pattern is infinitely composable
  • Parents delegate to children
  • Children return to their parents
  • Single data source at top

Elm 0.17

Simplified concepts

Used to be a bit convoluted

Better Docs

Questions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment