-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.ex
More file actions
164 lines (118 loc) · 2.13 KB
/
Copy pathmain.ex
File metadata and controls
164 lines (118 loc) · 2.13 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
## Run Code in IEX (Interactive Elixir)
## Numbers - Integers and Floats
2 + 3
3 * 4
3 - 5
x = 10
## 5.0
x / 2
# 255
0xFF
3.14159
3.0e-2
div(5, 2)
rem(5, 2)
99_999_999_999_999_999_999_999_999_999_999_999_999_999_123_871_293_871_902_387_102 +
99_999_999_999_999_999_999_999_999_999_999_999_999_999_123_871_293_871_902_387_102
## 1000000
1_000_000
## Atoms
:atoms
:"an atom"
:"12387 &*^#2"
var = :atom
## Aliases
AModule
# true
:"Elixir.AModule" == AModlue
alias IO, as: MyIO
MyIO.puts("Hello" == IO.puts("Hello"))
## Booleans and nil
true
false
true
false
# true
true == true
# true
false == false
true and false
true or false
not true
nil
nil
nil == nil
if 10 do
IO.puts("true")
end
# prints true
if nil do
IO.puts("true")
end
# get back nil
# short circuit
nil || false || 4 || true
true && 5
!5
nil && 5
true && 5 && nil && false
## Tuples
cat = {"Jazzpurr", 12}
age = elem(cat, 1)
put_elem(cat, 1, 13)
cat = put_elem(cat, 1, 23)
## Lists
numbers = [1, 2, 3, 4]
length(numbers)
Enum.at(numbers, 5)
Enum.at(numbers, 3)
3 in numbers
5 in numbers
List.replace_at(numbers, 0, 12)
# [12, 2, 3, 4]
List.insert_at(numbers, 3, 13)
List.insert_at(numbers, -1, 13)
[1, 2, 3] ++ [4, 5]
[head | tail] = [1, 2, 3, 4, 5, 6, 7]
# head = 1
# tail = [2, 3, 4, 5, 6, 7]
[1 | [2 | [3 | [4 | [5 | [6 | [7 | []]]]]]]]
hd(numbers)
tl(numbers)
alist = [15, :atom, true]
[:first | atlist]
# [:first, 15, :atom, true]
## Maps
empty = %{}
sqrs = %{1 => 1, 2 => 4, 3 => 9}
sqrs[1]
sqrs[2]
Map.get(sqrs, 3)
Map.get(sqrs, 5)
cat = %{name: "Jazzpurr", age: 12}
cat.age
cat.name
cat[:name]
cat[:age]
older_cat = %{cat | age: 15}
older_cat.age
## Binaries and Bitstrings
<<1, 2, 3>>
# <<0>>
<<256>>
# <<1>>
<<257>>
<<256::16>>
<<1::4, 15::4>>
<<1, 2>> <> <<3, 4>>
## Strings and Charlist
"This is a String"
"Add together #{3 + 4}"
"\r \n \" \\"
~s("This is a string!")
~S(This is a String!)
'ABC' == [65, 66, 67]
'sum: #{3 + 5}'
~c(Character Sigil)
# 'A String'
String.to_charlist("A String")