Skip to content

Commit 9c56121

Browse files
Add doctests to Kernel module (#14450)
1 parent 0fb90f1 commit 9c56121

File tree

1 file changed

+88
-64
lines changed

1 file changed

+88
-64
lines changed

lib/elixir/lib/kernel.ex

+88-64
Original file line numberDiff line numberDiff line change
@@ -497,14 +497,14 @@ defmodule Kernel do
497497
498498
## Examples
499499
500-
div(5, 2)
501-
#=> 2
500+
iex> div(5, 2)
501+
2
502502
503-
div(6, -4)
504-
#=> -1
503+
iex> div(6, -4)
504+
-1
505505
506-
div(-99, 2)
507-
#=> -49
506+
iex> div(-99, 2)
507+
-49
508508
509509
div(100, 0)
510510
** (ArithmeticError) bad argument in arithmetic expression
@@ -619,11 +619,11 @@ defmodule Kernel do
619619
620620
## Examples
621621
622-
hd([1, 2, 3, 4])
623-
#=> 1
622+
iex> hd([1, 2, 3, 4])
623+
1
624624
625-
hd([1 | 2])
626-
#=> 1
625+
iex> hd([1 | 2])
626+
1
627627
628628
Giving it an empty list raises:
629629
@@ -1153,12 +1153,12 @@ defmodule Kernel do
11531153
11541154
## Examples
11551155
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
11621162
11631163
"""
11641164
@spec spawn((-> any)) :: pid
@@ -1206,12 +1206,12 @@ defmodule Kernel do
12061206
12071207
## Examples
12081208
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
12151215
12161216
"""
12171217
@spec spawn_link((-> any)) :: pid
@@ -1260,8 +1260,12 @@ defmodule Kernel do
12601260
12611261
## Examples
12621262
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
12651269
12661270
"""
12671271
@spec spawn_monitor((-> any)) :: {pid, reference}
@@ -1347,17 +1351,17 @@ defmodule Kernel do
13471351
13481352
## Examples
13491353
1350-
tl([1, 2, 3, :go])
1351-
#=> [2, 3, :go]
1354+
iex> tl([1, 2, 3, :go])
1355+
[2, 3, :go]
13521356
1353-
tl([:one])
1354-
#=> []
1357+
iex> tl([:one])
1358+
[]
13551359
1356-
tl([:a, :b | :improper_end])
1357-
#=> [:b | :improper_end]
1360+
iex> tl([:a, :b | :improper_end])
1361+
[:b | :improper_end]
13581362
1359-
tl([:a | %{b: 1}])
1360-
#=> %{b: 1}
1363+
iex> tl([:a | %{b: 1}])
1364+
%{b: 1}
13611365
13621366
Giving it an empty list raises:
13631367
@@ -1524,14 +1528,14 @@ defmodule Kernel do
15241528
15251529
## Examples
15261530
1527-
1 / 2
1528-
#=> 0.5
1531+
iex> 1 / 2
1532+
0.5
15291533
1530-
-3.0 / 2.0
1531-
#=> -1.5
1534+
iex> -3.0 / 2.0
1535+
-1.5
15321536
1533-
5 / 1
1534-
#=> 5.0
1537+
iex> 5 / 1
1538+
5.0
15351539
15361540
7 / 0
15371541
** (ArithmeticError) bad argument in arithmetic expression
@@ -1873,9 +1877,9 @@ defmodule Kernel do
18731877
18741878
## Examples
18751879
1876-
tuple = {:foo, :bar, 3}
1877-
elem(tuple, 1)
1878-
#=> :bar
1880+
iex> tuple = {:foo, :bar, 3}
1881+
iex> elem(tuple, 1)
1882+
:bar
18791883
18801884
elem({}, 0)
18811885
** (ArgumentError) argument error
@@ -2210,12 +2214,13 @@ defmodule Kernel do
22102214
22112215
## Examples
22122216
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
22192224
22202225
"""
22212226
defmacro reraise(message, stacktrace) do
@@ -3480,17 +3485,17 @@ defmodule Kernel do
34803485
34813486
Remember the pin operator matches _values_, not _patterns_.
34823487
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:
34843489
3485-
# don't do this
34863490
pattern = %{a: :a}
34873491
match?(pattern, %{b: :b})
3492+
#=> true
34883493
34893494
Similarly, moving an expression out the pattern may no longer preserve
34903495
its semantics. For example:
34913496
3492-
match?([_ | _], [1, 2, 3])
3493-
#=> true
3497+
iex> match?([_ | _], [1, 2, 3])
3498+
true
34943499
34953500
pattern = [_ | _]
34963501
match?(pattern, [1, 2, 3])
@@ -3499,12 +3504,12 @@ defmodule Kernel do
34993504
Another example is that a map as a pattern performs a subset match, but not
35003505
once assigned to a variable:
35013506
3502-
match?(%{x: 1}, %{x: 1, y: 2})
3503-
#=> true
3507+
iex> match?(%{x: 1}, %{x: 1, y: 2})
3508+
true
35043509
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
35083513
35093514
The pin operator will check if the values are equal, using `===/2`, while
35103515
patterns have their own rules when matching maps, lists, and so forth.
@@ -3921,6 +3926,21 @@ defmodule Kernel do
39213926
baz
39223927
end
39233928
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+
39243944
If you find yourself nesting conditionals inside conditionals,
39253945
consider using `cond/1`.
39263946
"""
@@ -4021,8 +4041,10 @@ defmodule Kernel do
40214041
The left-hand side supports any expression you would use
40224042
on the left-hand side of a match:
40234043
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}
40264048
40274049
The example above will only work if `x` matches the first value in the right
40284050
list. Otherwise, it will raise a `MatchError` (like the `=` operator would
@@ -4363,20 +4385,22 @@ defmodule Kernel do
43634385
call. Therefore, to pipe into an anonymous function, you need to
43644386
invoke it:
43654387
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"
43684391
43694392
Alternatively, you can use `then/2` for the same effect:
43704393
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"
43734397
43744398
`then/2` is most commonly used when you want to pipe to a function
43754399
but the value is expected outside of the first argument, such as
43764400
above. By replacing `some_fun` by its value, we get:
43774401
4378-
"Hello" |> then(&Regex.replace(~r/l/, &1, "L"))
4379-
4402+
iex> "Hello" |> then(&Regex.replace(~r/l/, &1, "L"))
4403+
"HeLLo"
43804404
"""
43814405
defmacro left |> right do
43824406
fun = fn {x, pos}, acc ->

0 commit comments

Comments
 (0)