Skip to content

Commit 6081b34

Browse files
author
José Valim
committed
Improvements to the getting started guide
1 parent 243984e commit 6081b34

File tree

2 files changed

+14
-24
lines changed

2 files changed

+14
-24
lines changed

getting_started/2.markdown

+13-23
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ When discussing lists earlier we saw the following example:
172172
iex> t
173173
[2, 3]
174174

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:
176176

177177
iex> { 1, 2, 3 } = { 1, 2, 3 }
178178
{ 1, 2, 3 }
@@ -249,36 +249,22 @@ The second example uses the block syntax. We can also use `else` in the block sy
249249
10 + 3
250250
end
251251

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:
253253

254-
if(false) do
254+
is_number if true do
255255
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
264256
end
265257

266258
Would be parsed as:
267259

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
276262
end
277263

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:
279265

280-
Enum.map([1,2,3], fn(x) ->
281-
x * 2
266+
is_number(if true do
267+
1 + 2
282268
end)
283269

284270
## 2.7 Control flow structures
@@ -451,7 +437,11 @@ In Elixir, creating a function is similar to the `case` mechanism we have just s
451437

452438
A function with one clause can be defined with a (preferred) shortcut syntax as follows:
453439

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
455445

456446
This is the syntax that's going to be used throughout this guide.
457447

getting_started/5.markdown

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ With those basic structures in mind, we are ready to define our own macro.
5555

5656
## 5.2 Defining our own macro
5757

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`:
5959

6060
{% highlight ruby %}
6161
defmodule MyMacro do

0 commit comments

Comments
 (0)