Skip to content

Commit

Permalink
Convert some julia-repl examples to doctests (#57452)
Browse files Browse the repository at this point in the history
Some progress towards #56921.
  • Loading branch information
lgoettgens authored Feb 19, 2025
1 parent b9a8d46 commit e67a22c
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 26 deletions.
2 changes: 1 addition & 1 deletion base/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ BitVector() = BitVector(undef, 0)
Construct a `BitVector` from a tuple of `Bool`.
# Examples
```julia-repl
```jldoctest
julia> nt = (true, false, true, false)
(true, false, true, false)
Expand Down
4 changes: 2 additions & 2 deletions base/essentials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ f(y) = [x for x in y]
# Examples
```julia-repl
```jldoctest; setup = :(using InteractiveUtils)
julia> f(A::AbstractArray) = g(A)
f (generic function with 1 method)
Expand All @@ -102,7 +102,7 @@ g (generic function with 1 method)
julia> @code_typed f([1.0])
CodeInfo(
1 ─ %1 = invoke Main.g(_2::AbstractArray)::Float64
1 ─ %1 = invoke g(A::AbstractArray)::Float64
└── return %1
) => Float64
```
Expand Down
8 changes: 4 additions & 4 deletions base/expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ Take the expression `x` and return an equivalent expression with all macros remo
for executing in module `m`.
The `recursive` keyword controls whether deeper levels of nested macros are also expanded.
This is demonstrated in the example below:
```julia-repl
```jldoctest; filter = r"#= .*:6 =#"
julia> module M
macro m1()
42
Expand All @@ -118,7 +118,7 @@ julia> macroexpand(M, :(@m2()), recursive=true)
42
julia> macroexpand(M, :(@m2()), recursive=false)
:(#= REPL[16]:6 =# M.@m1)
:(#= REPL[1]:6 =# @m1)
```
"""
function macroexpand(m::Module, @nospecialize(x); recursive=true)
Expand Down Expand Up @@ -926,7 +926,7 @@ This can be used to limit the number of compiler-generated specializations durin
# Examples
```julia
```jldoctest; setup = :(using InteractiveUtils)
julia> f(A::AbstractArray) = g(A)
f (generic function with 1 method)
Expand All @@ -935,7 +935,7 @@ g (generic function with 1 method)
julia> @code_typed f([1.0])
CodeInfo(
1 ─ %1 = invoke Main.g(_2::AbstractArray)::Any
1 ─ %1 = invoke g(A::AbstractArray)::Any
└── return %1
) => Any
```
Expand Down
2 changes: 1 addition & 1 deletion base/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ otherwise it searches all recursive dependencies (from the resolved manifest of
each environment) until it locates the context `where`, and from there
identifies the dependency with the corresponding name.
```julia-repl
```jldoctest
julia> Base.identify_package("Pkg") # Pkg is a dependency of the default environment
Pkg [44cfe95a-1eb2-52ea-b672-e2afdf69b78f]
Expand Down
6 changes: 3 additions & 3 deletions base/twiceprecision.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ representation, even though it is exact from the standpoint of binary
representation.
Example:
```julia-repl
```jldoctest
julia> 1.0 + 1.0001e-15
1.000000000000001
Expand Down Expand Up @@ -94,7 +94,7 @@ numbers. Mathematically, `zhi + zlo = x * y`, where `zhi` contains the
most significant bits and `zlo` the least significant.
Example:
```julia-repl
```jldoctest
julia> x = Float32(π)
3.1415927f0
Expand Down Expand Up @@ -126,7 +126,7 @@ numbers. Mathematically, `zhi + zlo ≈ x / y`, where `zhi` contains the
most significant bits and `zlo` the least significant.
Example:
```julia-repl
```jldoctest
julia> x, y = Float32(π), 3.1f0
(3.1415927f0, 3.1f0)
Expand Down
10 changes: 5 additions & 5 deletions doc/src/manual/arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -1160,7 +1160,7 @@ dimension `d`. For example, the builtin `Array` returned by `rand(5,7,2)` has it
arranged contiguously in column major order. This means that the stride of the first
dimension — the spacing between elements in the same column — is `1`:

```julia-repl
```jldoctest strides
julia> A = rand(5, 7, 2);
julia> stride(A, 1)
Expand All @@ -1172,7 +1172,7 @@ as many elements as there are in a single column (`5`). Similarly, jumping betwe
"pages" (in the third dimension) requires skipping `5*7 == 35` elements. The [`strides`](@ref)
of this array is the tuple of these three numbers together:

```julia-repl
```jldoctest strides
julia> strides(A)
(1, 5, 35)
```
Expand All @@ -1185,7 +1185,7 @@ This view `V` refers to the same memory as `A` but is skipping and re-arranging
elements. The stride of the first dimension of `V` is `3` because we're only selecting every
third row from our original array:

```julia-repl
```jldoctest strides
julia> V = @view A[1:3:4, 2:2:6, 2:-1:1];
julia> stride(V, 1)
Expand All @@ -1196,7 +1196,7 @@ This view is similarly selecting every other column from our original `A` — an
needs to skip the equivalent of two five-element columns when moving between indices in the
second dimension:

```julia-repl
```jldoctest strides
julia> stride(V, 2)
10
```
Expand All @@ -1205,7 +1205,7 @@ The third dimension is interesting because its order is reversed! Thus to get fr
"page" to the second one it must go _backwards_ in memory, and so its stride in this
dimension is negative!

```julia-repl
```jldoctest strides
julia> stride(V, 3)
-35
```
Expand Down
21 changes: 13 additions & 8 deletions doc/src/manual/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -584,10 +584,12 @@ The destructuring feature can also be used within a function argument.
If a function argument name is written as a tuple (e.g. `(x, y)`) instead of just
a symbol, then an assignment `(x, y) = argument` will be inserted for you:

```julia-repl
```jldoctest
julia> minmax(x, y) = (y < x) ? (y, x) : (x, y)
minmax (generic function with 1 method)
julia> gap((min, max)) = max - min
gap (generic function with 1 method)
julia> gap(minmax(10, 2))
8
Expand All @@ -598,7 +600,7 @@ would be a two-argument function, and this example would not work.

Similarly, property destructuring can also be used for function arguments:

```julia-repl
```jldoctest
julia> foo((; x, y)) = x + y
foo (generic function with 1 method)
Expand All @@ -616,7 +618,7 @@ julia> foo(A(3, 4))

For anonymous functions, destructuring a single argument requires an extra comma:

```julia-repl
```jldoctest
julia> map(((x, y),) -> x + y, [(1, 2), (3, 4)])
2-element Vector{Int64}:
3
Expand Down Expand Up @@ -784,12 +786,15 @@ Optional arguments are actually just a convenient syntax for writing multiple me
with different numbers of arguments (see [Note on Optional and keyword Arguments](@ref)).
This can be checked for our `date` function example by calling the `methods` function:

```julia-repl
```jldoctest date_default_args; filter = r"@ .*"a
julia> methods(date)
# 3 methods for generic function "date":
[1] date(y::Int64) in Main at REPL[1]:1
[2] date(y::Int64, m::Int64) in Main at REPL[1]:1
[3] date(y::Int64, m::Int64, d::Int64) in Main at REPL[1]:1
# 3 methods for generic function "date" from Main:
[1] date(y::Int64, m::Int64, d::Int64)
@ REPL[2]:1
[2] date(y::Int64, m::Int64)
@ REPL[2]:1
[3] date(y::Int64)
@ REPL[2]:1
```

## Keyword Arguments
Expand Down
2 changes: 1 addition & 1 deletion doc/src/manual/metaprogramming.md
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ Since expressions are just `Expr` objects which can be constructed programmatica
it is possible to dynamically generate arbitrary code which can then be run using [`eval`](@ref).
Here is a simple example:

```julia-repl
```jldoctest
julia> a = 1;
julia> ex = Expr(:call, :+, a, :b)
Expand Down
2 changes: 1 addition & 1 deletion stdlib/Sockets/src/addrinfo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ Uses the operating system's underlying getaddrinfo implementation, which may do
a DNS lookup.
# Examples
```julia-repl
```jldoctest
julia> getaddrinfo("localhost", IPv6)
ip"::1"
Expand Down

0 comments on commit e67a22c

Please sign in to comment.