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
168 lines (127 loc) · 6.38 KB
/
Copy pathindex.html
File metadata and controls
168 lines (127 loc) · 6.38 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
---
section: home
layout: default
---
<div class="hfeed">
<div class="hentry post">
<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>Elixir also provides first-class support for pattern matching, polymorphism via protocols (similar to Clojure's), aliases and associative data structures (usually known as dicts or hashes in other programming languages).</p>
<p>Finally, Elixir and Erlang share the same bytecode and data types. This means you can invoke Erlang code from Elixir (and vice-versa) without any conversion or performance hit. This allows a developer to mix the expressiveness of Elixir with the robustness and performance of Erlang.</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>.</p>
</div>
</div>
<div class="hentry post">
<h3>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>Erlang all the way down</h4>
<div class="entry-summary">
<p>After all, Elixir is still Erlang. 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>