为什么要学习Elixir?答案很简单,为了更好的学习Erlang.这么无厘头的理由?
Erlang语法设计几乎没有考虑过取悦开发者,所以学习之初的门槛略高.对于已经克服了最初语法障碍的同学,Elixir其实没有什么吸引力. 在Elixir之前已经有很多类似的项目,比如http://lfe.github.io Elixir类似思路的还有http://reia-lang.org 在前,但Elixir显然做得更好一些.看这些语言的时候,会有一种感觉:把这语言语法层面做优化调整,理想状态就是Erlang的样子吧!
那为什么要投入时间到Elixir? 深入Elixir内部,可以看到很多非常棒的设计,这些设计并不陌生,之前散见于各种开源项目.不过现在Elixir将这些解决方案整合在一起,而且更具有系统性.对我来说,这是一个很好的切入点.对于很多Ruby开发者来讲,Elixir具有很强的语法亲和力,而这点对我来讲没有什么意义,其实我反倒觉得Elixir在语法层面复杂了很多,需要"记住"的规则比较多,而Erlang这方面要精简很多.
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).
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
(ns
elixir-demo )(defprotocol
Concatenatable (cat
[this other]))(extend-type
java.util.List Concatenatable (cat
[this other] (concat
this other))
)(extend-type
String Concatenatable (cat
[this other] (.concat
this other))
)(println (cat
[1,2,3,4] [5,6,7]))(println (cat
"Hello
" "World!!!!" )) |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
public static class TypeExtensions { public static void Dump(this int num) { Console.WriteLine(string.Format("The
int number is :{0} \r\n ",
num)); } public static void Dump(this double num) { Console.WriteLine(string.Format("The
float number is :{0} \r\n ",
num)); } public static void Dump<T>(this IEnumerable<T>
items) { StringBuilder
sb = new StringBuilder("Data:"); foreach (var item
in items) { sb.AppendFormat("{0}\n",
item); } Console.WriteLine(sb.ToString()); } } |
|
1
2
3
4
5
6
7
8
|
static void Main(string[]
args) { 12.Dump(); (12.23).Dump(); (new List<string>()
{ "123",
"ok",
"test" }).Dump<string>(); Console.ReadLine(); } |
|
1
2
3
|
defprotocol
String.Chars do def
to_string(thing)end |
|
1
2
3
4
5
6
7
8
9
10
11
|
defimpl
String.Chars,
for:
Integer do def to_string(thing)
do integer_to_binary(thing) endenddefimpl
String.Chars,
for:
Float do def to_string(thing)
do iolist_to_binary(:io_lib_format.fwrite_g(thing)) endend |
|
1
2
3
4
5
6
7
8
9
10
11
12
|
iex(8)>
to_string([1,2])<<1,
2>>iex(9)>
to_string(:a)
"a"iex(10)>
to_string(12.3)"12.3"iex(11)>
to_string(12)
"12"iex(12)>
to_string({1,2})**
(Protocol.UndefinedError) protocol String.Chars
not implemented
for {1,
2} /data2/src_elixir/elixir/lib/elixir/lib/string/chars.ex:3:
String.Chars.impl_for!/1 /data2/src_elixir/elixir/lib/elixir/lib/string/chars.ex:17:
String.Chars.to_string/1 |

[Erlang 0112] Elixir Protocols
原文:http://blog.csdn.net/ligaorenvip/article/details/39226519