-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_elixir.exs
More file actions
194 lines (160 loc) · 3.07 KB
/
example_elixir.exs
File metadata and controls
194 lines (160 loc) · 3.07 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
i = 1
j = 2
s = "kahfahflas"
a = :a
l = [1, 2, 3]
t = {"gino", 4, :a}
tt = {:a, 2}
kw = [{:opt1, 44}, {:opt3, true}]
kw1 = [opt1: 44, opt3: true]
kw == kw1
IO.inspect([1, 2, 3, 4, 5, 6, 7, 8])
IO.inspect([1, 2, 3, 4, 5, 6, 7, 8], label: "lista")
IO.inspect([1, 2, 3, 4, 5, 6, 7, 8], label: "lista", width: 9)
m = %{"s" => 2}
m = %{:a => 2}
m = %{a: 2}
m = %{a: 2, c: 3, d: 4}
m1 = %{:a => 2, :c => 3, :d => 4}
m == m1
l = [1, 2, 3]
[x, y, z] = l
x
y
z
[h | t] = l
h
t
l1 = [h | t]
l1 == l
m = %{a: 2, c: 3, d: 4}
%{a: v} = m
v
%{z: v} = m
# Talk about rebinding
m
m = %{x: 9, y: 8, z: 7}
v
%{x: x} = m
x
%{y: x} = m
x
%{z: ^x} = m
# FUNCTIONS
clear
f = fn x, y -> x + y end
f
f.(1, 2)
f.(3, 3)
f.(1)
clear
l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
l1 = Enum.map(l, fn x -> x * 3 end)
l2 = Enum.filter(l1, fn x -> rem(x, 2) == 0 end)
sum = Enum.reduce(l2, fn x, acc -> x + acc end)
sum
sum = l |> Enum.map(fn x -> x * 3 end) |>
Enum.filter(fn x -> rem(x, 2) == 0 end) |>
Enum.reduce(fn x, acc -> x + acc end)
l = [1, 2, 3, 5, 6, 7, 8, 9, 10]
sum = 1..10 |> Enum.map(&(&1 * 3)) |>
Enum.filter(&(rem(&1, 2) == 0)) |>
Enum.reduce(&(&1 + &2))
1..10 |> Enum.map(&(&1 * 3)) |>
IO.inspect() |>
Enum.filter(&(rem(&1, 2) == 0)) |>
IO.inspect() |>
Enum.reduce(&(&1 + &2))
# ---------------------------------------
# Show code of the Ws2019.User
# > iex -S mix
Ws2019.User.new()
Ws2019.User.new(age: 44)
Ws2019.User.new(age: 44, tags: [:platinum, :metalhead, :senior_treehugger])
Ws2019.User.new(tags: [:platinum, :metalhead, :senior_treehugger], age: 44)
u = %Ws2019.User{}
is_map(u)
# Process
@doc """
- Send Message / receive message
- Link Monitor
- GenServer / GenStateMachine
"""
Process.list |> length
clear
self()
pid = spawn(fn ->
Process.sleep(1000)
IO.puts("DONE #{inspect(self())}")
end)
Process.list |> length
Process.alive?(pid)
# explain self and flush
send(self(), {:hey, 42})
parent = self
pid = spawn(fn ->
Process.sleep(1000)
IO.puts("DONE #{inspect(self())}")
send(parent, {:done, self()})
end)
Process.alive?(pid)
flush
# Process are isolated
clear
self
spawn(fn ->
Process.sleep(1000)
IO.puts("try to dived by Zero")
1 / 0
IO.puts("Never printed")
end)
self
flush
clear
flush
self
spawn_monitor(fn ->
Process.sleep(1000)
IO.puts("try to dived by Zero")
1 / 0
IO.puts("NEVER PRINTED!!!")
end)
# wait
self
flush
clear
self
spawn_link(fn ->
Process.sleep(1000)
IO.puts("try to dived by Zero")
1 / 0
IO.puts("ininite")
end)
# wait
self
# Talk about GenServer and show example of the account!!
# ---
# distribution
# > iex --sname server1
# > iex --sname server2
#on server 2
Node.list
Node.ping(:server1@tardis)
Node.list
#on server 1
Node.list
Agent.start(fn -> 42 end, name: :the_response)
Process.whereis :the_response
Agent.get(:the_response, fn state -> state end)
#on server 2
Process.whereis :the_response
Agent.get({:the_response, :server1@tardis}, fn state -> state end)
#on server 1
Process.whereis(:the_response) |> Process.exit(:kill)
Process.whereis(:the_response)
# Debugging
@doc """
- Observer
"""
:observer.start()
# in Logger kill the bottom right and then top