-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotes.norg
More file actions
177 lines (130 loc) · 4.59 KB
/
Copy pathnotes.norg
File metadata and controls
177 lines (130 loc) · 4.59 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
### Elixir notes.
Implicit returns:
Elixir has a thing called implicit returns, so in a function, the last value in the function automatically gets returned.
Iex recompile:
To recompile the iex shell, you can run the command recompile.
Errors:
** (UndefinedFunctionError) function Cards.shuffle/0 is undefined or private. Did you mean:
* shuffle/1
(cards 0.1.0) Cards.shuffle()
iex:3: (file)
In elixir, a module can have multiple methods with the same name, that take in different lengths or values of arguments.
What the error above is showing, is that someone tried to call the `shuffle` method with 0 arguments but 1 argument is required as showed in `*shuffle/1` meaning 1 argument.
Stdlib:
The elixir standard library documentation you can find all sorts of methods for all data types.
Immutability:
Everything in Elixir is immutable, meaning that you never change existing data structure, you simply make copies of it.
Do nothing:
Similar to Python's pass, you can add :ok inside a function to do nothing and recompile without errors.
Comprehensions:
You can use List.flatten to flatten out nested lists
You can use a comprehension loop on multiple lists. Ex:
```elixir
for item <- items, value <- values do
"#{item}, #{value}"
end
```
Accessing elements in a list:
```elixir
[color1, color2] = ["blue", "red"]
```
This will match the first element in the left hand array to the first element in the right hand array and assign the value from the right hand side to the variable in the left hand side list.
Accessing elements in a tuple:
```elixir
{status, data} = Some.read()
```
Compilation:
Elixir -> Transpiled to Erlang -> Compiled and executed on the BEAM (Bogdan Erland Abstrac Machine)
BEAM:
BEAM is a virtual machine that executes the Elixir code similar to the JVM in Java
Erlang:
You can run erlang code with ease in Elixir like so.
```elixir
:erlang.function_to_call()
```
Logic checks:
When possible, avoid using if statements and use case statements instead.
```elixir
# Bad
{status, data} = some_function_that_returns_tuple()
if status == :error do
return "Some error"
else
return data
end
```
```elixir
# Good
{status, data} = some_function_that_returns_tuple()
case status do
:ok -> data
:error -> raise SomeException
end
```
Utilize elixir's pattern matching features.
Pipe operator.
A pipe operator can be used to chain method calls in a clean and concise way.
```elixir
Cards.create_deck()
|> Cards.shuffle()
|> Cards.deal(hand_size)
```
When calling a function using the pipe operator, the chained function first argument is the result of the previous function call.
Anonymous functions:
In Elixir, anonymous functions are treated as a first class citizen. Meaning that:
- Named and anonymous functions can be assigned to variables.
- Named and anonymous functions can be passed around like data as arguments
and return values
- Anonymous functions can be created dynamically.
Doctests:
Elixir will look for a @doc string and an # Examples block to execute blocktest.
- Example
```elixir
@doc """
This is an example docstring with an included doctest.
# Examples
iex > Module.some_function
|> another_funcion
|> third_function
[return_value]
"""
```
Atoms:
- An Atom is a constant where it's name is the value
A practical use of atoms are when you have to reuse a string value.
Ints:
- A nice syntastic sugar for large integers is seperating underscores
```elixir
# Without sugar
one_million = 1000000
# With sugar
one_million = 1_000_000 # Will output the same as above
```
Strings:
- String values are defined with the double quotes.
```elixir
first_name = "Ragnar"
```
- To concat strings you use the <> operator
```elixir
full_name = "Ragnar " <> "Olafsson" # Will return Ragnar Olafsson
```
- Another way to do concat operation is string interpolation
```elixir
first_name = "Ragnar"
last_name = "Olafsson"
IO.puts("#{first_name} #{last_name}") # Will return Ragnar Olafsson
```
Booleans:
- You have the basic true and false booleans.
- To represent empty valeus you have nil
- Under the hood, booleans are represented as :atoms
Conditionals:
- The only falsy values in Elixir is false and nil, everything else is truthy.
Lists:
- Lists in Elixir are singly linked lists
- Linked lists are represented as such [value, address_to_node_two] -> [value, address_to_node_tree] ... until the end of the list.
```elixir
list = [1,2,3]
IO.puts list # Will return [1,2,3]
```