Elixir (programming language)
![]() |
|
Paradigm | multi-paradigm: functional, concurrent, distributed, process-oriented |
---|---|
First appeared | 2011 |
Stable release | 1.9.2 / 11 October 2019[1] |
Typing discipline | dynamic, strong, duck |
Platform | Erlang |
License | Apache License 2.0[2] |
Filename extensions | .ex, .exs |
Website | elixir-lang |
Influenced by | |
Clojure, Erlang, Ruby | |
Influenced | |
LFE |
Elixir is a functional, concurrent, general-purpose programming language that runs on the Erlang virtual machine (BEAM).[3] Elixir builds on top of Erlang and shares the same abstractions for building distributed, fault-tolerant applications. Elixir also provides productive tooling and an extensible design. The latter is supported by compile-time metaprogramming with macros and polymorphism via protocols.[4]
Elixir is used by companies such as PagerDuty[5], Discord[6], E-MetroTel[7], Pinterest[8], Moz[9], Bleacher Report[10], The Outline[11], Inverse[12] and Divvy[13], and for building embedded systems.[14][15] The community organizes yearly events in the United States,[16] Europe[17] and Japan[18] as well as minor local events and conferences.[19][20]
Contents
History
José Valim is the creator of the Elixir programming language, a research and development project of Plataformatec. His goals were to enable higher extensibility and productivity in the Erlang VM while keeping compatibility with Erlang's ecosystem.[21][22]
On July 12, 2018, Honeypot released a mini-documentary on Elixir.[23]
Versioning
Elixir follows Semantic Versioning and has only 1 major version with no plans for a second. Each of the minor versions supports a specific range of Erlang/OTP versions.[24]
Features
- A language that compiles to bytecode for the Erlang Virtual Machine (BEAM)[25]
- Everything is an expression[25]
- Erlang functions can be called from Elixir without run time impact, due to compilation to Erlang bytecode, and vice versa
- Meta programming allowing direct manipulation of abstract syntax tree (AST)[25]
- Polymorphism via a mechanism called protocols. Like in Clojure, protocols provide a dynamic dispatch mechanism. However, this is not to be confused with multiple dispatch as Elixir protocols dispatch on a single type.
- Support for documentation via Python-like docstrings in the Markdown formatting language[25]
- Shared nothing concurrent programming via message passing (Actor model)[26]
- Emphasis on recursion and higher-order functions instead of side-effect-based looping
- Lightweight concurrency utilizing Erlang's mechanisms[25]
- Railway oriented programming via the
with
construct - Built-in tooling for managing dependencies, code compilation, running tests, formatting code, remote debugging and more
- Lazy and async collections with streams
- Pattern matching[25] to promote assertive code[27]
- Unicode support and UTF-8 strings
Examples
The following examples can be run in an iex
shell or saved in a file and run from the command line by typing elixir <filename>
.
Classic Hello world example:
iex> IO.puts("Hello World!")
Hello World!
Comprehensions
iex> for n <- [1,2,3,4,5], rem(n, 2) == 1, do: n*n
[1, 9, 25]
Pattern Matching (destructuring)
iex> [1, a] = [1, 2]
iex> a
2
iex> {:ok, [hello: a]} = {:ok, [hello: "world"]}
iex> a
"world"
Pattern Matching (multiple clauses)
iex> case File.read("path/to/file") do
iex> {:ok, contents} -> IO.puts("found file: #{contents}")
iex> {:error, reason} -> IO.puts("missing file: #{reason}")
iex> end
Pipe Operator
iex> "1" |> String.to_integer() |> Kernel.*(2)
2
Modules
defmodule Fun do
def fib(0), do: 0
def fib(1), do: 1
def fib(n), do: fib(n-2) + fib(n-1)
end
Sequentially spawning a thousand processes
for num <- 1..1000, do: spawn fn -> IO.puts("#{num * 2}") end
Asynchronously performing a task
task = Task.async fn -> perform_complex_action() end
other_time_consuming_action()
Task.await task
Noteworthy Elixir projects
- Mix is a build automation tool that provides tasks for creating, compiling, and testing Elixir projects, managing its dependencies, and more.[28]
- Phoenix is a web development framework written in Elixir which implements the server-side Model View Controller (MVC) pattern.[29]
- Nerves is a platform, framework, and tooling environment for building embedded systems and devices.[15][30]
- Ecto is the database wrapper and query generator for Elixir.[31]
See also
References
<templatestyles src="Reflist/styles.css" />
Cite error: Invalid <references>
tag; parameter "group" is allowed only.
<references />
, or <references group="..." />
External links
- ↑ Lua error in package.lua at line 80: module 'strict' not found.
- ↑ Lua error in package.lua at line 80: module 'strict' not found.
- ↑ Lua error in package.lua at line 80: module 'strict' not found.
- ↑ Lua error in package.lua at line 80: module 'strict' not found.
- ↑ Lua error in package.lua at line 80: module 'strict' not found.
- ↑ Lua error in package.lua at line 80: module 'strict' not found.
- ↑ Lua error in package.lua at line 80: module 'strict' not found.
- ↑ Lua error in package.lua at line 80: module 'strict' not found.
- ↑ Lua error in package.lua at line 80: module 'strict' not found.
- ↑ Lua error in package.lua at line 80: module 'strict' not found.
- ↑ Lua error in package.lua at line 80: module 'strict' not found.
- ↑ Lua error in package.lua at line 80: module 'strict' not found.
- ↑ Lua error in package.lua at line 80: module 'strict' not found.
- ↑ Lua error in package.lua at line 80: module 'strict' not found.
- ↑ 15.0 15.1 Lua error in package.lua at line 80: module 'strict' not found.
- ↑ Lua error in package.lua at line 80: module 'strict' not found.
- ↑ Lua error in package.lua at line 80: module 'strict' not found.
- ↑ Lua error in package.lua at line 80: module 'strict' not found.
- ↑ Lua error in package.lua at line 80: module 'strict' not found.
- ↑ Lua error in package.lua at line 80: module 'strict' not found.
- ↑ Lua error in package.lua at line 80: module 'strict' not found.
- ↑ Lua error in package.lua at line 80: module 'strict' not found.
- ↑ Lua error in package.lua at line 80: module 'strict' not found.
- ↑ Lua error in package.lua at line 80: module 'strict' not found.
- ↑ 25.0 25.1 25.2 25.3 25.4 25.5 Lua error in package.lua at line 80: module 'strict' not found.
- ↑ Lua error in package.lua at line 80: module 'strict' not found.
- ↑ Lua error in package.lua at line 80: module 'strict' not found.
- ↑ Lua error in package.lua at line 80: module 'strict' not found.
- ↑ Lua error in package.lua at line 80: module 'strict' not found.
- ↑ Lua error in package.lua at line 80: module 'strict' not found.
- ↑ Lua error in package.lua at line 80: module 'strict' not found.