Skip to content

Commit 94719c1

Browse files
committed
fix doctest
It seems hard to maintain the doctest of `NamedTuple`'s printing.
1 parent 5bafbc1 commit 94719c1

File tree

3 files changed

+52
-119
lines changed

3 files changed

+52
-119
lines changed

docs/src/advanced.md

+32-65
Original file line numberDiff line numberDiff line change
@@ -6,89 +6,56 @@ StructArrays support structures with custom data layout. The user is required to
66

77
Here is an example of a type `MyType` that has as custom fields either its field `data` or fields of its field `rest` (which is a named tuple):
88

9-
```jldoctest advanced1
10-
julia> using StructArrays
9+
```@repl advanced1
10+
using StructArrays
1111
12-
julia> struct MyType{T, NT<:NamedTuple}
13-
data::T
14-
rest::NT
15-
end
12+
struct MyType{T, NT<:NamedTuple}
13+
data::T
14+
rest::NT
15+
end
1616
17-
julia> MyType(x; kwargs...) = MyType(x, values(kwargs))
18-
MyType
17+
MyType(x; kwargs...) = MyType(x, values(kwargs))
1918
```
2019

2120
Let's create a small array of these objects:
2221

23-
```jldoctest advanced1
24-
julia> s = [MyType(i/5, a=6-i, b=2) for i in 1:5]
25-
5-element Vector{MyType{Float64, NamedTuple{(:a, :b), Tuple{Int64, Int64}}}}:
26-
MyType{Float64, NamedTuple{(:a, :b), Tuple{Int64, Int64}}}(0.2, (a = 5, b = 2))
27-
MyType{Float64, NamedTuple{(:a, :b), Tuple{Int64, Int64}}}(0.4, (a = 4, b = 2))
28-
MyType{Float64, NamedTuple{(:a, :b), Tuple{Int64, Int64}}}(0.6, (a = 3, b = 2))
29-
MyType{Float64, NamedTuple{(:a, :b), Tuple{Int64, Int64}}}(0.8, (a = 2, b = 2))
30-
MyType{Float64, NamedTuple{(:a, :b), Tuple{Int64, Int64}}}(1.0, (a = 1, b = 2))
22+
```@repl advanced1
23+
s = [MyType(i/5, a=6-i, b=2) for i in 1:5]
3124
```
3225

3326
The default `StructArray` does not unpack the `NamedTuple`:
3427

35-
```jldoctest advanced1
36-
julia> sa = StructArray(s);
37-
38-
julia> sa.rest
39-
5-element Vector{NamedTuple{(:a, :b), Tuple{Int64, Int64}}}:
40-
(a = 5, b = 2)
41-
(a = 4, b = 2)
42-
(a = 3, b = 2)
43-
(a = 2, b = 2)
44-
(a = 1, b = 2)
45-
46-
julia> sa.a
47-
ERROR: type NamedTuple has no field a
48-
Stacktrace:
49-
[1] component
50-
[...]
28+
```@repl advanced1
29+
sa = StructArray(s);
30+
sa.rest
31+
sa.a
5132
```
5233

5334
Suppose we wish to give the keywords their own fields. We can define custom `staticschema`, `component`, and `createinstance` methods for `MyType`:
5435

55-
```jldoctest advanced1
56-
julia> function StructArrays.staticschema(::Type{MyType{T, NamedTuple{names, types}}}) where {T, names, types}
57-
# Define the desired names and eltypes of the "fields"
58-
return NamedTuple{(:data, names...), Base.tuple_type_cons(T, types)}
59-
end;
60-
61-
julia> function StructArrays.component(m::MyType, key::Symbol)
62-
# Define a component-extractor
63-
return key === :data ? getfield(m, 1) : getfield(getfield(m, 2), key)
64-
end;
65-
66-
julia> function StructArrays.createinstance(::Type{MyType{T, NT}}, x, args...) where {T, NT}
67-
# Generate an instance of MyType from components
68-
return MyType(x, NT(args))
69-
end;
36+
```@repl advanced1
37+
function StructArrays.staticschema(::Type{MyType{T, NamedTuple{names, types}}}) where {T, names, types}
38+
# Define the desired names and eltypes of the "fields"
39+
return NamedTuple{(:data, names...), Base.tuple_type_cons(T, types)}
40+
end;
41+
42+
function StructArrays.component(m::MyType, key::Symbol)
43+
# Define a component-extractor
44+
return key === :data ? getfield(m, 1) : getfield(getfield(m, 2), key)
45+
end;
46+
47+
function StructArrays.createinstance(::Type{MyType{T, NT}}, x, args...) where {T, NT}
48+
# Generate an instance of MyType from components
49+
return MyType(x, NT(args))
50+
end;
7051
```
7152

