@@ -497,14 +497,14 @@ defmodule Kernel do
497
497
498
498
## Examples
499
499
500
- div(5, 2)
501
- #=> 2
500
+ iex> div(5, 2)
501
+ 2
502
502
503
- div(6, -4)
504
- #=> -1
503
+ iex> div(6, -4)
504
+ -1
505
505
506
- div(-99, 2)
507
- #=> -49
506
+ iex> div(-99, 2)
507
+ -49
508
508
509
509
div(100, 0)
510
510
** (ArithmeticError) bad argument in arithmetic expression
@@ -619,11 +619,11 @@ defmodule Kernel do
619
619
620
620
## Examples
621
621
622
- hd([1, 2, 3, 4])
623
- #=> 1
622
+ iex> hd([1, 2, 3, 4])
623
+ 1
624
624
625
- hd([1 | 2])
626
- #=> 1
625
+ iex> hd([1 | 2])
626
+ 1
627
627
628
628
Giving it an empty list raises:
629
629
@@ -1153,12 +1153,12 @@ defmodule Kernel do
1153
1153
1154
1154
## Examples
1155
1155
1156
- current = self()
1157
- child = spawn(fn -> send(current, {self(), 1 + 2}) end)
1158
-
1159
- receive do
1160
- {^child, 3} -> IO.puts("Received 3 back")
1161
- end
1156
+ iex> current = self()
1157
+ iex> child = spawn(fn -> send(current, {self(), 1 + 2}) end)
1158
+ iex> receive do
1159
+ ...> {^child, 3} -> :ok
1160
+ ...> end
1161
+ :ok
1162
1162
1163
1163
"""
1164
1164
@ spec spawn ( ( -> any ) ) :: pid
@@ -1206,12 +1206,12 @@ defmodule Kernel do
1206
1206
1207
1207
## Examples
1208
1208
1209
- current = self()
1210
- child = spawn_link(fn -> send(current, {self(), 1 + 2}) end)
1211
-
1212
- receive do
1213
- {^child, 3} -> IO.puts("Received 3 back")
1214
- end
1209
+ iex> current = self()
1210
+ iex> child = spawn_link(fn -> send(current, {self(), 1 + 2}) end)
1211
+ iex> receive do
1212
+ ...> {^child, 3} -> :ok
1213
+ ...> end
1214
+ :ok
1215
1215
1216
1216
"""
1217
1217
@ spec spawn_link ( ( -> any ) ) :: pid
@@ -1260,8 +1260,12 @@ defmodule Kernel do
1260
1260
1261
1261
## Examples
1262
1262
1263
- current = self()
1264
- spawn_monitor(fn -> send(current, {self(), 1 + 2}) end)
1263
+ iex> current = self()
1264
+ iex> {child, _ref} = spawn_monitor(fn -> send(current, {self(), 1 + 2}) end)
1265
+ iex> receive do
1266
+ ...> {^child, 3} -> :ok
1267
+ ...> end
1268
+ :ok
1265
1269
1266
1270
"""
1267
1271
@ spec spawn_monitor ( ( -> any ) ) :: { pid , reference }
@@ -1347,17 +1351,17 @@ defmodule Kernel do
1347
1351
1348
1352
## Examples
1349
1353
1350
- tl([1, 2, 3, :go])
1351
- #=> [2, 3, :go]
1354
+ iex> tl([1, 2, 3, :go])
1355
+ [2, 3, :go]
1352
1356
1353
- tl([:one])
1354
- #=> []
1357
+ iex> tl([:one])
1358
+ []
1355
1359
1356
- tl([:a, :b | :improper_end])
1357
- #=> [:b | :improper_end]
1360
+ iex> tl([:a, :b | :improper_end])
1361
+ [:b | :improper_end]
1358
1362
1359
- tl([:a | %{b: 1}])
1360
- #=> %{b: 1}
1363
+ iex> tl([:a | %{b: 1}])
1364
+ %{b: 1}
1361
1365
1362
1366
Giving it an empty list raises:
1363
1367
@@ -1524,14 +1528,14 @@ defmodule Kernel do
1524
1528
1525
1529
## Examples
1526
1530
1527
- 1 / 2
1528
- #=> 0.5
1531
+ iex> 1 / 2
1532
+ 0.5
1529
1533
1530
- -3.0 / 2.0
1531
- #=> -1.5
1534
+ iex> -3.0 / 2.0
1535
+ -1.5
1532
1536
1533
- 5 / 1
1534
- #=> 5.0
1537
+ iex> 5 / 1
1538
+ 5.0
1535
1539
1536
1540
7 / 0
1537
1541
** (ArithmeticError) bad argument in arithmetic expression
@@ -1873,9 +1877,9 @@ defmodule Kernel do
1873
1877
1874
1878
## Examples
1875
1879
1876
- tuple = {:foo, :bar, 3}
1877
- elem(tuple, 1)
1878
- #=> :bar
1880
+ iex> tuple = {:foo, :bar, 3}
1881
+ iex> elem(tuple, 1)
1882
+ :bar
1879
1883
1880
1884
elem({}, 0)
1881
1885
** (ArgumentError) argument error
@@ -2210,12 +2214,13 @@ defmodule Kernel do
2210
2214
2211
2215
## Examples
2212
2216
2213
- try do
2214
- raise "oops"
2215
- rescue
2216
- exception ->
2217
- reraise exception, __STACKTRACE__
2218
- end
2217
+ iex> try do
2218
+ ...> raise "oops"
2219
+ ...> rescue
2220
+ ...> exception ->
2221
+ ...> reraise exception, __STACKTRACE__
2222
+ ...> end
2223
+ ** (RuntimeError) oops
2219
2224
2220
2225
"""
2221
2226
defmacro reraise ( message , stacktrace ) do
@@ -3480,17 +3485,17 @@ defmodule Kernel do
3480
3485
3481
3486
Remember the pin operator matches _values_, not _patterns_.
3482
3487
Passing a variable as the pattern will always return `true` and will
3483
- result in a warning that the variable is unused:
3488
+ result in a warning that the variable is unused. Don't do this :
3484
3489
3485
- # don't do this
3486
3490
pattern = %{a: :a}
3487
3491
match?(pattern, %{b: :b})
3492
+ #=> true
3488
3493
3489
3494
Similarly, moving an expression out the pattern may no longer preserve
3490
3495
its semantics. For example:
3491
3496
3492
- match?([_ | _], [1, 2, 3])
3493
- #=> true
3497
+ iex> match?([_ | _], [1, 2, 3])
3498
+ true
3494
3499
3495
3500
pattern = [_ | _]
3496
3501
match?(pattern, [1, 2, 3])
@@ -3499,12 +3504,12 @@ defmodule Kernel do
3499
3504
Another example is that a map as a pattern performs a subset match, but not
3500
3505
once assigned to a variable:
3501
3506
3502
- match?(%{x: 1}, %{x: 1, y: 2})
3503
- #=> true
3507
+ iex> match?(%{x: 1}, %{x: 1, y: 2})
3508
+ true
3504
3509
3505
- attrs = %{x: 1}
3506
- match?(^attrs, %{x: 1, y: 2})
3507
- #=> false
3510
+ iex> attrs = %{x: 1}
3511
+ iex> match?(^attrs, %{x: 1, y: 2})
3512
+ false
3508
3513
3509
3514
The pin operator will check if the values are equal, using `===/2`, while
3510
3515
patterns have their own rules when matching maps, lists, and so forth.
@@ -3921,6 +3926,21 @@ defmodule Kernel do
3921
3926
baz
3922
3927
end
3923
3928
3929
+ ## Examples
3930
+
3931
+ iex> if 5 > 7 do
3932
+ ...> "This will never be returned"
3933
+ ...> else
3934
+ ...> "This will be returned"
3935
+ ...> end
3936
+ "This will be returned"
3937
+
3938
+ iex> if 2 + 2 == 4, do: :correct
3939
+ :correct
3940
+
3941
+ iex> if 2 + 2 == 5, do: :correct
3942
+ nil
3943
+
3924
3944
If you find yourself nesting conditionals inside conditionals,
3925
3945
consider using `cond/1`.
3926
3946
"""
@@ -4021,8 +4041,10 @@ defmodule Kernel do
4021
4041
The left-hand side supports any expression you would use
4022
4042
on the left-hand side of a match:
4023
4043
4024
- x = 1
4025
- destructure([^x, y, z], [1, 2, 3])
4044
+ iex> x = 1
4045
+ iex> destructure([^x, y, z], [1, 2, 3])
4046
+ iex> {x, y, z}
4047
+ {1, 2, 3}
4026
4048
4027
4049
The example above will only work if `x` matches the first value in the right
4028
4050
list. Otherwise, it will raise a `MatchError` (like the `=` operator would
@@ -4363,20 +4385,22 @@ defmodule Kernel do
4363
4385
call. Therefore, to pipe into an anonymous function, you need to
4364
4386
invoke it:
4365
4387
4366
- some_fun = &Regex.replace(~r/l/, &1, "L")
4367
- "Hello" |> some_fun.()
4388
+ iex> some_fun = &Regex.replace(~r/l/, &1, "L")
4389
+ iex> "Hello" |> some_fun.()
4390
+ "HeLLo"
4368
4391
4369
4392
Alternatively, you can use `then/2` for the same effect:
4370
4393
4371
- some_fun = &Regex.replace(~r/l/, &1, "L")
4372
- "Hello" |> then(some_fun)
4394
+ iex> some_fun = &Regex.replace(~r/l/, &1, "L")
4395
+ iex> "Hello" |> then(some_fun)
4396
+ "HeLLo"
4373
4397
4374
4398
`then/2` is most commonly used when you want to pipe to a function
4375
4399
but the value is expected outside of the first argument, such as
4376
4400
above. By replacing `some_fun` by its value, we get:
4377
4401
4378
- "Hello" |> then(&Regex.replace(~r/l/, &1, "L"))
4379
-
4402
+ iex> "Hello" |> then(&Regex.replace(~r/l/, &1, "L"))
4403
+ "HeLLo"
4380
4404
"""
4381
4405
defmacro left |> right do
4382
4406
fun = fn { x , pos } , acc ->
0 commit comments