What feature would you like to recommend if you share Elixir to a newbie?

(Im very new to Elixir and its also my first functional language I really use.)

I vote for: processes.
They provide an easy and robust way to manage state.
What I have seen so far in other languages was either way beyond me (Haskell with Monads, FRP) oder just global variables (OCaml, F#).

Also I worked with Actors for a long time, so this just feels like home.

1 Like

I’m still not decided on how much I love it, but I have noticed that when I go back to try and write some code in Elm or Rust that I have to reorganize how I think about defining functions for various situations, usually by wrapping the arg in some Enum (Rust) or custom type (Elm) and then using a match or case statement in the body of the function. If I start to get nested matches I really start to appreciate the Elixir pattern. Even though the nested matches are probably a sign that I’m poorly designing my project to start with I can appreciate that Elixir provides a way to keep that code clean.

3 Likes

The thing to me, specifically about pattern matching during function overloading is that it really gives both elixir/erlang it’s own feel.

When I first started learning Erlang I was like “okay, where’s the currying here? why aren’t we doing partial application” as I was coming hot off a tour of duty with OCaml and then it took me about a year to realize that I was reaching for a tool that didn’t need to exist in Erlang (and ultimately Elixir), because it’s just organized in a different way.

I think the thing for me that feels most useful is that you’re clearly defining the boundaries in the function signature, trading explicitness for composability. Granted it’s absolutely a scale on both of those, but Ex/Erl tends to favor the former vs the latter.

3 Likes

excuse the stupid question, but how does pattern matching in function signatures replace partial application?

I am fully aware of that. Same goes for Rebar3 AFAIK.

I never said that it is like Mix, I just said that a lot of languages try to have unified build and dependency management tools, as these have proven their usefulness for the developers (and became bane of the OS package repository maintainers).

1 Like

I’m just thinking when I write OCaml code, the pattern matching exists as the body of the method, not the signature, when we do pattern matching especially with doing partial application in Ex/Erl, it can live in the signature, which allows us to provide iteratables as well that are specific to the information at hand, even though Ex/Erl doesn’t really do partial application. I mean, sorta.

Because y’know (oh god this is gonna be bad bc my brain is broke right now)

def something({:ok, file_type: "json", whatever}), do: fn (x) -> whatever(x) |> JSON.something; end
def something({:ok, file_type: "xml", whatever}), do: fn (x) -> whatever(x) |> to_charlist() |> :xmerl_scan.something; 
end

gives us the ability to apply the function as part of the condition, where x might be a file? some sort of data here. Lazy operating? maybe Instead of doing the conditional inline like

(* i'm not gonna plug this in and figure the type *)
let something file_type whatever =
  match file_type with
    "json" -> whatever |> Yojson.something
    "xml" -> whatever |> xml.something 
;;

You can see that in some like Enum-ish related concepts a lot more that deal with polymorphic types more than anything else.

But it’s not that it replaces necessarily, it just moves the responsibility into the forefront. That was probably the biggest difference for me was that you’re improving clarity by adding relevant data to the method signature instead of putting it in the body.

It’s a code readability thing more than a “it replaces”.

3 Likes

All types of pattern matching, but especially in function definitions.
Also the pipe operator.
Two things I hate not having when I have to use another language.

1 Like

My favourite Elixir features are pattern matching, macros, and tooling (Mix, IEx, ExDoc, etc). I like to share this article: Elixir · How I Start. as it shows some language basics but more importantly supervision trees and distribution.

6 Likes

Same, this made me organize my Rust code by making smaller functions covering part of the case arms. Since Rust can’t do pattern matching in the function heads I end up making those functions with different names – which I’m not sure yet it it’s a good pattern or not.

That’s why I end up wrapping args in Enum variants.

enum Bar {
    BarString(String),
    BarNum(i32)
}

fn foo(bar: Bar) -> Result<i32, std::num::ParseIntError> {
    match bar {
      Bar::BarString(x) => x.parse(),
      Bar::BarNum(y) => Ok(y)
    }
}

Yep, me too, although more often than not I just make functions that describe a part of the logic chain with their suffix.

2 Likes

I think it’s a big advantage that Elixir and Rust had an official build/package manager from early on.

Clojure has Leinengen but it also has Boot and now it has an official sanctioned build tool so it’s a somewhat more complicated situation.

1 Like

It depends on what is meant by “newbie”. I can think of two interpretations:

  1. A “newbie” to Elixir who has programming experience in some mainstream language(s), e.g. Javascript, C++, Python, C#, Java, etc. For such a person, i would show them Processes, GenServers and the IEx shell (with custom .iex.exs for fun). Also use remsh to show them how you can open a shell into a running process, even on another computer.

  2. A “newbie” to programming who is just learning to write code. I don’t know what to show this person. Maybe scaffold a Phoenix app in 2 minutes and show them that? I cannot imagine starting my coding life with Elixir. I feel like Elixir is the language you discover after travelling a difficult road of JS, OOP, .NET, etc. I actually think Javascript is the best language to show someone who is a true “newbie” because you can show them how to open the devtools console and write a “program” to delete the window that asks for your cookies preferences and that’s going to make any normie feel like Mr Robot.

4 Likes

m gonna give remsh a try

The reason why I publish this question is that I want to introduce Elixir to my colleagues. anyway, thanks a lot ! Now I know share what features to them.

1 Like

Things that made me stay:

  1. Pattern matching
  2. Piping
  3. Genserver for cheap running background tasks

I am not sure, if that was already mentioned, I didn’t read this thread thoroughly, but for me, this forum is a BIG plus of the whole Elixir ecosystem. It’s full of very nice and very smart people, that are always willing to help. :slight_smile: :heart:

1 Like

I think Elixir is a really good candidate as a first language for someone. They can learn functional programming from the get go, and won’t have to carry the usual baggage that a lot of programmers that turned to functional programming have to.

3 Likes

I would share two things:

First, one of this two by @pragdave, because they are similar, just the media different


Elixir for Programmers video course

Programming Elixir 1.6 book

The best book and/or video course to be introduced to Functional Programming in Elixir. It was the one that made me the click for my brain to switch from thinking in Object Orientate Programming way to Functional Programming way of doing things.

The Soul of Erlang and Elixir

This talk given by @sasajuric is the best overview that anyone can take to understand the power of the BEAM.

You can see my video notes on it here:

Screenshot from 2021-03-03 21-43-42:

1 Like