forked from elixir-lang/elixir-lang.github.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
175 lines (131 loc) · 7.09 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
---
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’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’s write a simple test case using <a href="https://hexdocs.pm/ex_unit/">Elixir’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’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’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>, <a href="http://basho.com">Basho</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>