-
Notifications
You must be signed in to change notification settings - Fork 823
/
index.html
166 lines (126 loc) · 6.05 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
---
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 with macros support that leverages Erlang's abilities to build concurrent, distributed, fault-tolerant applications with hot code upgrades.</p>
<p>Elixir also supports polymorphism via protocols (similar to Clojure's), dynamic records, aliases and first-class support to 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">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 ruby %}
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 with 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>With 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 ruby %}
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 write 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 ruby %}
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 only limited to the data types that ships with Elixir. A developer can use his own collections with <code>Enum</code> as long as it implements the <code>Enum.Iterator</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 ruby %}
{ :ok, file } = File.iterator("README.md")
lines = Enum.map(file, fn(line) -> Regex.replace_all(%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 written in markdown using Elixir data structures for accessibility:</p>
{% highlight ruby %}
defmodule MyModule do
@moduledoc """
Documentation for my module
"""
@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
{% endhighlight %}
</div>
</div>
<div class="hentry post">
<h4>Pattern matching</h4>
<div class="entry-summary">
<p>Pattern matching allow developer to easily destructure data and access its contents:</p>
{% highlight ruby %}
{ User, name, age } = User.get("John Doe")
{% endhighlight %}
<p>When mixed with guards, it allows us to easily express our problem:</p>
{% highlight ruby %}
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 ruby %}
:application.start(:crypto)
:crypto.md5("Using crypto from Erlang OTP")
#=> <<192,223,75,115,...>>
{% endhighlight %}
<p>Since Elixir generates the same bytecode, Elixir is fully <a href="http://learnyousomeerlang.com/what-is-otp" target="_blank">OTP</a> compliant and doesn't try to break any of those battle-field tested techniques Erlang is familiar for. Erlang type specifications, behaviors 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">online documentation available</a> and a <a href="/crash-course.html">Crash Course for Erlang developers</a>.</p>
</div>
</div>
</div>