Anyone ever mess with Elixir?

Elixir is a programming language that stems from Erlang. Erlang is traditionally one of the more scable languages, and has powered the Telecom industry for years.

It’s a functional language that’s pretty fun / easy to write. It has some more complicated constructs in the form of OTP, but to get started it’s super easy. The syntax is very familiar to Ruby, but that’s where the similarities end :slight_smile:

One of my favorite things is pattern matching

# In this example a User has been defined as 
# %User{name: "John Doe", age: 42, other_attribute: "something"}
# This allows you to grab only what you need, for example the users name
%User{name: name} = User.get("John Doe")
name #=> "John Doe"

# This makes writing functions way funner, because you only have to worry 
# about when the input is what you expect
def serve_drinks(%User{age: age}) when age >= 21 do
  # Code that serves drinks!
end
def serve_drinks(%User{age: age}) do
  # Don't serve drinks!
end

serve_drinks(User.get("John Doe"))
#=> Fails if the user is under 21
1 Like

I’m going to have to try it … very interesting …
I decided to see how close I could come in PHP haha

<?php

// php 7.2

$user = ["name" => "John Doe", "age" => 42, "other_attribute" => "something"];

["name" => $name] = $user;
// $name is "John Doe";

function serve_drinks($user) { return $user['age'] >= 21 ?(function($age){
	print "Code that serves drinks! Your age is $age\n";
})($user['age']): serve_drinks_under($user['age']); }

function serve_drinks_under($age) {
	print "Don't serve drinks! Your age is $age\n";
}

serve_drinks($user);

1 Like

Here’s one of my favorite things,

mp3_byte_size = (byte_size(binary) - 128)
<< _ :: binary-size(mp3_byte_size), id3_tag :: binary >> = binary

<< "TAG",
    title   :: binary-size(30), 
    artist  :: binary-size(30), 
    album   :: binary-size(30), 
    year    :: binary-size(4), 
    comment :: binary-size(30), 
    _rest   :: binary >> = id3_tag

Reads a MP3 file, separating out each attribute and giving you the rest.

Source: http://benjamintan.io/blog/2014/06/10/elixir-bit-syntax-and-id3/

Oh and here’s some other media https://blog.discordapp.com/scaling-elixir-f9b8e1e7c29b

How well does it deal with structures with different fields of differing sizes based on the content of another field?

If that question makes absolutely no sense, I am talking about dealing with the Windows PE executable file format.

https://en.wikibooks.org/wiki/X86_Disassembly/Windows_Executable_Files

I’m learning Elixir now. Recently bought Dave Thomas’s Elixir for Programmer’s course. I’m considering teaching a class at DMS on Ruby’s functional aspects before I ramp up.