文章目录
基础知识
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}