You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: getting_started/2.markdown
+13-23
Original file line number
Diff line number
Diff line change
@@ -172,7 +172,7 @@ When discussing lists earlier we saw the following example:
172
172
iex> t
173
173
[2, 3]
174
174
175
-
In Elixir, `=`does not mean assignment as in programming languages like Java and Ruby. `=` is actually a match operator which will check if the expressions on both left and right side match. Consider this example:
175
+
In Elixir, `=`is not an assignment as in programming languages like Java, Ruby, Python, etc. `=` is actually a match operator which will check if the expressions on both left and right side match. Consider this example:
176
176
177
177
iex> { 1, 2, 3 } = { 1, 2, 3 }
178
178
{ 1, 2, 3 }
@@ -249,36 +249,22 @@ The second example uses the block syntax. We can also use `else` in the block sy
249
249
10 + 3
250
250
end
251
251
252
-
Elixir blocks work similarly to Ruby blocks. For example, a developer could add parentheses as follow:
252
+
It is important to notice that `do`/`end` always binds to the farthest function call. For example, the following expression:
253
253
254
-
if(false) do
254
+
is_number if true do
255
255
1 + 2
256
-
else
257
-
10 + 3
258
-
end
259
-
260
-
It is also important to notice that `do`/`end` always binds to the farthest function call. For example, the following expression:
261
-
262
-
Enum.map [1,2,3], fn(x) do
263
-
x * 2
264
256
end
265
257
266
258
Would be parsed as:
267
259
268
-
Enum.map([1,2,3], fn(x)) do
269
-
x * 2
270
-
end
271
-
272
-
Which is not what we want since `do` is binding to the farthest function call; in this case `Enum.map`. For this reason, we always use the `->` syntax for functions and reserve `do/end` for the remaining control structures:
273
-
274
-
Enum.map [1,2,3], fn(x) ->
275
-
x * 2
260
+
is_number(if true) do
261
+
1 + 2
276
262
end
277
263
278
-
Which is then parsed as:
264
+
Which is not what we want since `do` is binding to the farthest function call, in this case `is_number`. Adding explicit parenthesis is enough to resolve the ambiguity:
279
265
280
-
Enum.map([1,2,3], fn(x) ->
281
-
x * 2
266
+
is_number(if true do
267
+
1 + 2
282
268
end)
283
269
284
270
## 2.7 Control flow structures
@@ -451,7 +437,11 @@ In Elixir, creating a function is similar to the `case` mechanism we have just s
451
437
452
438
A function with one clause can be defined with a (preferred) shortcut syntax as follows:
453
439
454
-
function = fn x, y -> x + y end
440
+
f = fn x, y -> x + y end
441
+
442
+
f = fn x, y ->
443
+
x + y
444
+
end
455
445
456
446
This is the syntax that's going to be used throughout this guide.
Copy file name to clipboardExpand all lines: getting_started/5.markdown
+1-1
Original file line number
Diff line number
Diff line change
@@ -55,7 +55,7 @@ With those basic structures in mind, we are ready to define our own macro.
55
55
56
56
## 5.2 Defining our own macro
57
57
58
-
A macro can be defined using `defmacro`. For instance, in just a few lines of code we can define a macro called `unless` which works the same way Ruby's `unless` does:
58
+
A macro can be defined using `defmacro`. For instance, in just a few lines of code we can define a macro called `unless` which does the opposite of `if`:
0 commit comments