DEV Community

Austin S. Hemmelgarn
Austin S. Hemmelgarn

Posted on

Any tips for someone just starting out learning Elixir?

I recently decided to start learning Elixir. I've already got a project in mind (I'm going to be converting some existing tools I use regularly on my systems from Python (a language I already have solid experience with) to Elixir since the concurrency model should help them be significantly more efficient), and I've worked through the whole 'Getting Started' guide on the website without any significant issues.

However, it's drastically different from most of what I'm used to (I'm coming from a mostly Python background, with some JS and SH experience), and I'm wondering if anybody might have some advice for people new to the language? Any particular gotcha's to watch out for that aren't well documented? Possibly things that need to be handled significantly differently in Elixir compared to other languages for efficiency reasons?

Top comments (3)

Collapse
 
edisonywh profile image
Edison Yap

Heya welcome to Elixir ๐Ÿ‘‹! I can't really pull any tips out of my head as of right now, but I've compiled a list of resources for you to checkout, so here goes!

P.S: With courtesy from ElixirForum (probably the biggest Elixir community forum?), you can sometimes get up to 40% off the learning resources! Just try to input elixirforum whenever you try to checkout :)

In fact I've updated this to be in the sidebar's wiki of the #elixir tag,

Collapse
 
hugecoderguy profile image
Christian Kreiling

In learning Elixir, it's important to think functionally. You're coming from languages that are traditionally referred to as object-oriented languages, but they still allow you to program functionally.

Functional programming might seem intimidating at first, but it's easiest to think about the difference with this simple JavaScript example:

function sayHello(name) { console.log(`Hello, my name is ${name}`) }

function Person(name) {
  this.name = name;
}

Person.prototype.introduce = function() { sayHello(this.name) }
Person.prototype.getName = function() { return this.name }

function introduceUsingFP(person) { sayHello(person.getName()) }

var harryPotter = new Person("Harry Potter");

# OOP
harryPotter.introduce();
// Hello, my name is Harry Potter

# FP
introduceUsingFP(harryPotter);
// Hello, my name is Harry Potter

Notice that in OOP, you have an object which has data and behavior (the introduce() function). In FP, you have an object with data, but its behavior is not a part of the object.

So in Elixir you'd have a module Person, which defines a struct using defstruct. It's best-practice to then implement functions in this module which accept an instance of the struct, and do something to/with it.

defmodule Person do
  defstruct [:name]

  def introduce(%Person{} = person) do
    IO.puts("Hello, my name is " <> person.name)
  end
end

harry_potter = %Person{name: "Harry Potter"}
Person.introduce(harry_potter)
# Hello, my name is Harry Potter

You will witness this in many libraries in Elixir. Ecto's changeset functions are a great example. All of the validation functions in the Ecto.Changeset module accept an Ecto.Changeset struct and return an updated Ecto.Changeset struct.

My Elixir knowledge really took off after I read the book Programming Elixir โ‰ฅ 1.6: Functional |> Concurrent |> Pragmatic |> Fun. It starts with basic Elixir, but by the end of the book familiarizes you with complex topics such as Open Telecom Platform (OTP) and metaprogramming using macros. I now use it as reference when I need some clarification on concepts ranging in complexity.

Collapse
 
antonrich profile image
Anton

Maybe watch some advent of code hacking from Jose himself
twitch.tv/josevalim/videos?filter=...