7253
and now:
7354

74-
```jldoctest advanced1
75-
julia> sa = StructArray(s);
76-
77-
julia> sa.a
78-
5-element Vector{Int64}:
79-
5
80-
4
81-
3
82-
2
83-
1
84-
85-
julia> sa.b
86-
5-element Vector{Int64}:
87-
2
88-
2
89-
2
90-
2
91-
2
55+
```@repl advanced1
56+
sa = StructArray(s);
57+
sa.a
58+
sa.b
9259
```
9360

9461
The above strategy has been tested and implemented in [GeometryBasics.jl](https://github.com/JuliaGeometry/GeometryBasics.jl).

docs/src/index.md

+18-52
Original file line numberDiff line numberDiff line change
@@ -9,73 +9,39 @@ The package was largely inspired by the `Columns` type in [IndexedTables](https:
99
## Collection and initialization
1010

1111
One can create a `StructArray` by providing the struct type and a tuple or NamedTuple of field arrays:
12-
```jldoctest intro
13-
julia> using StructArrays
14-
15-
julia> struct Foo{T}
16-
a::T
17-
b::T
18-
end
19-
20-
julia> adata = [1 2; 3 4]; bdata = [10 20; 30 40];
21-
22-
julia> x = StructArray{Foo}((adata, bdata))
23-
2×2 StructArray(::Matrix{Int64}, ::Matrix{Int64}) with eltype Foo:
24-
Foo{Int64}(1, 10) Foo{Int64}(2, 20)
25-
Foo{Int64}(3, 30) Foo{Int64}(4, 40)
12+
```@repl intro
13+
using StructArrays
14+
struct Foo{T}
15+
a::T
16+
b::T
17+
end
18+
adata = [1 2; 3 4]; bdata = [10 20; 30 40];
19+
x = StructArray{Foo}((adata, bdata))
2620
```
2721

2822
You can also initialze a StructArray by passing in a NamedTuple, in which case the name (rather than the order) specifies how the input arrays are assigned to fields:
2923

30-
```jldoctest intro
31-
julia> x = StructArray{Foo}((b = adata, a = bdata)) # initialize a with bdata and vice versa
32-
2×2 StructArray(::Matrix{Int64}, ::Matrix{Int64}) with eltype Foo:
33-
Foo{Int64}(10, 1) Foo{Int64}(20, 2)
34-
Foo{Int64}(30, 3) Foo{Int64}(40, 4)
24+
```@repl intro
25+
x = StructArray{Foo}((b = adata, a = bdata)) # initialize a with bdata and vice versa
3526
```
3627

3728
If a struct is not specified, a StructArray with Tuple or NamedTuple elements will be created:
38-
```jldoctest intro
39-
julia> x = StructArray((adata, bdata))
40-
2×2 StructArray(::Matrix{Int64}, ::Matrix{Int64}) with eltype Tuple{Int64, Int64}:
41-
(1, 10) (2, 20)
42-
(3, 30) (4, 40)
43-
44-
julia> x = StructArray((a = adata, b = bdata))
45-
2×2 StructArray(::Matrix{Int64}, ::Matrix{Int64}) with eltype NamedTuple{(:a, :b), Tuple{Int64, Int64}}:
46-
(a = 1, b = 10) (a = 2, b = 20)
47-
(a = 3, b = 30) (a = 4, b = 40)
29+
```@repl intro
30+
x = StructArray((adata, bdata))
31+
x = StructArray((a = adata, b = bdata))
4832
```
4933

