Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Elm and Ruby

Elm and Ruby

A quick overview of Elm and comparison with Ruby (including how to write better Ruby code).

Brooke Kuhlmann

April 17, 2024
Tweet

More Decks by Brooke Kuhlmann

Other Decks in Programming

Transcript

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

    A strong focus on simplicity with a concise syntax. 🎉 • No runtime exceptions (no null or unde fi ned)! • Has a friendly, time traveling, debugger! elm-lang.org alchemists.io
  2. The Benefits of Elm A brief comparison between Elm and

    Ruby. elm-lang.org/docs/syntax alchemists.io
  3. Elm Lists colors = ["red", "black", "white"] List.head colors --

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

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

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

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

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

    habitable: true} 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} alchemists.io
  9. Elm Type Aliases -- MODEL type alias Planet = {

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

    => String def label prefix, message "#{prefix}: #{message}" end Requires a gem... ...which then allows for type checking: alchemists.io
  11. Elm If Expression elm-lang.org/docs/syntax#conditionals -- Record: planet = {name =

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

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

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

    | Habitable | Barren -- Multiple lines. type Status = Terraforming | Habitable | Barren elm-lang.org/docs/syntax#custom-typess ℹ No Ruby equivalent. alchemists.io
  15. 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." elm-lang.org/docs/syntax#conditionals ℹ A `_ ->` branch can be used as a catch all (in some situations). planet = {name = "Earth", status = Habitable} alchemists.io
  16. Ruby Case Statement 💩 case planet.status when :terraforming "Under construction."

    when :habitable "Life is good, stop by!" when :barren "The humans are dead." end www.rubyist.net/~slagell/ruby/control.html Planet = Struct.new :name, :status planet = Planet.new "Earth", :habitable alchemists.io
  17. 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 www.rubyist.net/~slagell/ruby/control.html 💡Can be enforced by RuboCop. alchemists.io
  18. Elm Functions add : Int -> Int -> Int add

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

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

    0, second = 0 first + second end 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 alchemists.io
  21. Summary 🌠 • Immutable by default! • Strong types! •

    No runtime exceptions! • No nil! www.youtube.com/watch?v=URSWYvyc42M alchemists.io
  22. 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. elm-lang.org/docs alchemists.io