Skip to content

Commit 3c62f3d

Browse files
committed
Ensure code blocks in doc are doctests or @repl
1 parent 1495e75 commit 3c62f3d

File tree

2 files changed

+36
-16
lines changed

2 files changed

+36
-16
lines changed

docs/src/mutable.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1+
```@meta
2+
DocTestSetup = quote
3+
using Bijections
4+
end
5+
```
6+
17
# [Bijections for Mutable Structures](@id mutable)
28

39
## Mutating keys/values can lead to corruption
410

511
The safest use of a `Bijection` is when the keys and values are immutable.
612
If a mutable key or value in a `Bijection` is altered, the bijective property
713
can be compromised. Here is an example:
8-
```
14+
15+
```jldoctest
916
julia> b = Bijection{Int, Vector{Int}}();
1017
1118
julia> b[1] = [1,2,3]
@@ -42,7 +49,8 @@ In case none of these is a viable option, we provide the following additional al
4249
## Keys/values as objects
4350

4451
The issue in the example presented above is that distinct Julia objects may be equal, but not the same object. For example:
45-
```
52+
53+
```jldoctest
4654
julia> v = [1,2,3];
4755
4856
julia> w = [1,2,3];
@@ -55,13 +63,16 @@ false
5563
```
5664

5765
We may wish to create a `Bijection` in which the keys or values are permitted to be equal, but are distinct objects. Julia's `IdDict` is a variation of `Dict` in which keys/values are considered different if they are distinct object (even if they hold the same data). To replicate this behavior in a `Bijection` use this longer form constructor:
58-
```
66+
67+
```julia
5968
Bijection{K, V, IdDict{K,V}, IdDict{V,K}}()
6069
```
70+
6171
where `K` is the type of the keys and `V` is the type of the values.
6272

6373
For example:
64-
```
74+
75+
```jldoctest
6576
julia> b = Bijection{Vector{Int}, String, IdDict{Vector{Int},String}, IdDict{String,Vector{Int}}}();
6677
6778
julia> b[ [1,2,3] ] = "alpha";

docs/src/operations.md

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
```@meta
2+
DocTestSetup = quote
3+
using Bijections
4+
end
5+
```
6+
17
# Operations
28

39

@@ -9,12 +15,14 @@ There are two functions that take a `Bijection` and return a new
915
[`inv`](@ref) and [`active_inv`](@ref).
1016

1117
### Independent inverse: `inv`
18+
1219
Given a `Bijection` `b`, calling `inv(b)` creates a new `Bijection`
1320
that is the inverse of `b`. The new `Bijection` is completely independent
1421
of the original, `b`. Changes to one do not affect the other:
15-
```
22+
23+
```jldoctest
1624
julia> b = Bijection{Int,String}()
17-
Bijection Dict{Int64, String}()
25+
Bijection{Int64, String, Dict{Int64, String}, Dict{String, Int64}}()
1826
1927
julia> b[1] = "alpha"
2028
"alpha"
@@ -23,7 +31,7 @@ julia> b[2] = "beta"
2331
"beta"
2432
2533
julia> bb = inv(b)
26-
Bijection Dict{String, Int64} with 2 entries:
34+
Bijection{String, Int64, Dict{String, Int64}, Dict{Int64, String}} with 2 entries:
2735
"alpha" => 1
2836
"beta" => 2
2937
@@ -38,6 +46,7 @@ julia> b[3] = "gamma"
3846
3947
julia> bb["gamma"]
4048
ERROR: KeyError: key "gamma" not found
49+
[...]
4150
```
4251

4352
### Active inverse: `active_inv`
@@ -47,9 +56,9 @@ case the original and the inverse are actively tied together.
4756
That is, modification of one immediately affects the other.
4857
The two `Bijection`s remain inverses no matter how either is modified.
4958

50-
```
59+
```jldoctest
5160
julia> b = Bijection{Int,String}()
52-
Bijection Dict{Int64, String}()
61+
Bijection{Int64, String, Dict{Int64, String}, Dict{String, Int64}}()
5362
5463
julia> b[1] = "alpha"
5564
"alpha"
@@ -58,7 +67,7 @@ julia> b[2] = "beta"
5867
"beta"
5968
6069
julia> bb = active_inv(b)
61-
Bijection Dict{String, Int64} with 2 entries:
70+
Bijection{String, Int64, Dict{String, Int64}, Dict{Int64, String}} with 2 entries:
6271
"alpha" => 1
6372
"beta" => 2
6473
@@ -73,10 +82,10 @@ julia> bb["gamma"]
7382

7483
`Bijection`s can be used in a `for` statement just like Julia
7584
dictionaries:
76-
```
77-
julia> for (x,y) in b; println("$x --> $y"); end
78-
2 --> beta
79-
3 --> gamma
80-
1 --> alpha
81-
```
8285

86+
```@repl example
87+
# The order of iteration is not guaranteed, so this can't be a doctest # hide
88+
using Bijections # hide
89+
b = Bijection(1 => "alpha", 2 => "beta", 3 => "gamma");
90+
for (x, y) in b; println("$x --> $y"); end
91+
```

0 commit comments

Comments
 (0)