Elm on Rails

Elm on Rails

A quick overview of Elm and comparison with Ruby (including how to write better Ruby code). Instructions are provided on how to easily configure your Ruby on Rails app with Elm so you can be up and running in only a few minutes!

E0e036f89c14b3e59640318eedf9670b?s=128

Brooke Kuhlmann

March 28, 2020
Tweet

Transcript

  1. Elm on Rails Presented by Brooke Kuhlmann +

  2. Overview • A functional language that compiles to JavaScript. •

    A strong focus on simplicity with a concise syntax. • No runtime exceptions (no null or undefined)! • Has a friendly, time traveling, debugger! • Runs on Rails (via the Webpacker gem)! https://elm-lang.org
  3. The Benefits of Elm A brief comparison between Elm and

    Ruby. https://elm-lang.org/docs/syntax
  4. Elm Lists colors = ["red", "black", "white"] List.head colors --

    Result: Just "red" https://elm-lang.org/docs/syntax#lists colors = [] List.head colors -- Result: Nothing Example 1: Example 2:
  5. Ruby Arrays colors = ["red", "black", "white"] colors.first # Result:

    "red" https://ruby-doc.org/core-2.6.3/Array.html colors = [] colors.first # Result: nil Example 1: Example 2:
  6. Elm Maybe -- The type definition. type Maybe a =

    Just a | Nothing https://package.elm-lang.org/packages/elm/core/latest/Maybe ℹ No equivalent in Ruby.
  7. Elm Records -- Initial record. planet = {name = "Earth",

    isHabitable = True} https://elm-lang.org/docs/syntax#records -- Updated record. updatedPlanet = {planet | isHabitable = False} -- Result: {name = "Earth", isHabitable = False} ℹ All records are immutable by default.
  8. Ruby Hashes # Initial hash. planet = {name: "Earth", habitable:

    true} https://ruby-doc.org/core-2.6.3/Hash.html # Mutatation! planet[:name] = "Mars" planet.merge! name: "Mars" # Result {name: "Mars", :habitable: true}
  9. Ruby Hashes ⭐ # Initial hash. planet = {name: "Earth",

    habitable: true} https://ruby-doc.org/core-2.6.3/Hash.html # No mutation. updatedPlanet = planet.merge name: "Mars" # Planet: {name: "Earth", :habitable: true} # Updated planet: {name: "Mars", :habitable: true}
  10. Elm Type Aliases -- MODEL type alias Planet = {

    name: String, isHabitable: Bool } -- RECORD (function) initialPlanet : Planet initialPlanet = { name = "Planet", isHabitable = True } https://elm-lang.org/docs/syntax#type-aliases
  11. Ruby Contracts gem install contracts https://github.com/egonSchiele/contracts.ruby Contract String => String

    => String def label prefix, message "#{prefix}: #{message}" end ℹ Ruby 3.x.x might support this. Requires a gem... ...which then allows for type checking:
  12. Elm If Expression https://elm-lang.org/docs/syntax#conditionals -- Record: planet = {name =

    "Earth", isHabitable = True} -- Conditional: if planet.isHabitable then "habitable" else "inhabitable" -- Result: "habitable"
  13. Ruby If Statement # Hash: planet = {name: "Earth", habitable:

    true} # Conditional: if planet[:habitable] "habitable" end # Result: "habitable" http://www.rubyist.net/~slagell/ruby/control.html ℹ Has potential to answer a nil.
  14. Ruby If Statement ⭐ # Hash: planet = {name: "Earth",

    habitable: true} # Conditional: if planet[:habitable] "habitable" else "inhabitable" end # Result: "habitable" http://www.rubyist.net/~slagell/ruby/control.html Can be enforced by Rubocop.
  15. Elm Custom Types -- Single line. type Status = Terraforming

    | Habitable | Barren -- Multiple lines. type Status = Terraforming | Habitable | Barren https://elm-lang.org/docs/syntax#custom-types ℹ No Ruby equivalent.
  16. Elm Case Expression toSentence : Planet -> String toSentence planet

    = case planet.status of Terraforming -> "Under construction." Habitable -> "Life is good, stop by!" Barren -> "The humans are dead." https://elm-lang.org/docs/syntax#conditionals ℹ A `_ ->` branch can be used as a catch all (in some situations). planet = {name = "Earth", status = Habitable}
  17. Ruby Case Statement case planet.status when :terraforming "Under construction." when

    :habitable "Life is good, stop by!" when :barren "The humans are dead." end http://www.rubyist.net/~slagell/ruby/control.html Planet = Struct.new :name, :status planet = Planet.new "Earth", :habitable
  18. Ruby Case Statement ⭐ case planet.status when :terraforming "Under construction."

    when :habitable "Life is good, stop by!" when :barren "The humans are dead." else fail "Unknown planet status." end http://www.rubyist.net/~slagell/ruby/control.html Can be enforced by Rubocop.
  19. Elm Functions add : Int -> Int -> Int add

    first second = first + second https://elm-lang.org/docs/syntax#functions add 2 5 -- Result: 7
  20. Ruby Methods def add first, second first + second end

    https://www.rubyist.net/~slagell/ruby/methods.html add 2, 5 -- Result: 7
  21. Ruby Methods (caveats) # Example 1: def add first =

    0, second = 0 first + second end https://www.rubyist.net/~slagell/ruby/methods.html # Example 2: def add first, second return unless first && second first + second end # Example 3: def add first, second Integer(first) + Integer(second) end
  22. Summary • Immutable by default! • Strong types! • No

    runtime exceptions! • No nil! The Magic Tricks of Testing by Sandi Metz (https://www.youtube.com/watch?v=URSWYvyc42M)
  23. Elm on Rails Demo!

  24. The Elm Debugger Demo! (with a Rails API)

  25. Elm Documentation Getting Started • An Introduction to Elm (online

    book) • Functional Programming in Elm (online book, partially complete) Read in order listed. Diving Deeper • Elm in Action by Richard Feldman - Book. $40. • Building Web Apps with Elm by The Pragmatic Studio - Screencasts. $50. • Integrating Elm by The Pragmatic Studio - Screencasts. Free. https://elm-lang.org/docs
  26. Elm Community • Slack - Find us in the #boulder

    channel. • Elm Meetup Group - Held at Pivotal Labs. https://elm-lang.org/community
  27. Thanks! https://www.alchemists.io