ctrlcv-dev.comgithub

Elixircheatsheet

贡献者:BAI

Getting started

Hello world

ELIXIR
# hello.exs
defmodule Greeter do
def greet(name) do
message = "Hello, " <> name <> "!"
IO.puts message
end
end
Greeter.greet("world")
BASH
elixir hello.exs
# Hello, world!

变量

ELIXIR
age = 23

Map

ELIXIR
user = %{
name: "John",
city: "Melbourne"
}
ELIXIR
IO.puts "Hello, " <> user.name

List

ELIXIR
users = [ "Tom", "Dick", "Harry" ]
ELIXIR
Enum.map(users, fn user ->
IO.puts "Hello " <> user
end)

Piping

ELIXIR
source
|> transform(:hello)
|> print()
ELIXIR
print(transform(source, :hello))

上面两个等价

模式匹配

ELIXIR
user = %{name: "Tom", age: 23}
%{name: username} = user

上面语境将 username 赋值为 "TOM"

函数中的模式匹配

ELIXIR
def greet(%{name: username}) do
IO.puts "Hello, " <> username
end
user = %{name: "Tom", age: 23}

模式匹配也可以在函数参数中使用

控制流

If

ELIXIR
if false do
"This will never be seen"
else
"This will"
end

Case

ELIXIR
case {1, 2, 3} do
{4, 5, 6} ->
"This clause won't match"
{1, x, 3} ->
"This will match and bind x to 2"
_ ->
"This will match any value"
end

Cond

ELIXIR
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

Errors

ELIXIR
try do
throw(:hello)
catch
message -> "Got #{message}."
after
IO.puts("I'm the after clause.")
end

类型

原始类型

SampleType
nilNil/null
true / falseBoolean
------
?aInteger (ASCII)
23Integer
3.14Float
------
'hello'Charlist
<<2, 3>>Binary
"hello"Binary string
:helloAtom
------
[a, b]List
{a, b}Tuple
------
%{a: "hello"}Map
%MyStruct{a: "hello"}Struct
fn -> ... endFunction

类型检查

ELIXIR
is_atom/1
is_bitstring/1
is_boolean/1
is_function/1
is_function/2
is_integer/1
is_float/1
ELIXIR
is_binary/1
is_list/1
is_map/1
is_tuple/1
ELIXIR
is_nil/1
is_number/1
is_pid/1
is_port/1
is_reference/1

操作符

ELIXIR
left != right # equal
left !== right # match
left ++ right # concat lists
left <> right # concat string/binary
left =~ right # regexp

模块

导入

ELIXIR
require Redux # compiles a module
import Redux # compiles, and you can use without the `Redux.` prefix
use Redux # compiles, and runs Redux.__using__/1
use Redux, async: true
import Redux, only: [duplicate: 2]
import Redux, only: :functions
import Redux, only: :macros
import Foo.{Bar, Baz}

别名(Alias)

ELIXIR
alias Foo.Bar, as: Bar
alias Foo.Bar # same as above
alias Foo.{Bar, Baz}

字符串

常用函数

ELIXIR
import String
ELIXIR
str = "hello"
str |> length() # → 5
str |> codepoints() # → ["h", "e", "l", "l", "o"]
str |> slice(2..-1) # → "llo"
str |> split(" ") # → ["hello"]
str |> capitalize() # → "Hello"
str |> match(regex)

检查对象

ELIXIR
inspect(object, opts \\ [])
ELIXIR
value |> IO.inspect()
ELIXIR
value |> IO.inspect(label: "value")

数字

操作符

ELIXIR
abs(n)
round(n)
rem(a, b) # remainder (modulo)
div(a, b) # integer division

Float

ELIXIR
import Float
ELIXIR
n = 10.3
ELIXIR
n |> ceil() # → 11.0
n |> ceil(2) # → 11.30
n |> to_string() # → "1.030000+e01"
n |> to_string([decimals: 2, compact: true])
ELIXIR
Float.parse("34") # → { 34.0, "" }

Integer

ELIXIR
import Integer
ELIXIR
n = 12
ELIXIR
n |> digits() # → [1, 2]
n |> to_charlist() # → '12'
n |> to_string() # → "12"
n |> is_even()
n |> is_odd()
ELIXIR
# Different base:
n |> digits(2) # → [1, 1, 0, 0]
n |> to_charlist(2) # → '1100'
n |> to_string(2) # → "1100"
ELIXIR
parse("12") # → {12, ""}
undigits([1, 2]) # → 12

Type 转换

ELIXIR
Float.parse("34.1") # → {34.1, ""}
Integer.parse("34") # → {34, ""}
ELIXIR
Float.to_string(34.1) # → "3.4100e+01"
Float.to_string(34.1, [decimals: 2, compact: true]) # → "34.1"

Map

定义

ELIXIR
m = %{name: "hi"} # atom keys (:name)
m = %{"name" => "hi"} # string keys ("name")

更改

ELIXIR
import Map
ELIXIR
m = %{m | name: "yo"} # key must exist
ELIXIR
m |> put(:id, 2) # → %{id: 2, name: "hi"}
m |> put_new(:id, 2) # only if `id` doesn't exist (`||=`)
ELIXIR
m |> put(:b, "Banana")
m |> merge(%{b: "Banana"})
m |> update(:a, &(&1 + 1))
m |> update(:a, fun a -> a + 1 end)
ELIXIR
m |> get_and_update(:a, &(&1 || "default"))
# → {old, new}

