Skip to content

Commit 0317463

Browse files
authored
Format and clean up crash-course (#1638)
1 parent b0ec6d5 commit 0317463

File tree

1 file changed

+61
-49
lines changed

1 file changed

+61
-49
lines changed

crash-course.markdown

Lines changed: 61 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ hello() ->
2828
Add your functions to it, save it to disk, run `erl` from the same directory and execute the `compile` command:
2929

3030
```erl
31-
Eshell V5.9 (abort with ^G)
31+
Eshell V13.0.4 (abort with ^G)
3232
1> c(module_name).
3333
ok
3434
1> module_name:hello().
@@ -46,30 +46,37 @@ Elixir too has an interactive shell called `iex`. Compiling Elixir code can be d
4646
# module_name.ex
4747
defmodule ModuleName do
4848
def hello do
49-
IO.puts "Hello World"
49+
IO.puts("Hello world!")
5050
end
5151
end
5252
```
5353

5454
And compiled from `iex`:
5555

5656
```elixir
57-
Interactive Elixir
57+
Interactive Elixir (1.14.0) - press Ctrl+C to exit (type h() ENTER for help)
5858
iex> c("module_name.ex")
5959
[ModuleName]
60-
iex> ModuleName.hello
60+
iex> ModuleName.hello()
6161
Hello world!
6262
:ok
6363
```
6464

6565
However, notice that in Elixir you don't need to create a file only to create a new module; Elixir modules can be defined directly in the shell:
6666

6767
```elixir
68-
defmodule MyModule do
69-
def hello do
70-
IO.puts "Another Hello"
71-
end
72-
end
68+
iex> defmodule MyModule do
69+
...> def hello do
70+
...> IO.puts("Another Hello")
71+
...> end
72+
...> end
73+
{:module, MyModule,
74+
<<70, 79, 82, 49, 0, 0, 5, 136, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 187,
75+
0, 0, 0, 19, 15, 69, 108, 105, 120, 105, 114, 46, 77, 121, 77, 111, 100, 117,
76+
108, 101, 8, 95, 95, 105, 110, 102, 111, ...>>, {:hello, 0}}
77+
iex> MyModule.hello()
78+
Another Hello
79+
:ok
7380
```
7481

7582

@@ -218,11 +225,11 @@ is_atom(''). %=> true
218225
**Elixir**
219226

220227
```elixir
221-
is_atom :ok #=> true
222-
is_atom :'ok' #=> true
223-
is_atom Ok #=> true
224-
is_atom :"Multiple words" #=> true
225-
is_atom :"" #=> true
228+
is_atom(:ok) #=> true
229+
is_atom(:'ok') #=> true
230+
is_atom(Ok) #=> true
231+
is_atom(:"Multiple words") #=> true
232+
is_atom(:"") #=> true
226233
```
227234

228235
### Tuples
@@ -263,9 +270,9 @@ is_binary(<<"Hello">>). %=> true
263270
**Elixir**
264271

265272
```elixir
266-
is_list 'Hello' #=> true
267-
is_binary "Hello" #=> true
268-
is_binary <<"Hello">> #=> true
273+
is_list('Hello') #=> true
274+
is_binary("Hello") #=> true
275+
is_binary(<<"Hello">>) #=> true
269276
<<"Hello">> === "Hello" #=> true
270277
```
271278

@@ -274,11 +281,11 @@ In Elixir, the word **string** means a UTF-8 binary and there is a `String` modu
274281
Elixir also supports multiline strings (also called *heredocs*):
275282

276283
```elixir
277-
is_binary """
284+
is_binary("""
278285
This is a binary
279286
spanning several
280287
lines.
281-
"""
288+
""")
282289
#=> true
283290
```
284291

@@ -349,18 +356,18 @@ re:run("abc ", Pattern).
349356
**Elixir**
350357

351358
```elixir
352-
Regex.run ~r/abc\s/, "abc "
359+
Regex.run(~r/abc\s/, "abc ")
353360
#=> ["abc "]
354361
```
355362

356363
Regexes are also supported in heredocs, which is convenient when defining multiline regexes:
357364

358365
```elixir
359-
Regex.regex? ~r"""
366+
Regex.regex?(~r"""
360367
This is a regex
361368
spanning several
362369
lines.
363-
"""
370+
""")
364371
#=> true
365372
```
366373

@@ -394,12 +401,12 @@ An Elixir equivalent to the Erlang above:
394401
defmodule HelloModule do
395402
# A "Hello world" function
396403
def some_fun do
397-
IO.puts "Hello world!"
404+
IO.puts("Hello world!")
398405
end
399406

400407
# This one works only with lists
401408
def some_fun(list) when is_list(list) do
402-
IO.inspect list
409+
IO.inspect(list)
403410
end
404411

405412
# A private function
@@ -415,7 +422,7 @@ In Elixir, it is also possible to have multiple modules in one file, as well as
415422
defmodule HelloModule do
416423
defmodule Utils do
417424
def util do
418-
IO.puts "Utilize"
425+
IO.puts("Utilize")
419426
end
420427

421428
defp priv do
@@ -431,13 +438,14 @@ end
431438
defmodule ByeModule do
432439
end
433440

434-
HelloModule.dummy
441+
HelloModule.dummy()
435442
#=> :ok
436443

437-
HelloModule.Utils.util
438-
#=> "Utilize"
444+
HelloModule.Utils.util()
445+
# "Utilize"
446+
#=> :ok
439447

440-
HelloModule.Utils.priv
448+
HelloModule.Utils.priv()
441449
#=> ** (UndefinedFunctionError) undefined function: HelloModule.Utils.priv/0
442450
```
443451

@@ -467,8 +475,8 @@ loop_through([]) ->
467475

468476
```elixir
469477
def loop_through([head | tail]) do
470-
IO.inspect head
471-
loop_through tail
478+
IO.inspect(head)
479+
loop_through(tail)
472480
end
473481

474482
def loop_through([]) do
@@ -541,13 +549,13 @@ def sum(a, b) when is_binary(a) and is_binary(b) do
541549
a <> b
542550
end
543551

544-
sum 1, 2
552+
sum(1, 2)
545553
#=> 3
546554

547-
sum [1], [2]
555+
sum([1], [2])
548556
#=> [1, 2]
549557

550-
sum "a", "b"
558+
sum("a", "b")
551559
#=> "ab"
552560
```
553561

@@ -560,8 +568,8 @@ def mul_by(x, n \\ 2) do
560568
x * n
561569
end
562570

563-
mul_by 4, 3 #=> 12
564-
mul_by 4 #=> 8
571+
mul_by(4, 3) #=> 12
572+
mul_by(4) #=> 8
565573
```
566574

567575
### Anonymous functions
@@ -588,7 +596,7 @@ sum.(4, 3)
588596
#=> 7
589597

590598
square = fn x -> x * x end
591-
Enum.map [1, 2, 3, 4], square
599+
Enum.map([1, 2, 3, 4], square)
592600
#=> [1, 4, 9, 16]
593601
```
594602

@@ -614,11 +622,12 @@ F({a, b}).
614622

615623
```elixir
616624
f = fn
617-
{:a, :b} = tuple ->
618-
IO.puts "All your #{inspect tuple} are belong to us"
619-
[] ->
620-
"Empty"
621-
end
625+
{:a, :b} = tuple ->
626+
"All your #{inspect(tuple)} are belong to us"
627+
628+
[] ->
629+
"Empty"
630+
end
622631

623632
f.([])
624633
#=> "Empty"
@@ -659,7 +668,7 @@ defmodule Math do
659668
end
660669
end
661670

662-
Enum.map [1, 2, 3], &Math.square/1
671+
Enum.map([1, 2, 3], &Math.square/1)
663672
#=> [1, 4, 9]
664673
```
665674

@@ -669,10 +678,10 @@ Enum.map [1, 2, 3], &Math.square/1
669678
Elixir supports partial application of functions which can be used to define anonymous functions in a concise way:
670679

671680
```elixir
672-
Enum.map [1, 2, 3, 4], &(&1 * 2)
681+
Enum.map([1, 2, 3, 4], &(&1 * 2))
673682
#=> [2, 4, 6, 8]
674683

675-
List.foldl [1, 2, 3, 4], 0, &(&1 + &2)
684+
List.foldl([1, 2, 3, 4], 0, &(&1 + &2))
676685
#=> 10
677686
```
678687

@@ -685,7 +694,7 @@ defmodule Math do
685694
end
686695
end
687696

688-
Enum.map [1, 2, 3], &Math.square/1
697+
Enum.map([1, 2, 3], &Math.square/1)
689698
#=> [1, 4, 9]
690699
```
691700

@@ -749,14 +758,17 @@ Test_fun(10).
749758
**Elixir**
750759

751760
```elixir
752-
test_fun = fn(x) ->
761+
test_fun = fn x ->
753762
cond do
754763
x > 10 ->
755764
:greater_than_ten
765+
756766
x < 10 and x > 0 ->
757767
:less_than_ten_positive
768+
758769
x < 0 or x === 0 ->
759770
:zero_or_negative
771+
760772
true ->
761773
:exactly_ten
762774
end
@@ -810,9 +822,9 @@ end.
810822
**Elixir**
811823

812824
```elixir
813-
pid = Kernel.self
825+
pid = Kernel.self()
814826

815-
send pid, {:hello}
827+
send(pid, {:hello})
816828

817829
receive do
818830
{:hello} -> :ok

0 commit comments

Comments
 (0)