forked from elixir-lang/elixir-lang.github.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
178 lines (134 loc) · 6.97 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
176
177
178
---
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 是一门动态函数式语言,它是为了创建可扩展,可维护的应用程序而设计的</p>
<p>
Elixir 利用了 Erlang VM,它因为低延迟,分布式,容错而广受赞誉,同时也在 Web开发,嵌入式等领域也正在取得成功。
</p>
<p>想要获得更多关于 Elixir 的信息, 请查看 <a href="/getting-started/introduction.html">入门指南</a>。或者继续阅读下面的内容,以了解整个平台,语言,工具的概览情况。</p>
</div>
</div>
<div class="hentry post">
<h3>平台特性</h3>
</div>
<div class="hentry post">
<h4>可扩展</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 %}
parent = self()
# Spawns an Elixir process (not an operating system one!)
spawn_link(fn ->
send parent, {: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>容错</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 %}
import Supervisor.Spec
children = [
supervisor(TCP.Pool, []),
worker(TCP.Acceptor, [4040])
]
Supervisor.start_link(children, strategy: :one_for_one)
{% endhighlight %}
</div>
</div>
<div class="hentry post">
<h3>语言特性</h3>
</div>
<div class="hentry post">
<h4>函数式编程</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>扩展能力和 DSL</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="/docs/stable/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>工具特性</h3>
</div>
<div class="hentry post">
<h4>一个快速增长的生态环境</h4>
<div class="entry-summary">
<p>Elixir ships with a great set of tools to ease development. <a href="/docs/stable/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>交互式开发</h4>
<div class="entry-summary">
<p>Tools like <a href="/docs/stable/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> c "my_file.ex" # Compiles a file
iex> t Enum # Prints types defined in the module Enum
iex> h IEx.pry # Prints the documentation for IEx pry functionality
iex> i "Hello, World" # Prints information about the given data type
{% endhighlight %}
</div>
</div>
<div class="hentry post">
<h4>Erlang 兼容</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="http://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.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>