删除

ELIXIR
m |> delete(:name) # → %{}
m |> pop(:name) # → {"John", %{}}

读取

ELIXIR
m |> get(:id) # → 1
m |> keys() # → [:id, :name]
m |> values() # → [1, "hi"]
ELIXIR
m |> to_list() # → [id: 1, name: "hi"]
# → []

深层读取

ELIXIR
put_in(map, [:b, :c], "Banana")
put_in(map[:b][:c], "Banana") # via macros
ELIXIR
get_and_update_in(users, ["john", :age], &{&1, &1 + 1})

从 List 中构建

ELIXIR
Map.new([])
Map.new([a: 1, b: 2])
Map.new([:a, :b], fn x -> {x, x} end) # → %{a: :a, b: :b}

List

ELIXIR
import List
ELIXIR
l = [ 1, 2, 3, 4 ]
ELIXIR
l = l ++ [5] # push (append)
l = [ 0 | list ] # unshift (prepend)
ELIXIR
l |> first()
l |> last()
ELIXIR
l |> flatten()
l |> flatten(tail)

详见 Enum.

Enum

用法

ELIXIR
import Enum
ELIXIR
list = [:a, :b, :c]
ELIXIR
list |> at(0) # → :a
list |> count() # → 3
list |> empty?() # → false
list |> any?() # → true
ELIXIR
list |> concat([:d]) # → [:a, :b, :c, :d]

也可以用 Stream 代替

Map 和 Reduce

ELIXIR
list |> reduce(fn)
list |> reduce(acc, fn)
list |> map(fn)
list |> reject(fn)
list |> any?(fn)
list |> empty?(fn)
ELIXIR
[1, 2, 3, 4]
|> Enum.reduce(0, fn(x, acc) -> x + acc end)

Tuple

Tuple

ELIXIR
import Tuple
ELIXIR
t = { :a, :b }
ELIXIR
t |> elem(1) # like tuple[1]
t |> put_elem(index, value)
t |> tuple_size()

带关键字的 List

ELIXIR
list = [{ :name, "John" }, { :age, 15 }]
list[:name]
ELIXIR
# For string-keyed keyword lists
list = [{"size", 2}, {"type", "shoe"}]
List.keyfind(list, "size", 0) # → {"size", 2}

函数

Lambda

ELIXIR
square = fn n -> n*n end
square.(20)

& 标识符

ELIXIR
square = &(&1 * &1)
square.(20)
square = &Math.square/1

函数执行

ELIXIR
fun.(args)
apply(fun, args)
apply(module, fun, args)

函数头

ELIXIR
def join(a, b \\ nil)
def join(a, b) when is_nil(b) do: a
def join(a, b) do: a <> b

结构体

结构体

ELIXIR
defmodule User do
defstruct name: "", age: nil
end
%User{name: "John", age: 20}
%User{}.struct # → User

详见: Structs

协议(Protocol)

定义

ELIXIR
defprotocol Blank do
@doc "Returns true if data is considered blank/empty"
def blank?(data)
end
ELIXIR
defimpl Blank, for: List do
def blank?([]), do: true
def blank?(_), do: false
end
Blank.blank?([]) # → true

Any

..
defimpl Blank, for: Any do ... end
defmodule User do
@derive Blank # Falls back to Any
defstruct name: ""
end

示例

  • Enumerable and Enum.map()
  • Inspect and inspect()

推导式

For

ELIXIR
for n <- [1, 2, 3, 4], do: n * n
for n <- 1..4, do: n * n
ELIXIR
for {key, val} <- %{a: 10, b: 20}, do: val
# → [10, 20]
ELIXIR
for {key, val} <- %{a: 10, b: 20}, into: %{}, do: {key, val*val}

条件

ELIXIR
for n <- 1..10, rem(n, 2) == 0, do: n
# → [2, 4, 6, 8, 10]

复杂的示例

ELIXIR
for dir <- dirs,
file <- File.ls!(dir), # 嵌套
path = Path.join(dir, file), # 调用
File.regular?(path) do # 条件判断
IO.puts(file)
end

Misc

元编程

ELIXIR
__MODULE__
__MODULE__.__info__
@after_compile __MODULE__
def __before_compile__(env)
def __after_compile__(env, _bytecode)
def __using__(opts) # invoked on `use`
@on_definition {__MODULE__, :on_def}
def on_def(_env, kind, name, args, guards, body)
@on_load :load_check
def load_check

正则

ELIXIR
exp = ~r/hello/
exp = ~r/hello/i
"hello world" =~ exp

Sigil

ELIXIR
~r/regexp/
~w(list of strings)
~s|strings with #{interpolation} and \x20 escape codes|
~S|no interpolation and no escapes|
~c(charlist)

允许的字符: / | " ' ( [ { < """. 详见: Sigils

类型 Spec

ELIXIR
@spec round(number) :: integer
@type number_with_remark :: {number, String.t}
@spec add(number, number) :: number_with_remark

相关资料: dialyzer. Typespecs

行为

ELIXIR
defmodule Parser do
@callback parse(String.t) :: any
@callback extensions() :: [String.t]
end
ELIXIR
defmodule JSONParser do
@behaviour Parser
def parse(str), do: # ... parse JSON
def extensions, do: ["json"]
end

详见: Module

参考资料