From bba7307611c5b0e6656c41c284a18249b6f86c86 Mon Sep 17 00:00:00 2001 From: lidashuang Date: Mon, 31 Mar 2014 17:57:19 +0800 Subject: Create elixir-cn.html.markdown --- zh-cn/elixir-cn.html.markdown | 398 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 398 insertions(+) create mode 100644 zh-cn/elixir-cn.html.markdown (limited to 'zh-cn/elixir-cn.html.markdown') diff --git a/zh-cn/elixir-cn.html.markdown b/zh-cn/elixir-cn.html.markdown new file mode 100644 index 00000000..8ea499ff --- /dev/null +++ b/zh-cn/elixir-cn.html.markdown @@ -0,0 +1,398 @@ +--- +language: elixir +contributors: + - ["Joao Marques", "http://github.com/mrshankly"] +filename: learnelixir.ex +--- + +Elixir is a modern functional language built on top of the Erlang VM. +It's fully compatible with Erlang, but features a more standard syntax +and many more features. + +```ruby + +# Single line comments start with a hashtag. + +# There's no multi-line comment, +# but you can stack multiple comments. + +# To use the elixir shell use the `iex` command. +# Compile your modules with the `elixirc` command. + +# Both should be in your path if you installed elixir correctly. + +## --------------------------- +## -- Basic types +## --------------------------- + +# There are numbers +3 # integer +0x1F # integer +3.0 # float + +# Atoms, that are literals, a constant with name. They start with `:`. +:hello # atom + +# Tuples that are stored contiguously in memory. +{1,2,3} # tuple + +# We can access a tuple element with the `elem` function: +elem({1, 2, 3}, 0) #=> 1 + +# Lists that are implemented as linked lists. +[1,2,3] # list + +# We can access the head and tail of a list as follows: +[head | tail] = [1,2,3] +head #=> 1 +tail #=> [2,3] + +# In elixir, just like in Erlang, the `=` denotes pattern matching and +# not an assignment. +# +# This means that the left-hand side (pattern) is matched against a +# right-hand side. +# +# This is how the above example of accessing the head and tail of a list works. + +# A pattern match will error when the sides don't match, in this example +# the tuples have different sizes. +# {a, b, c} = {1, 2} #=> ** (MatchError) no match of right hand side value: {1,2} + +# There's also binaries +<<1,2,3>> # binary + +# Strings and char lists +"hello" # string +'hello' # char list + +# Multi-line strings +""" +I'm a multi-line +string. +""" +#=> "I'm a multi-line\nstring.\n" + +# Strings are all encoded in UTF-8: +"héllò" #=> "héllò" + +# Strings are really just binaries, and char lists are just lists. +<> #=> "abc" +[?a, ?b, ?c] #=> 'abc' + +# `?a` in elixir returns the ASCII integer for the letter `a` +?a #=> 97 + +# To concatenate lists use `++`, for binaries use `<>` +[1,2,3] ++ [4,5] #=> [1,2,3,4,5] +'hello ' ++ 'world' #=> 'hello world' + +<<1,2,3>> <> <<4,5>> #=> <<1,2,3,4,5>> +"hello " <> "world" #=> "hello world" + +## --------------------------- +## -- Operators +## --------------------------- + +# Some math +1 + 1 #=> 2 +10 - 5 #=> 5 +5 * 2 #=> 10 +10 / 2 #=> 5.0 + +# In elixir the operator `/` always returns a float. + +# To do integer division use `div` +div(10, 2) #=> 5 + +# To get the division remainder use `rem` +rem(10, 3) #=> 1 + +# There's also boolean operators: `or`, `and` and `not`. +# These operators expect a boolean as their first argument. +true and true #=> true +false or true #=> true +# 1 and true #=> ** (ArgumentError) argument error + +# Elixir also provides `||`, `&&` and `!` which accept arguments of any type. +# All values except `false` and `nil` will evaluate to true. +1 || true #=> 1 +false && 1 #=> false +nil && 20 #=> nil + +!true #=> false + +# For comparisons we have: `==`, `!=`, `===`, `!==`, `<=`, `>=`, `<` and `>` +1 == 1 #=> true +1 != 1 #=> false +1 < 2 #=> true + +# `===` and `!==` are more strict when comparing integers and floats: +1 == 1.0 #=> true +1 === 1.0 #=> false + +# We can also compare two different data types: +1 < :hello #=> true + +# The overall sorting order is defined below: +# number < atom < reference < functions < port < pid < tuple < list < bit string + +# To quote Joe Armstrong on this: "The actual order is not important, +# but that a total ordering is well defined is important." + +## --------------------------- +## -- Control Flow +## --------------------------- + +# `if` expression +if false do + "This will never be seen" +else + "This will" +end + +# There's also `unless` +unless true do + "This will never be seen" +else + "This will" +end + +# Remember pattern matching? Many control-flow structures in elixir rely on it. + +# `case` allows us to compare a value against many patterns: +case {:one, :two} do + {:four, :five} -> + "This won't match" + {:one, x} -> + "This will match and assign `x` to `:two`" + _ -> + "This will match any value" +end + +# It's common practice to assign a value to `_` if we don't need it. +# For example, if only the head of a list matters to us: +[head | _] = [1,2,3] +head #=> 1 + +# For better readability we can do the following: +[head | _tail] = [:a, :b, :c] +head #=> :a + +# `cond` lets us check for many conditions at the same time. +# Use `cond` instead of nesting many `if` expressions. +cond do + 1 + 1 == 3 -> + "I will never be seen" + 2 * 5 == 12 -> + "Me neither" + 1 + 2 == 3 -> + "But I will" +end + +# It is common to see a last condition equal to `true`, which will always match. +cond do + 1 + 1 == 3 -> + "I will never be seen" + 2 * 5 == 12 -> + "Me neither" + true -> + "But I will (this is essentially an else)" +end + +# `try/catch` is used to catch values that are thrown, it also supports an +# `after` clause that is invoked whether or not a value is catched. +try do + throw(:hello) +catch + message -> "Got #{message}." +after + IO.puts("I'm the after clause.") +end +#=> I'm the after clause +# "Got :hello" + +## --------------------------- +## -- Modules and Functions +## --------------------------- + +# Anonymous functions (notice the dot) +square = fn(x) -> x * x end +square.(5) #=> 25 + +# They also accept many clauses and guards. +# Guards let you fine tune pattern matching, +# they are indicated by the `when` keyword: +f = fn + x, y when x > 0 -> x + y + x, y -> x * y +end + +f.(1, 3) #=> 4 +f.(-1, 3) #=> -3 + +# Elixir also provides many built-in functions. +# These are available in the current scope. +is_number(10) #=> true +is_list("hello") #=> false +elem({1,2,3}, 0) #=> 1 + +# You can group several functions into a module. Inside a module use `def` +# to define your functions. +defmodule Math do + def sum(a, b) do + a + b + end + + def square(x) do + x * x + end +end + +Math.sum(1, 2) #=> 3 +Math.square(3) #=> 9 + +# To compile our simple Math module save it as `math.ex` and use `elixirc` +# in your terminal: elixirc math.ex + +# Inside a module we can define functions with `def` and private functions with `defp`. +# A function defined with `def` is available to be invoked from other modules, +# a private function can only be invoked locally. +defmodule PrivateMath do + def sum(a, b) do + do_sum(a, b) + end + + defp do_sum(a, b) do + a + b + end +end + +PrivateMath.sum(1, 2) #=> 3 +# PrivateMath.do_sum(1, 2) #=> ** (UndefinedFunctionError) + +# Function declarations also support guards and multiple clauses: +defmodule Geometry do + def area({:rectangle, w, h}) do + w * h + end + + def area({:circle, r}) when is_number(r) do + 3.14 * r * r + end +end + +Geometry.area({:rectangle, 2, 3}) #=> 6 +Geometry.area({:circle, 3}) #=> 28.25999999999999801048 +# Geometry.area({:circle, "not_a_number"}) +#=> ** (FunctionClauseError) no function clause matching in Geometry.area/1 + +# Due to immutability, recursion is a big part of elixir +defmodule Recursion do + def sum_list([head | tail], acc) do + sum_list(tail, acc + head) + end + + def sum_list([], acc) do + acc + end +end + +Recursion.sum_list([1,2,3], 0) #=> 6 + +# Elixir modules support attributes, there are built-in attributes and you +# may also add custom attributes. +defmodule MyMod do + @moduledoc """ + This is a built-in attribute on a example module. + """ + + @my_data 100 # This is a custom attribute. + IO.inspect(@my_data) #=> 100 +end + +## --------------------------- +## -- Records and Exceptions +## --------------------------- + +# Records are basically structures that allow you to associate a name with +# a particular value. +defrecord Person, name: nil, age: 0, height: 0 + +joe_info = Person.new(name: "Joe", age: 30, height: 180) +#=> Person[name: "Joe", age: 30, height: 180] + +# Access the value of name +joe_info.name #=> "Joe" + +# Update the value of age +joe_info = joe_info.age(31) #=> Person[name: "Joe", age: 31, height: 180] + +# The `try` block with the `rescue` keyword is used to handle exceptions +try do + raise "some error" +rescue + RuntimeError -> "rescued a runtime error" + _error -> "this will rescue any error" +end + +# All exceptions have a message +try do + raise "some error" +rescue + x in [RuntimeError] -> + x.message +end + +## --------------------------- +## -- Concurrency +## --------------------------- + +# Elixir relies on the actor model for concurrency. All we need to write +# concurrent programs in elixir are three primitives: spawning processes, +# sending messages and receiving messages. + +# To start a new process we use the `spawn` function, which takes a function +# as argument. +f = fn -> 2 * 2 end #=> #Function +spawn(f) #=> #PID<0.40.0> + +# `spawn` returns a pid (process identifier), you can use this pid to send +# messages to the process. To do message passing we use the `<-` operator. +# For all of this to be useful we need to be able to receive messages. This is +# achived with the `receive` mechanism: +defmodule Geometry do + def area_loop do + receive do + {:rectangle, w, h} -> + IO.puts("Area = #{w * h}") + area_loop() + {:circle, r} -> + IO.puts("Area = #{3.14 * r * r}") + area_loop() + end + end +end + +# Compile the module and create a process that evaluates `area_loop` in the shell +pid = spawn(fn -> Geometry.area_loop() end) #=> #PID<0.40.0> + +# Send a message to `pid` that will match a pattern in the receive statement +pid <- {:rectangle, 2, 3} +#=> Area = 6 +# {:rectangle,2,3} + +pid <- {:circle, 2} +#=> Area = 12.56000000000000049738 +# {:circle,2} + +# The shell is also a process, you can use `self` to get the current pid +self() #=> #PID<0.27.0> +``` + +## References + +* [Getting started guide](http://elixir-lang.org/getting_started/1.html) from [elixir webpage](http://elixir-lang.org) +* [Elixir Documentation](http://elixir-lang.org/docs/master/) +* ["Learn You Some Erlang for Great Good!"](http://learnyousomeerlang.com/) by Fred Hebert +* "Programming Erlang: Software for a Concurrent World" by Joe Armstrong -- cgit v1.2.3 From 708fd4a65b9a3c9dc216f23e1906de2421e657fd Mon Sep 17 00:00:00 2001 From: lidashuang Date: Mon, 31 Mar 2014 19:49:46 +0800 Subject: Update elixir-cn.html.markdown --- zh-cn/elixir-cn.html.markdown | 127 +++++++++++++++++++++--------------------- 1 file changed, 63 insertions(+), 64 deletions(-) (limited to 'zh-cn/elixir-cn.html.markdown') diff --git a/zh-cn/elixir-cn.html.markdown b/zh-cn/elixir-cn.html.markdown index 8ea499ff..2c8f5fb5 100644 --- a/zh-cn/elixir-cn.html.markdown +++ b/zh-cn/elixir-cn.html.markdown @@ -2,88 +2,88 @@ language: elixir contributors: - ["Joao Marques", "http://github.com/mrshankly"] + - ["lidashuang", "http://github.com/lidashuang"] filename: learnelixir.ex --- -Elixir is a modern functional language built on top of the Erlang VM. -It's fully compatible with Erlang, but features a more standard syntax -and many more features. +Elixir 是一门构建在Elang VM 之上的函数式编程语言。Elixir 完全兼容 Eralng, +另外还提供了更标准的语法,特性。 -```ruby +```elixir -# Single line comments start with a hashtag. +# 这是单行注释, 注释以井号开头 -# There's no multi-line comment, -# but you can stack multiple comments. +# 没有多行注释 +# 但你可以堆叠多个注释。 -# To use the elixir shell use the `iex` command. -# Compile your modules with the `elixirc` command. +# elixir shell 使用命令 `iex` 进入。 +# 编译模块使用 `elixirc` 命令。 -# Both should be in your path if you installed elixir correctly. +# 如果安装正确,这些命令都会在环境变量里 ## --------------------------- -## -- Basic types +## -- 基本类型 ## --------------------------- -# There are numbers -3 # integer -0x1F # integer -3.0 # float +# 数字 +3 # 整型 +0x1F # 整型 +3.0 # 浮点类型 -# Atoms, that are literals, a constant with name. They start with `:`. +# 原子(Atoms),以 `:`开头 :hello # atom # Tuples that are stored contiguously in memory. {1,2,3} # tuple -# We can access a tuple element with the `elem` function: +# 使用`elem`函数访问元组(tuple)里的元素: elem({1, 2, 3}, 0) #=> 1 -# Lists that are implemented as linked lists. +# 列表(list) [1,2,3] # list -# We can access the head and tail of a list as follows: +# 可以用下面的方法访问列表的头尾元素: [head | tail] = [1,2,3] head #=> 1 tail #=> [2,3] -# In elixir, just like in Erlang, the `=` denotes pattern matching and -# not an assignment. +# 在elixir,就像在Erlang, `=` 表示模式匹配 (pattern matching) +# 不是赋值。 # # This means that the left-hand side (pattern) is matched against a # right-hand side. # # This is how the above example of accessing the head and tail of a list works. -# A pattern match will error when the sides don't match, in this example -# the tuples have different sizes. +# 当左右两边不匹配时,会返回error, 在这个 +# 例子中,元组大小不一样。 # {a, b, c} = {1, 2} #=> ** (MatchError) no match of right hand side value: {1,2} -# There's also binaries +# 还有二进制类型 (binaries) <<1,2,3>> # binary -# Strings and char lists +# 字符串(Strings) 和 字符列表(char lists) "hello" # string 'hello' # char list -# Multi-line strings +# 多行字符串 """ I'm a multi-line string. """ #=> "I'm a multi-line\nstring.\n" -# Strings are all encoded in UTF-8: +# 所有的字符串(Strings)以UTF-8编码: "héllò" #=> "héllò" -# Strings are really just binaries, and char lists are just lists. +# 字符串(Strings)本质就是二进制类型(binaries), 字符列表(char lists)本质是列表(lists) <> #=> "abc" [?a, ?b, ?c] #=> 'abc' -# `?a` in elixir returns the ASCII integer for the letter `a` +# 在 elixir中,`?a`返回 `a` 的 ASCII 整型值 ?a #=> 97 -# To concatenate lists use `++`, for binaries use `<>` +# 合并列表使用 `++`, 对于二进制类型则使用 `<>` [1,2,3] ++ [4,5] #=> [1,2,3,4,5] 'hello ' ++ 'world' #=> 'hello world' @@ -91,67 +91,67 @@ string. "hello " <> "world" #=> "hello world" ## --------------------------- -## -- Operators +## -- 操作符(Operators) ## --------------------------- -# Some math +# 一些数学运算 1 + 1 #=> 2 10 - 5 #=> 5 5 * 2 #=> 10 10 / 2 #=> 5.0 -# In elixir the operator `/` always returns a float. +# 在 elixir 操作符 `/` 返回值总是浮点数。 -# To do integer division use `div` +# 做整数除法使用 `div` div(10, 2) #=> 5 -# To get the division remainder use `rem` +# 为了得到余数使用 `rem` rem(10, 3) #=> 1 -# There's also boolean operators: `or`, `and` and `not`. -# These operators expect a boolean as their first argument. +# 还有 boolean 操作符: `or`, `and` and `not`. +# 第一个参数必须是boolean 类型 true and true #=> true false or true #=> true # 1 and true #=> ** (ArgumentError) argument error -# Elixir also provides `||`, `&&` and `!` which accept arguments of any type. -# All values except `false` and `nil` will evaluate to true. +# Elixir 也提供了 `||`, `&&` 和 `!` 可以接受任意的类型 +# 除了`false` 和 `nil` 其它都会被当作true. 1 || true #=> 1 false && 1 #=> false nil && 20 #=> nil !true #=> false -# For comparisons we have: `==`, `!=`, `===`, `!==`, `<=`, `>=`, `<` and `>` +# 比较有: `==`, `!=`, `===`, `!==`, `<=`, `>=`, `<` 和 `>` 1 == 1 #=> true 1 != 1 #=> false 1 < 2 #=> true -# `===` and `!==` are more strict when comparing integers and floats: +# `===` 和 `!==` 在比较整型和浮点类型时更为严格: 1 == 1.0 #=> true 1 === 1.0 #=> false -# We can also compare two different data types: +# 我们也可以比较两种不同的类型: 1 < :hello #=> true -# The overall sorting order is defined below: +# 总的排序顺序定义如下: # number < atom < reference < functions < port < pid < tuple < list < bit string # To quote Joe Armstrong on this: "The actual order is not important, # but that a total ordering is well defined is important." ## --------------------------- -## -- Control Flow +## -- 控制结构(Control Flow) ## --------------------------- -# `if` expression +# `if` 表达式 if false do "This will never be seen" else "This will" end -# There's also `unless` +# 还有 `unless` unless true do "This will never be seen" else @@ -170,12 +170,12 @@ case {:one, :two} do "This will match any value" end -# It's common practice to assign a value to `_` if we don't need it. -# For example, if only the head of a list matters to us: +# 模式匹配时,如果不需要某个值,通用的做法是把值 匹配到 `_` +# 例如,我们只需要要列表的头元素: [head | _] = [1,2,3] head #=> 1 -# For better readability we can do the following: +# 下面的方式效果一样,但可读性更好 [head | _tail] = [:a, :b, :c] head #=> :a @@ -213,10 +213,10 @@ end # "Got :hello" ## --------------------------- -## -- Modules and Functions +## -- 模块和函数(Modules and Functions) ## --------------------------- -# Anonymous functions (notice the dot) +# 匿名函数 (注意点) square = fn(x) -> x * x end square.(5) #=> 25 @@ -231,14 +231,13 @@ end f.(1, 3) #=> 4 f.(-1, 3) #=> -3 -# Elixir also provides many built-in functions. -# These are available in the current scope. +# Elixir 提供了很多内建函数 +# 在默认作用域都是可用的 is_number(10) #=> true is_list("hello") #=> false elem({1,2,3}, 0) #=> 1 -# You can group several functions into a module. Inside a module use `def` -# to define your functions. +# 你可以在一个模块里定义多个函数,定义函数使用 `def` defmodule Math do def sum(a, b) do a + b @@ -252,12 +251,12 @@ end Math.sum(1, 2) #=> 3 Math.square(3) #=> 9 -# To compile our simple Math module save it as `math.ex` and use `elixirc` -# in your terminal: elixirc math.ex +# 保存到 `math.ex`,使用 `elixirc` 编译你的 Math 模块 +# 在终端里: elixirc math.ex -# Inside a module we can define functions with `def` and private functions with `defp`. -# A function defined with `def` is available to be invoked from other modules, -# a private function can only be invoked locally. +# 在模块中可以使用`def`定义函数,使用 `defp` 定义私有函数 +# 使用`def` 定义的函数可以被其它模块调用 +# 私有函数只能在本模块内调用 defmodule PrivateMath do def sum(a, b) do do_sum(a, b) @@ -287,7 +286,7 @@ Geometry.area({:circle, 3}) #=> 28.25999999999999801048 # Geometry.area({:circle, "not_a_number"}) #=> ** (FunctionClauseError) no function clause matching in Geometry.area/1 -# Due to immutability, recursion is a big part of elixir +#由于不变性,递归是Elixir的重要组成部分 defmodule Recursion do def sum_list([head | tail], acc) do sum_list(tail, acc + head) @@ -322,7 +321,7 @@ defrecord Person, name: nil, age: 0, height: 0 joe_info = Person.new(name: "Joe", age: 30, height: 180) #=> Person[name: "Joe", age: 30, height: 180] -# Access the value of name +# 访问name的值 joe_info.name #=> "Joe" # Update the value of age @@ -345,7 +344,7 @@ rescue end ## --------------------------- -## -- Concurrency +## -- 并发(Concurrency) ## --------------------------- # Elixir relies on the actor model for concurrency. All we need to write @@ -386,7 +385,7 @@ pid <- {:circle, 2} #=> Area = 12.56000000000000049738 # {:circle,2} -# The shell is also a process, you can use `self` to get the current pid +# shell也是一个进程(process), 你可以使用`self`获取当前 pid self() #=> #PID<0.27.0> ``` -- cgit v1.2.3 From 816ba7d7e6f3e6b7b93275c6041498eabcf41c84 Mon Sep 17 00:00:00 2001 From: lidashuang Date: Mon, 31 Mar 2014 22:12:03 +0800 Subject: Update elixir-cn.html.markdown --- zh-cn/elixir-cn.html.markdown | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'zh-cn/elixir-cn.html.markdown') diff --git a/zh-cn/elixir-cn.html.markdown b/zh-cn/elixir-cn.html.markdown index 2c8f5fb5..a2e5f4aa 100644 --- a/zh-cn/elixir-cn.html.markdown +++ b/zh-cn/elixir-cn.html.markdown @@ -311,7 +311,7 @@ defmodule MyMod do end ## --------------------------- -## -- Records and Exceptions +## -- 记录和异常(Records and Exceptions) ## --------------------------- # Records are basically structures that allow you to associate a name with @@ -324,7 +324,7 @@ joe_info = Person.new(name: "Joe", age: 30, height: 180) # 访问name的值 joe_info.name #=> "Joe" -# Update the value of age +# 更新age的值 joe_info = joe_info.age(31) #=> Person[name: "Joe", age: 31, height: 180] # The `try` block with the `rescue` keyword is used to handle exceptions @@ -351,8 +351,7 @@ end # concurrent programs in elixir are three primitives: spawning processes, # sending messages and receiving messages. -# To start a new process we use the `spawn` function, which takes a function -# as argument. +# 启动一个新的进程使用`spawn`函数,接收一个函数作为参数 f = fn -> 2 * 2 end #=> #Function spawn(f) #=> #PID<0.40.0> @@ -373,10 +372,10 @@ defmodule Geometry do end end -# Compile the module and create a process that evaluates `area_loop` in the shell +# 编译这个模块,在shell中创建一个进程,并执行 `area_looop` 函数。 pid = spawn(fn -> Geometry.area_loop() end) #=> #PID<0.40.0> -# Send a message to `pid` that will match a pattern in the receive statement +# 发送一个消息给 `pid`, 会在receive语句进行模式匹配 pid <- {:rectangle, 2, 3} #=> Area = 6 # {:rectangle,2,3} @@ -389,7 +388,7 @@ pid <- {:circle, 2} self() #=> #PID<0.27.0> ``` -## References +## 参考文献 * [Getting started guide](http://elixir-lang.org/getting_started/1.html) from [elixir webpage](http://elixir-lang.org) * [Elixir Documentation](http://elixir-lang.org/docs/master/) -- cgit v1.2.3 From c9bc93c261e74cfd3f5b9fedaade75aec91ec253 Mon Sep 17 00:00:00 2001 From: lidashuang Date: Mon, 31 Mar 2014 22:24:09 +0800 Subject: Update elixir-cn.html.markdown --- zh-cn/elixir-cn.html.markdown | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'zh-cn/elixir-cn.html.markdown') diff --git a/zh-cn/elixir-cn.html.markdown b/zh-cn/elixir-cn.html.markdown index a2e5f4aa..1f8e3e2c 100644 --- a/zh-cn/elixir-cn.html.markdown +++ b/zh-cn/elixir-cn.html.markdown @@ -299,14 +299,13 @@ end Recursion.sum_list([1,2,3], 0) #=> 6 -# Elixir modules support attributes, there are built-in attributes and you -# may also add custom attributes. +# Elixir 模块支持属性,模块内建了一些属性,你也可以自定义属性 defmodule MyMod do @moduledoc """ - This is a built-in attribute on a example module. + 内置的属性,模块文档 """ - @my_data 100 # This is a custom attribute. + @my_data 100 # 自定义属性 IO.inspect(@my_data) #=> 100 end @@ -327,7 +326,7 @@ joe_info.name #=> "Joe" # 更新age的值 joe_info = joe_info.age(31) #=> Person[name: "Joe", age: 31, height: 180] -# The `try` block with the `rescue` keyword is used to handle exceptions +# 使用 `try` `rescue` 进行异常处理 try do raise "some error" rescue @@ -335,7 +334,7 @@ rescue _error -> "this will rescue any error" end -# All exceptions have a message +# 所有的异常都有一个message try do raise "some error" rescue @@ -347,18 +346,19 @@ end ## -- 并发(Concurrency) ## --------------------------- -# Elixir relies on the actor model for concurrency. All we need to write -# concurrent programs in elixir are three primitives: spawning processes, -# sending messages and receiving messages. +# Elixir 依赖与actor并发模型。在Elixir编写并发程序的三要素: +# 创建进程,发送消息,接收消息 # 启动一个新的进程使用`spawn`函数,接收一个函数作为参数 + f = fn -> 2 * 2 end #=> #Function spawn(f) #=> #PID<0.40.0> -# `spawn` returns a pid (process identifier), you can use this pid to send -# messages to the process. To do message passing we use the `<-` operator. -# For all of this to be useful we need to be able to receive messages. This is -# achived with the `receive` mechanism: + +# `spawn` 函数返回一个pid(进程标识符),你可以使用pid向进程发送消息。 +# 使用 `<-` 操作符发送消息。 +# 我们需要在进程内接收消息,要用到 `receive` 机制。 + defmodule Geometry do def area_loop do receive do -- cgit v1.2.3 From d6bd212e433df381be331a512796b2572d023cb1 Mon Sep 17 00:00:00 2001 From: lidashuang Date: Mon, 31 Mar 2014 23:25:19 +0800 Subject: Update elixir-cn.html.markdown --- zh-cn/elixir-cn.html.markdown | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) (limited to 'zh-cn/elixir-cn.html.markdown') diff --git a/zh-cn/elixir-cn.html.markdown b/zh-cn/elixir-cn.html.markdown index 1f8e3e2c..a624f6b1 100644 --- a/zh-cn/elixir-cn.html.markdown +++ b/zh-cn/elixir-cn.html.markdown @@ -33,7 +33,7 @@ Elixir 是一门构建在Elang VM 之上的函数式编程语言。Elixir 完全 # 原子(Atoms),以 `:`开头 :hello # atom -# Tuples that are stored contiguously in memory. +# 元组(Tuple) 在内存中的存储是连续的 {1,2,3} # tuple # 使用`elem`函数访问元组(tuple)里的元素: @@ -50,10 +50,9 @@ tail #=> [2,3] # 在elixir,就像在Erlang, `=` 表示模式匹配 (pattern matching) # 不是赋值。 # -# This means that the left-hand side (pattern) is matched against a -# right-hand side. -# -# This is how the above example of accessing the head and tail of a list works. +# 这表示会用左边的模式(pattern)匹配右侧 +# +# 这是上面的例子中访问列列表的头部和尾部的就是这样工作的。 # 当左右两边不匹配时,会返回error, 在这个 # 例子中,元组大小不一样。 @@ -137,8 +136,8 @@ nil && 20 #=> nil # 总的排序顺序定义如下: # number < atom < reference < functions < port < pid < tuple < list < bit string -# To quote Joe Armstrong on this: "The actual order is not important, -# but that a total ordering is well defined is important." +# 引用Joe Armstrong :”实际的顺序并不重要, +# 但是,一个整体排序是否经明确界定是非常重要的“。 ## --------------------------- ## -- 控制结构(Control Flow) @@ -158,9 +157,9 @@ else "This will" end -# Remember pattern matching? Many control-flow structures in elixir rely on it. +# 在Elixir中,很多控制结构都依赖于模式匹配 -# `case` allows us to compare a value against many patterns: +# `case` 允许我们把一个值与多种模式进行比较: case {:one, :two} do {:four, :five} -> "This won't match" @@ -179,8 +178,8 @@ head #=> 1 [head | _tail] = [:a, :b, :c] head #=> :a -# `cond` lets us check for many conditions at the same time. -# Use `cond` instead of nesting many `if` expressions. +# `cond` 可以检测多种不同的分支 +# 使用 `cond` 代替多个`if` 表达式嵌套 cond do 1 + 1 == 3 -> "I will never be seen" @@ -190,7 +189,7 @@ cond do "But I will" end -# It is common to see a last condition equal to `true`, which will always match. +# 经常可以看到最后一个条件等于'true',这将总是匹配。 cond do 1 + 1 == 3 -> "I will never be seen" @@ -200,8 +199,9 @@ cond do "But I will (this is essentially an else)" end -# `try/catch` is used to catch values that are thrown, it also supports an -# `after` clause that is invoked whether or not a value is catched. +# `try/catch` 用于捕获被抛出的值, 它也支持 `after` 子句, +# 无论是否值被捕获,after 子句都会被调用 +# `try/catch` try do throw(:hello) catch @@ -220,9 +220,10 @@ end square = fn(x) -> x * x end square.(5) #=> 25 -# They also accept many clauses and guards. -# Guards let you fine tune pattern matching, -# they are indicated by the `when` keyword: + +# 也支持接收多个子句和卫士(guards). +# Guards可以进行模式匹配 +# Guards只有`when` 关键字指明: f = fn x, y when x > 0 -> x + y x, y -> x * y @@ -313,8 +314,7 @@ end ## -- 记录和异常(Records and Exceptions) ## --------------------------- -# Records are basically structures that allow you to associate a name with -# a particular value. +# 记录就是把特定值关联到某个名字的结构体 defrecord Person, name: nil, age: 0, height: 0 joe_info = Person.new(name: "Joe", age: 30, height: 180) -- cgit v1.2.3