Elixir 學習筆記


基礎知識

Elixir Shell

返回目錄

# 啓動交互模式,按兩次 Ctrl + C 退出
$ iex
# 在win系統下,啓動 Erlang Shell
$ iex --werl 
# 啓動,並運行腳本
$ iex -S 腳本名 #待驗證

# 執行.exs文件。腳本 simple.exs 內容爲:IO.puts "Hello world from Elixir"
$ elixir simple.exs
Hello world from Elixir #執行結果

iex -S mix run
elixirc

shell 幫助信息

返回目錄

# 幫助信息
iex> h()
# h 方法名
iex> h round
# h 方法名/參數個數
iex> h round/1
# h 操作符/參數個數
iex> h ==/1

基本類型

返回目錄

類型 說明
1 integer 十進制整型
0b1010 integer 二進制整型
0o777 integer 八進制整型
0x1F integer 十六進制整型
1.0 float 浮點型
1.0e-10 float 科學計數法表示浮點型
true boolean 布爾型,真
false boolean 布爾型,假
:atom atom/symbol 原子
“elixir” string 字符串
[1,2,3] list 列表
[1,2,3] tuple 元組

基本運算符

返回目錄

運算符 說明 示例
+ 1+2 //3
- 2-1 //1
* 2*3 //6
/ 10/2 //5.0
div 整除 div(10, 2) //5
div 10, 2 //5
rem 取餘 rem 10, 3 //1

基本方法

返回目錄

方法 說明 示例
round/1 四捨五入取整 round(3.58) //4
trunc/1 向下取整 trunc(3.58) //3
is_boolean/1 是否布爾型 is_boolean(1) //false
is_integer/1 是否整型 is_integer(1) //true
is_float/1 是否浮點型 is_float(2.4) //true
is_number/1 是否數字 is_number(1.2) //true
is_atom/1 是否原子 is_atom(:word) //true
is_binary/1 是否二進制 is_binary(“H”) //true
is_function/1 是否函數 is_function(round) //true
is_function/2 是否函數,並檢查參數個數 is_function(round, 2) //false

數據類型

atom 原子

返回目錄

原子就是字面符號常量

iex> :hello #原子
iex> Hello #原子

布爾值 true 和 false 也是原子

iex> true == :true #true
iex> is_atom(false) #true
iex> is_boolean(:false) #true

string 字符串

返回目錄

用雙引號標識字符串,使用 UTF-8 編碼

iex> "hello world" #"hello world"
# 原子 插入字符串
iex> "hello #{:world}" #"hello world"
# 換行
iex> "hello
...> world" #"hello\nworld"
iex> "hello\nworld" #"hello\nworld"
# 打印
iex> IO.puts "hello\nworld" #hello
							#world
# 字符串的內部使用的二進制表示的
iex> is_binary("hello") #true
# 獲取字符串的字節數
iex> byte_size("hellö") #6
# 獲取字符串的長度
iex> String.length("hellö") #5
# 轉換成大寫
iex> String.upcase("hellö") #"HELLÖ"

function 函數

返回目錄

函數可以像整數和字符串一樣作爲參數傳遞給其它函數

參數個數

返回目錄

# 定義函數round,其參數個數爲1
iex> round/1

匿名函數

返回目錄

使用 fn 和 end 組成
是閉包

# 定義匿名函數
iex> add = fn a, b -> a + b end
# 執行
iex> add.(1, 2) #3
# 立即執行的匿名函數
iex> (fn -> x = 0 end).() #返回x,即 0

list 列表

返回目錄

iex> list = [1, 2, true, 3] #[1, 2, true, 3]
# 列表的長度
iex> length [1, 2, 3] #3
# 相連操作符 ++/2;相減操作符 --/2
iex> [1, 2, 3] ++ [4, 5, 6] #[1,2,3,4,5,6]
iex> [1, true, 2, false, 3, true] -- [true, false] #[1,2,3,true]
# 獲取列表第一個元素
iex> hd(list) #1
# 獲取列表除第一個元素外的其它元素
iex> tl(list) #[2, true, 3]
# hd [] 和 tl [] 報錯

#  列表中元素都是 ASCII 碼時,會直接打印出來
iex> [11, 22, 33] #'\v\f\r'
iex> [104, 101, 108, 108, 111] #'hello'
# 用 i/1 來確認列表內容
iex> i [104]
iex> i 'hello'
# 單引號 表示的是 charlists,雙引號是字符串
iex> 'hello' == "hello" #false

# 列表級聯的性能取決於左側列表的長度
iex> list = [1,2,3]
# 快
iex> [0] ++ list
# 慢
iex> list ++ [4]

tuple 元組

返回目錄

元組將元素連續存儲在內存中。通過索引訪問元組元素或獲取元組大小是一種快速操作。索引從 0 開始,但是更新或添加元素是很昂貴的

iex> tuple = {:ok, "hello"} #{:ok, "hello"}
# 獲取元組的長度
iex> tuple_size {:ok, "hello"} #2
# 獲取元組中的元素
iex> elem(tuple, 1) #"hello"
# 插入元素,不能超出索引範圍
iex> put_elem(tuple, 1, "world") #返回一個新元組,不改變源值
{:ok, "world"}

IO

返回目錄

命令 說明 示例
IO.puts 輸出字符串 IO.puts “Hello world!”

File

返回目錄

iex> File.read("path/to/existing/file")
{:ok, "... contents ..."}
iex> File.read("path/to/unknown/file")
{:error, :enoent}
發佈了41 篇原創文章 · 獲贊 2 · 訪問量 3160
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章