Elixir (programming language)
Paradigm | multi-paradigm: functional, concurrent, process-oriented, homoiconic | ||
---|---|---|---|
First appeared | 2012 | ||
1.0.4 | |||
dynamic, strong | |||
Platform | Erlang | ||
License | Apache License | ||
.ex, .exs | |||
Website |
elixir-lang | ||
|
Elixir is a functional, concurrent, general-purpose programming language that runs on the Erlang Virtual Machine (BEAM). Elixir builds on top of Erlang to provide distributed, fault-tolerant, soft real-time, non-stop applications but also extends it to support metaprogramming with macros and polymorphism via protocols.[1]
History
José Valim is the creator of the Elixir programming language, a R&D project of Plataformatec. His goals were to enable higher extensibility and productivity in the Erlang VM while keeping compatibility with Erlang's tools and ecosystem.[2]
Features
- A language that compiles to bytecode for the Erlang Virtual Machine (BEAM)[3]
- Everything is an expression[3]
- 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 AST[3]
- Polymorphism via a mechanism called protocols. Enumerable is an example of a protocol, and is inspired by Clojure reducers[4]
- Support for documentation via Python-like docstrings in the Markdown formatting language[3]
- Shared nothing concurrent programming via message passing (Actor model)
- Emphasis on recursion and higher-order functions instead of side-effect-based looping
- Lightweight concurrency utilizing Erlang's mechanisms with simplified syntax (e.g. Task)[3]
- Lazy and async collections with streams
- Pattern matching[3]
- 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:
IO.puts "Hello World!"
Comprehensions
for n <- [1,2,3,4,5], rem(n,2) == 1, do: n*n #=> [1, 9, 25]
Pattern Matching
[1, a] = [1, 2] # 'a' now equals 2 {:ok, [hello: a]} = {:ok, [hello: "world"]} # 'a' now equals "world"
Modules
defmodule Fun do def fib(0) do 0 end def fib(1) do 1 end def fib(n) do fib(n-2) + fib(n-1) end end
References
External links
- Elixir language website
- Code on GitHub
- Elixir - A modern approach to programming for the Erlang VM video presentation
- Dave Thomas: "Programming Elixir: Functional |> Concurrent |> Pragmatic |> Fun" (book)
- Simon St. Laurent, J. David Eisenberg: "Introducing Elixir" (book)
- Chris McCord: "Metaprogramming Elixir " (book)
- Joe Armstrong: "A Week with Elixir" (blog entry)