5034
It's also possible to create a `StructArray` by choosing a particular dimension to interpret as the components of a struct:
5135

52-
```jldoctest intro
53-
julia> x = StructArray{Complex{Int}}(adata; dims=1) # along dimension 1, the first item `re` and the second is `im`
54-
2-element StructArray(view(::Matrix{Int64}, 1, :), view(::Matrix{Int64}, 2, :)) with eltype Complex{Int64}:
55-
1 + 3im
56-
2 + 4im
57-
58-
julia> x = StructArray{Complex{Int}}(adata; dims=2) # along dimension 2, the first item `re` and the second is `im`
59-
2-element StructArray(view(::Matrix{Int64}, :, 1), view(::Matrix{Int64}, :, 2)) with eltype Complex{Int64}:
60-
1 + 2im
61-
3 + 4im
36+
```@repl intro
37+
x = StructArray{Complex{Int}}(adata; dims=1) # along dimension 1, the first item `re` and the second is `im`
38+
x = StructArray{Complex{Int}}(adata; dims=2) # along dimension 2, the first item `re` and the second is `im`
6239
```
6340

6441
One can also create a `StructArray` from an iterable of structs without creating an intermediate `Array`:
6542

66-
```jldoctest intro
67-
julia> StructArray(log(j+2.0*im) for j in 1:10)
68-
10-element StructArray(::Vector{Float64}, ::Vector{Float64}) with eltype ComplexF64:
69-
0.8047189562170501 + 1.1071487177940904im
70-
1.0397207708399179 + 0.7853981633974483im
71-
1.2824746787307684 + 0.5880026035475675im
72-
1.4978661367769954 + 0.4636476090008061im
73-
1.683647914993237 + 0.3805063771123649im
74-
1.8444397270569681 + 0.3217505543966422im
75-
1.985145956776061 + 0.27829965900511133im
76-
2.1097538525880535 + 0.24497866312686414im
77-
2.2213256282451583 + 0.21866894587394195im
78-
2.3221954495706862 + 0.19739555984988078im
43+
```@repl intro
44+
StructArray(log(j+2.0*im) for j in 1:10)
7945
```
8046

8147
Another option is to create an uninitialized `StructArray` and then fill it with data. Just like in normal arrays, this is done with the `undef` syntax:

src/utils.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,9 @@ julia> s_pooled = StructArrays.replace_storage(s) do v
144144
isbitstype(eltype(v)) ? v : convert(PooledArray, v)
145145
end
146146
$(if VERSION < v"1.6-"
147-
"3-element StructArray(::UnitRange{Int64}, ::PooledArray{String,UInt32,1,Array{UInt32,1}}) with eltype NamedTuple{(:a, :b),Tuple{Int64,String}}:"
147+
"3-element StructArray(::UnitRange{Int64}, ::PooledArray{String,UInt32,1,Array{UInt32,1}}) with eltype $(NamedTuple{(:a, :b),Tuple{Int64,String}}):"
148148
else
149-
"3-element StructArray(::UnitRange{Int64}, ::PooledVector{String, UInt32, Vector{UInt32}}) with eltype NamedTuple{(:a, :b), Tuple{Int64, String}}:"
149+
"3-element StructArray(::UnitRange{Int64}, ::PooledVector{String, UInt32, Vector{UInt32}}) with eltype $(NamedTuple{(:a, :b), Tuple{Int64, String}}):"
150150
end)
151151
(a = 1, b = "string")
152152
(a = 2, b = "string")

0 commit comments

Comments
 (0)