@@ -28,7 +28,7 @@ hello() ->
28
28
Add your functions to it, save it to disk, run ` erl ` from the same directory and execute the ` compile ` command:
29
29
30
30
``` erl
31
- Eshell V5 . 9 (abort with ^G )
31
+ Eshell V13 . 0.4 (abort with ^G )
32
32
1 > c (module_name ).
33
33
ok
34
34
1 > module_name :hello ().
@@ -46,30 +46,37 @@ Elixir too has an interactive shell called `iex`. Compiling Elixir code can be d
46
46
# module_name.ex
47
47
defmodule ModuleName do
48
48
def hello do
49
- IO .puts " Hello World "
49
+ IO .puts ( " Hello world! " )
50
50
end
51
51
end
52
52
```
53
53
54
54
And compiled from ` iex ` :
55
55
56
56
``` elixir
57
- Interactive Elixir
57
+ Interactive Elixir ( 1.14 . 0 ) - press Ctrl + C to exit (type h () ENTER for help)
58
58
iex> c (" module_name.ex" )
59
59
[ModuleName ]
60
- iex> ModuleName .hello
60
+ iex> ModuleName .hello ()
61
61
Hello world!
62
62
:ok
63
63
```
64
64
65
65
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:
66
66
67
67
``` 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
73
80
```
74
81
75
82
@@ -218,11 +225,11 @@ is_atom(''). %=> true
218
225
** Elixir**
219
226
220
227
``` 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
226
233
```
227
234
228
235
### Tuples
@@ -263,9 +270,9 @@ is_binary(<<"Hello">>). %=> true
263
270
** Elixir**
264
271
265
272
``` 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
269
276
<< " Hello" >> === " Hello" # => true
270
277
```
271
278
@@ -274,11 +281,11 @@ In Elixir, the word **string** means a UTF-8 binary and there is a `String` modu
274
281
Elixir also supports multiline strings (also called * heredocs* ):
275
282
276
283
``` elixir
277
- is_binary """
284
+ is_binary ( """
278
285
This is a binary
279
286
spanning several
280
287
lines.
281
- """
288
+ """ )
282
289
# => true
283
290
```
284
291
@@ -349,18 +356,18 @@ re:run("abc ", Pattern).
349
356
** Elixir**
350
357
351
358
``` elixir
352
- Regex .run ~r/ abc\s / , " abc "
359
+ Regex .run ( ~r/ abc\s / , " abc " )
353
360
# => ["abc "]
354
361
```
355
362
356
363
Regexes are also supported in heredocs, which is convenient when defining multiline regexes:
357
364
358
365
``` elixir
359
- Regex .regex? ~r"""
366
+ Regex .regex? ( ~r"""
360
367
This is a regex
361
368
spanning several
362
369
lines.
363
- """
370
+ """ )
364
371
# => true
365
372
```
366
373
@@ -394,12 +401,12 @@ An Elixir equivalent to the Erlang above:
394
401
defmodule HelloModule do
395
402
# A "Hello world" function
396
403
def some_fun do
397
- IO .puts " Hello world!"
404
+ IO .puts ( " Hello world!" )
398
405
end
399
406
400
407
# This one works only with lists
401
408
def some_fun (list) when is_list (list) do
402
- IO .inspect list
409
+ IO .inspect ( list)
403
410
end
404
411
405
412
# A private function
@@ -415,7 +422,7 @@ In Elixir, it is also possible to have multiple modules in one file, as well as
415
422
defmodule HelloModule do
416
423
defmodule Utils do
417
424
def util do
418
- IO .puts " Utilize"
425
+ IO .puts ( " Utilize" )
419
426
end
420
427
421
428
defp priv do
@@ -431,13 +438,14 @@ end
431
438
defmodule ByeModule do
432
439
end
433
440
434
- HelloModule .dummy
441
+ HelloModule .dummy ()
435
442
# => :ok
436
443
437
- HelloModule .Utils .util
438
- # => "Utilize"
444
+ HelloModule .Utils .util ()
445
+ # "Utilize"
446
+ # => :ok
439
447
440
- HelloModule .Utils .priv
448
+ HelloModule .Utils .priv ()
441
449
# => ** (UndefinedFunctionError) undefined function: HelloModule.Utils.priv/0
442
450
```
443
451
@@ -467,8 +475,8 @@ loop_through([]) ->
467
475
468
476
``` elixir
469
477
def loop_through ([head | tail]) do
470
- IO .inspect head
471
- loop_through tail
478
+ IO .inspect ( head)
479
+ loop_through ( tail)
472
480
end
473
481
474
482
def loop_through ([]) do
@@ -541,13 +549,13 @@ def sum(a, b) when is_binary(a) and is_binary(b) do
541
549
a <> b
542
550
end
543
551
544
- sum 1 , 2
552
+ sum ( 1 , 2 )
545
553
# => 3
546
554
547
- sum [1 ], [2 ]
555
+ sum ( [1 ], [2 ])
548
556
# => [1, 2]
549
557
550
- sum " a" , " b"
558
+ sum ( " a" , " b" )
551
559
# => "ab"
552
560
```
553
561
@@ -560,8 +568,8 @@ def mul_by(x, n \\ 2) do
560
568
x * n
561
569
end
562
570
563
- mul_by 4 , 3 # => 12
564
- mul_by 4 # => 8
571
+ mul_by ( 4 , 3 ) # => 12
572
+ mul_by ( 4 ) # => 8
565
573
```
566
574
567
575
### Anonymous functions
@@ -588,7 +596,7 @@ sum.(4, 3)
588
596
# => 7
589
597
590
598
square = fn x -> x * x end
591
- Enum .map [1 , 2 , 3 , 4 ], square
599
+ Enum .map ( [1 , 2 , 3 , 4 ], square)
592
600
# => [1, 4, 9, 16]
593
601
```
594
602
@@ -614,11 +622,12 @@ F({a, b}).
614
622
615
623
``` elixir
616
624
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
622
631
623
632
f .([])
624
633
# => "Empty"
@@ -659,7 +668,7 @@ defmodule Math do
659
668
end
660
669
end
661
670
662
- Enum .map [1 , 2 , 3 ], & Math .square / 1
671
+ Enum .map ( [1 , 2 , 3 ], & Math .square / 1 )
663
672
# => [1, 4, 9]
664
673
```
665
674
@@ -669,10 +678,10 @@ Enum.map [1, 2, 3], &Math.square/1
669
678
Elixir supports partial application of functions which can be used to define anonymous functions in a concise way:
670
679
671
680
``` elixir
672
- Enum .map [1 , 2 , 3 , 4 ], & (&1 * 2 )
681
+ Enum .map ( [1 , 2 , 3 , 4 ], & (&1 * 2 ) )
673
682
# => [2, 4, 6, 8]
674
683
675
- List .foldl [1 , 2 , 3 , 4 ], 0 , & (&1 + &2 )
684
+ List .foldl ( [1 , 2 , 3 , 4 ], 0 , & (&1 + &2 ) )
676
685
# => 10
677
686
```
678
687
@@ -685,7 +694,7 @@ defmodule Math do
685
694
end
686
695
end
687
696
688
- Enum .map [1 , 2 , 3 ], & Math .square / 1
697
+ Enum .map ( [1 , 2 , 3 ], & Math .square / 1 )
689
698
# => [1, 4, 9]
690
699
```
691
700
@@ -749,14 +758,17 @@ Test_fun(10).
749
758
** Elixir**
750
759
751
760
``` elixir
752
- test_fun = fn (x) - >
761
+ test_fun = fn x ->
753
762
cond do
754
763
x > 10 ->
755
764
:greater_than_ten
765
+
756
766
x < 10 and x > 0 ->
757
767
:less_than_ten_positive
768
+
758
769
x < 0 or x === 0 ->
759
770
:zero_or_negative
771
+
760
772
true ->
761
773
:exactly_ten
762
774
end
810
822
** Elixir**
811
823
812
824
``` elixir
813
- pid = Kernel .self
825
+ pid = Kernel .self ()
814
826
815
- send pid, {:hello }
827
+ send ( pid, {:hello })
816
828
817
829
receive do
818
830
{:hello } -> :ok
0 commit comments