Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
176 lines (131 sloc) 7.06 KB
---
section: home
layout: default
---
<div class="hfeed">
<div class="hentry post no-border">
<img src="/images/contents/home-code.png" alt="Elixir Sample" class="archive-thumbnail" />
<div class="entry-summary">
<p>Elixir is a dynamic, functional language designed for building scalable and maintainable applications.</p>
<p>Elixir leverages the Erlang VM, known for running low-latency, distributed and fault-tolerant systems, while also being successfully used in web development and the embedded software domain.</p>
<p>To learn more about Elixir, check our <a href="/getting-started/introduction.html">getting started guide</a> and our <a href="/learning.html">learning page for other resources</a>. Or keep reading to get an overview of the platform, language and tools.</p>
</div>
</div>
<div class="hentry post">
<h3>Platform features</h3>
</div>
<div class="hentry post">
<h4>Scalability</h4>
<div class="entry-summary">
<p>All Elixir code runs inside lightweight threads of execution (called processes) that are isolated and exchange information via messages:</p>
{% highlight elixir %}
current_process = self()
# Spawn an Elixir process (not an operating system one!)
spawn_link(fn ->
send current_process, {:msg, "hello world"}
end)
# Block until the message is received
receive do
{:msg, contents} -> IO.puts contents
end
{% endhighlight %}
<p>Due to their lightweight nature, it is not uncommon to have hundreds of thousands of processes running <i>concurrently</i> in the same machine. Isolation allows processes to be garbage collected independently, reducing system-wide pauses, and using all machine resources as efficiently as possible (vertical scaling).</p>
<p>Processes are also able to communicate with other processes running on different machines in the same network. This provides the foundation for distribution, allowing developers to coordinate work across multiple nodes (horizontal scaling).</p>
</div>
</div>
<div class="hentry post">
<h4>Fault-tolerance</h4>
<div class="entry-summary">
<p>The unavoidable truth about software running in production is that <i>things will go wrong</i>. Even more when we take network, file systems, and other third-party resources into account.</p>
<p>To cope with failures, Elixir provides supervisors which describe how to restart parts of your system when things go awry, going back to a known initial state that is guaranteed to work:</p>
{% highlight elixir %}
children = [
TCP.Pool,
{TCP.Acceptor, port: 4040}
]
Supervisor.start_link(children, strategy: :one_for_one)
{% endhighlight %}
</div>
</div>
<div class="hentry post">
<h3>Language features</h3>
</div>
<div class="hentry post">
<h4>Functional programming</h4>
<div class="entry-summary">
<p>Functional programming promotes a coding style that helps developers write code that is short, fast, and maintainable. For example, pattern matching allows developers to easily destructure data and access its contents:</p>
{% highlight elixir %}
%User{name: name, age: age} = User.get("John Doe")
name #=> "John Doe"
{% endhighlight %}
<p>When mixed with guards, pattern matching allows us to elegantly match and assert specific conditions for some code to execute:</p>
{% highlight elixir %}
def serve_drinks(%User{age: age}) when age >= 21 do
# Code that serves drinks!
end
serve_drinks User.get("John Doe")
#=> Fails if the user is under 21
{% endhighlight %}
<p>Elixir relies heavily on those features to ensure your software is working under the expected constraints. And when it is not, don&rsquo;t worry, supervisors have your back!</p>
</div>
</div>
<div class="hentry post">
<h4>Extensibility and DSLs</h4>
<div class="entry-summary">
<p>Elixir has been designed to be extensible, letting developers naturally extend the language to particular domains, in order to increase their productivity.</p>
<p>As an example, let&rsquo;s write a simple test case using <a href="https://hexdocs.pm/ex_unit/">Elixir&rsquo;s test framework called ExUnit</a>:</p>
{% highlight elixir %}
defmodule MathTest do
use ExUnit.Case, async: true
test "can add two numbers" do
assert 1 + 1 == 2
end
end
{% endhighlight %}
<p>The <code>async: true</code> option allows <code>test</code>s to run in parallel, using as many CPU cores as possible, while the <code>assert</code> functionality can introspect your code, providing great reports in case of failures. Those features are built using Elixir macros, making it possible to add new constructs as if they were part of the language itself.</p>
</div>
</div>
<div class="hentry post">
<h3>Tooling features</h3>
</div>
<div class="hentry post">
<h4>A growing ecosystem</h4>
<div class="entry-summary">
<p>Elixir ships with a great set of tools to ease development. <a href="https://hexdocs.pm/mix/">Mix is a build tool</a> that allows you to easily create projects, manage tasks, run tests and more:</p>
{% highlight text %}
$ mix new my_app
$ cd my_app
$ mix test
.
Finished in 0.04 seconds (0.04s on load, 0.00s on tests)
1 tests, 0 failures
{% endhighlight %}
<p>Mix is also able to manage dependencies and integrates nicely with the <a href="https://hex.pm/">Hex package manager</a>, which provides dependency resolution and the ability to remotely fetch packages.</p>
</div>
</div>
<div class="hentry post">
<h4>Interactive development</h4>
<div class="entry-summary">
<p>Tools like <a href="https://hexdocs.pm/iex/">IEx (Elixir&rsquo;s interactive shell)</a> are able to leverage many aspects of the language and platform to provide auto-complete, debugging tools, code reloading, as well as nicely formatted documentation:</p>
{% highlight text %}
$ iex
Interactive Elixir - press Ctrl+C to exit (type h() ENTER for help)
iex> h String.trim # Prints the documentation for function
iex> i "Hello, World" # Prints information about the given data type
iex> break! String.trim/1 # Sets a breakpoint in the String.trim/1 function
iex> recompile # Recompiles the current project on the fly
{% endhighlight %}
</div>
</div>
<div class="hentry post">
<h4>Erlang compatible</h4>
<div class="entry-summary">
<p>Elixir runs on the Erlang VM giving developers complete access to Erlang&rsquo;s ecosystem, used by companies like <a href="https://www.heroku.com">Heroku</a>, <a href="https://www.whatsapp.com">WhatsApp</a>, <a href="https://klarna.com">Klarna</a> and many more to build distributed, fault-tolerant applications. An Elixir programmer can invoke any Erlang function with no runtime cost:</p>
{% highlight iex %}
iex> :crypto.hash(:md5, "Using crypto from Erlang OTP")
<<192, 223, 75, 115, ...>>
{% endhighlight %}
<p>To learn more about Elixir, check our <a href="/getting-started/introduction.html">getting started guide</a>. We also have <a href="/docs.html">online documentation available</a> and a <a href="/crash-course.html">Crash Course for Erlang developers</a>.</p>
</div>
</div>
</div>