-
Notifications
You must be signed in to change notification settings - Fork 393
Expand file tree
/
Copy pathElixir_Guards.rb
More file actions
164 lines (137 loc) · 3.28 KB
/
Copy pathElixir_Guards.rb
File metadata and controls
164 lines (137 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
cheatsheet do
title 'Elixir Guards'
docset_file_name 'Elixir_Guards'
keyword 'elixirguard'
source_url 'http://cheat.kapeli.com'
introduction <<-END
Guard clauses allow Elixir to determine which function to invoke
based not only on which arguments are passed, but also based on
type or some tests involving their values. Guard clauses are defined
using the `when` keyword.
Examples:
```elixir
defmodule Guard do
def kind_of(x) when is_binary(x) do
"\#{x} is a binary"
end
def kind_of(x) when is_atom(x) do
"\#{x} is an atom"
end
def is_ten(x) when x > 10, do: "Greater than ten"
def is_ten(x) when x < 10, do: "Less than ten"
def is_ten(x), do: "Yes"
end
```
```elixir
max = fn
x, y when x > y -> x
x, y -> y
end
```
```elxir
case 42 do
x when is_binary(x) -> "Nope"
x -> "Yep"
end
```
Additionally, users may define their own guards. Example: the `Bitwise`
module defines `bnot`, `~~~`, `band`, `&&&`, `bor`, `|||`, `bxor`, `^^^`,
`bsl` `<<\<`, `bsr` `>>>`.
END
category do
id 'Operators'
entry do
name 'Comparison'
notes "
`==`, `!=`, `===`, `!==`, `<`, `>`, `<=`, `>=`
"
end
entry do
name 'Arithmetic'
notes "
`+`, `-`, `*`, `/`
Unary operators `+`, and `-` are also allowed.
"
end
entry do
name 'Boolean and Negation'
notes "
`or`, `and`, `not`
Short-circuiting operators `&&`, `||`, and `!` are not allowed.
"
end
entry do
name 'Join'
notes "
`<>`, `++` as long as left side is a literal.
"
end
entry do
name 'Membership'
notes "
Test membership in a list or range with `in`.
Example:
```elixir
def is_animal(animal) when animal in [:dog, :cat, :fish] do
true
end
def in_range(x) when x in 1..6 do
true
end
```
"
end
end
category do
id 'Functions'
entry do
name 'Type-check'
notes "
See main Elixir documentation for more information.
`is_atom/1`,
`is_binary/1`,
`is_bitstring/1`,
`is_boolean/1`,
`is_float/1`,
`is_function/1`,
`is_function/2`,
`is_integer/1`,
`is_list/1`,
`is_map/1`,
`is_number/1`,
`is_pid/1`,
`is_port/1`,
`is_record/1`,
`is_record/2`,
`is_reference/1`,
`is_tuple/1`
"
end
entry do
name 'Other'
notes "
See main Elixir documentation for more information.
`abs(number)`,
`binary_part(binary, start, length)`,
`bit_size(bitstring)`,
`byte_size(bitstring)`,
`div(number, number)`,
`elem(tuple, n)`,
`hd(list)`,
`length(list)`,
`map_size(map)`,
`is_map_key(map, key)`,
`node()`, `node(pid | ref | port)`,
`rem(integer, integer)`,
`round(number)`,
`self()`,
`tl(list)`,
`trunc(number)`,
`tuple_size(tuple)`
"
end
end
notes "
* Based on [\"Expressions in guard clauses\"](http://elixir-lang.org/getting-started/case-cond-and-if.html#expressions-in-guard-clauses) from [Elixir-Lang Getting Started Guide](http://elixir-lang.org/getting-started/introduction.html)
"
end