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:
functionsayHello(name){console.log(`Hello, my name is ${name}`)}functionPerson(name){this.name=name;}Person.prototype.introduce=function(){sayHello(this.name)}Person.prototype.getName=function(){returnthis.name}functionintroduceUsingFP(person){sayHello(person.getName())}varharryPotter=newPerson("Harry Potter");#OOPharryPotter.introduce();// Hello, my name is Harry Potter#FPintroduceUsingFP(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.
defmodulePersondodefstruct[:name]defintroduce(%Person{}=person)doIO.puts("Hello, my name is "<>person.name)endendharry_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.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
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:
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 usingdefstruct
. It's best-practice to then implement functions in this module which accept an instance of the struct, and do something to/with it.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 updatedEcto.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.