forked from elixir-lang/elixir-lang.github.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
211 lines (159 loc) · 7.73 KB
/
index.html
File metadata and controls
211 lines (159 loc) · 7.73 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
---
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 functional, meta-programming aware language built on top of the Erlang VM. It is a dynamic language with flexible syntax and macro support that leverages Erlang's abilities to build concurrent, distributed and fault-tolerant applications with hot code upgrades.</p>
<p>To install Elixir or learn more about it, check our <a href="/getting_started/1.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>. Or you can just keep on reading for a few code samples!</p>
</div>
</div>
<div class="hentry post">
<h3>Language highlights</h3>
</div>
<div class="hentry post">
<h4>Everything is an expression</h4>
<div class="entry-summary">
{% highlight elixir %}
defmodule Hello do
IO.puts "Defining the function world"
def world do
IO.puts "Hello World"
end
IO.puts "Function world defined"
end
Hello.world
{% endhighlight %}
<p>Running the program above will print:</p>
<pre>
Defining the function world
Function world defined
Hello World
</pre>
<p>This allows a module to be defined in terms of many expressions, programmable by the developer, being the basic foundation for meta-programming. This is similar to what Joe Armstrong (creator of Erlang) proposes in his <a href="https://github.com/joearms/erl2" target="_blank">erl2 project</a>.</p>
</div>
</div>
<div class="hentry post">
<h4>Meta-programming and DSLs</h4>
<div class="entry-summary">
<p>Using expressions and meta-programming, Elixir developers can easily create <a href="http://en.wikipedia.org/wiki/Domain-specific_language">Domain Specific Languages</a>:</p>
{% highlight elixir %}
defmodule MathTest do
use ExUnit.Case
test "can add two numbers" do
assert 1 + 1 == 2
end
end
{% endhighlight %}
<p>DSLs allow a developer to create abstractions for specific domains, often getting rid of boilerplate code.</p>
</div>
</div>
<div class="hentry post">
<h4>Polymorphism via protocols</h4>
<div class="entry-summary">
<p>Protocols allow developers to provide type-specific functionality at chosen extension points. For example, the <code>Enum</code> module in Elixir is commonly used to iterate collections:</p>
{% highlight elixir %}
Enum.map([1,2,3], fn(x) -> x * 2 end) #=> [2,4,6]
{% endhighlight %}
<p>Since the <code>Enum</code> module is built on top of protocols, it is not limited to the data types that ship with Elixir. A developer can use their own collections with <code>Enum</code> as long as they implement the <code>Enumerable</code> protocol. For example, a developer can use all the convenience of the <code>Enum</code> module to easily manipulate a file, line by line:</p>
{% highlight elixir %}
file = File.stream!("README.md")
lines = Enum.map(file, fn(line) -> Regex.replace(~r/"/, line, "'") end)
File.write("README.md", lines)
{% endhighlight %}
</div>
</div>
<div class="hentry post">
<h4>Documentation as first-class citizen</h4>
<div class="entry-summary">
<p>Documentation is supported at the language level in the form of docstrings. Markdown is Elixir's defacto markup language of choice for use in docstrings:</p>
{% highlight elixir %}
defmodule MyModule do
@moduledoc """
Documentation for my module. With **formatting**.
"""
@doc "Hello"
def world do
"World"
end
end
{% endhighlight %}
<p>Different tools can easily access the documentation. For instance, IEx (Elixir's interactive shell) can show the documentation for any module or function with the help of the function <code>h</code>:</p>
{% highlight text %}
iex> h MyModule
# MyModule
Documentation for my module. With **formatting**.
{% endhighlight %}
<p>There is also a documentation generator for Elixir called <a href="https://github.com/elixir-lang/ex_doc">ExDoc</a> that can produce a static site using docstrings extracted from the source code.</p>
</div>
</div>
<div class="hentry post">
<h4>Pattern matching</h4>
<div class="entry-summary">
<p>Pattern matching allows developers to easily destructure data and access its contents:</p>
{% highlight elixir %}
{ User, name, age } = User.get("John Doe")
{% endhighlight %}
<p>When mixed with guards, pattern matching allows us to easily express our intent:</p>
{% highlight elixir %}
def serve_drinks({ User, name, age }) when age < 21 do
raise "No way #{name}!"
end
def serve_drinks({ User, name, age }) do
# Code that serves drinks!
end
serve_drinks User.get("John")
#=> Raises "No way John!" if John is under 21
{% endhighlight %}
</div>
</div>
<div class="hentry post">
<h4>Tooling included</h4>
<div class="entry-summary">
<p>Elixir ships with a great set of tools to ease development. Mix allows you to easily create your new project, manage tasks and dependencies:</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>In the example above, we have created a new project and ran its initial test suite powered by the ExUnit test framework.</p>
</div>
</div>
<div class="hentry post">
<h4>Interactive (and remote) shells</h4>
<div class="entry-summary">
<p>Elixir also ships with an Interactive Shell, called IEx, which provides a great set of helpers for writing code, like easy access to documentation (shown above), code reloading and so on. It is also a great companion for production, as it allows for example connecting to remote nodes. Here is a quick example you can try locally. In one terminal, do:</p>
{% highlight text %}
$ iex --name hello
iex(hello@machine)1> defmodule Hello do
...(hello@machine)1> def world, do: IO.puts "Distributed hello world"
...(hello@machine)1> end
{% endhighlight %}
<p>See the name in between parenthesis starting with hello? Copy that, open up another terminal and pass it to <code>--remsh</code> (short for remote shell):</p>
{% highlight text %}
$ iex --name world --remsh "hello@machine"
iex(hello@machine)1> Hello.world
"Distributed hello world"
{% endhighlight %}
<p>Notice we were able to invoke a function from a module defined in the other terminal! Even the node names are the same. This will work in between machines in the same network as long as they have the same value in the <code>~/.erlang.cookie</code> file!</p>
</div>
</div>
<div class="hentry post">
<h4>Erlang all the way down</h4>
<div class="entry-summary">
<p>After all, Elixir runs in the Erlang VM. An Elixir programmer can invoke any Erlang function with no runtime cost:</p>
{% highlight elixir %}
:application.start(:crypto)
:crypto.md5("Using crypto from Erlang OTP")
#=> <<192,223,75,115,...>>
{% endhighlight %}
<p>Since Elixir compiles to the same bytecode, it is fully <a href="http://learnyousomeerlang.com/what-is-otp" target="_blank">OTP</a> compliant and works seamlessly with all the battle-tested techniques for which Erlang/OTP is famous. Erlang type specifications, behaviours and module attributes are all supported. It is easy to <a href="/crash-course.html#interop">add Elixir to your existing Erlang programs too (including rebar support)</a>!
<p>To install Elixir or learn more about it, check our <a href="/getting_started/1.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>