Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

FAQ

Martin Gutsch edited this page · 28 revisions

1. What are the main problem domains Elixir is suitable for?

In short:

  • data processing
  • network related tasks: from plain sockets to web servers and frameworks
  • writing reliable, distributed, and highly available software.

2. Can Elixir be used to write programs that rely heavily on fast numerical computations?

Elixir will most likely show worse performance compared to a compiled imperative language when performing a strictly sequential (CPU) or uniformly parallel (GPU) computation. However, there are lots of use-cases where Erlang's concurrency features (that Elixir takes full advantage of) might be put to good use in mathematical computation contexts.

Here are a few resources on the subject that describe technical computing in Erlang (applies to Elixir as well).

  • From Telecom Networks to Neural Networks; Erlang, as the unintentional Neural Network Programming Language [video]
  • High-performance Technical Computing with Erlang [slides]
  • When does Erlang's parallelism overcome its weaknesses in numeric computing? [stackoverflow.com]
  • Go, F# and Erlang -- implementation of matrix multiplication and prime number test in each of the three languages [paper]

3. How do I start a shell (IEx) with my project and all its dependencies loaded and started?

iex -S mix inside your project's directory.

3.1. How to have my IEx session history to be persistent over different IEx sessions?

There's a very nice module [erlang-history] that does the trick for both elixir and erlang shells.

4. Why is my list of integers printed as a string?

For example:

iex(1)> [27, 35, 51]
'\e#3'

Pretty-printing of lists is done by using Erlang's native function. It is designed to print lists as strings when all elements of the list are valid ASCII codes.

You can avoid this by appending 0 to your list, or by disabling char list printing:

iex(2)> [27, 35, 51] ++ [0]
[27, 35, 51, 0]

iex(3)> inspect [27, 35, 51], char_lists: false
"[27,35,51]"

5. Which functions are allowed in guards?

The Erlang VM only allows a limited set of expressions in guards:

  • comparison operators (==, !=, ===, !==, >, <, <=, >=)
  • boolean operators (and, or) and negation operators (not, !)
  • arithmetic operators (+, -, *, /)
  • <> and ++ as long as the left side is a literal
  • the in operator

  • all the following type check functions:

    • is_atom/1
    • is_binary/1
    • is_bitstring/1
    • is_boolean/1
    • is_float/1
    • is_function/1
    • is_function/2
    • is_integer/1
    • is_list/1
    • is_map/1
    • is_number/1
    • is_pid/1
    • is_port/1
    • is_reference/1
    • is_tuple/1
  • plus these functions:

    • abs(number)
    • bit_size(bitstring)
    • byte_size(bitstring)
    • div(integer, integer)
    • elem(tuple, n)
    • hd(list)
    • length(list)
    • map_size(map)
    • node()
    • node(pid | ref | port)
    • rem(integer, integer)
    • round(number)
    • self()
    • tl(list)
    • trunc(number)
    • tuple_size(tuple)

Keep in mind errors in guards do not leak but simply make the guard fail:

iex> hd(1)
** (ArgumentError) argument error
    :erlang.hd(1)
iex> case 1 do
...>   x when hd(x) -> "Won't match"
...>   x -> "Got: #{x}"
...> end
"Got 1"
Something went wrong with that request. Please try again.