Skip to content

Commit 63340e1

Browse files
author
Pietro Vertechi
authored
Merge pull request JuliaArrays#43 from piever/pv/docs
Fix JuliaArrays#39 : document undef initializer
2 parents 05d3ba5 + 98cfc45 commit 63340e1

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

README.md

+34-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ This package introduces the type `StructArray` which is an `AbstractArray` whose
77

88
`Base.getproperty` or the dot syntax can be used to access columns, whereas rows can be accessed with `getindex`.
99

10-
The package is largely inspired from the `Columns` type in [IndexedTables](https://github.com/JuliaComputing/IndexedTables.jl)
10+
The package was largely inspired by the `Columns` type in [IndexedTables](https://github.com/JuliaComputing/IndexedTables.jl) which it now replaces.
1111

1212
## Example usage to store complex numbers
1313

@@ -39,6 +39,39 @@ julia> StructArray([1+im, 3-2im])
3939
3 - 2im
4040
```
4141

42+
### Collection and initialization
43+
44+
One can also create a `StructArrray` from an iterable of structs without creating an intermediate `Array`:
45+
46+
```julia
47+
julia> StructArray(log(j+2.0*im) for j in 1:10)
48+
10-element StructArray{Complex{Float64},1,NamedTuple{(:re, :im),Tuple{Array{Float64,1},Array{Float64,1}}}}:
49+
0.8047189562170501 + 1.1071487177940904im
50+
1.0397207708399179 + 0.7853981633974483im
51+
1.2824746787307684 + 0.5880026035475675im
52+
1.4978661367769954 + 0.4636476090008061im
53+
1.683647914993237 + 0.3805063771123649im
54+
1.8444397270569681 + 0.3217505543966422im
55+
1.985145956776061 + 0.27829965900511133im
56+
2.1097538525880535 + 0.24497866312686414im
57+
2.2213256282451583 + 0.21866894587394195im
58+
2.3221954495706862 + 0.19739555984988078im
59+
```
60+
61+
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:
62+
63+
```julia
64+
julia> s = StructArray{ComplexF64}(undef, 2, 2)
65+
2×2 StructArray{Complex{Float64},2,NamedTuple{(:re, :im),Tuple{Array{Float64,2},Array{Float64,2}}}}:
66+
6.91646e-310+6.91646e-310im 6.91646e-310+6.91646e-310im
67+
6.91646e-310+6.91646e-310im 6.91646e-310+6.91646e-310im
68+
69+
julia> rand!(s)
70+
2×2 StructArray{Complex{Float64},2,NamedTuple{(:re, :im),Tuple{Array{Float64,2},Array{Float64,2}}}}:
71+
0.446415+0.671453im 0.0797964+0.675723im
72+
0.0340059+0.420472im 0.907252+0.808263im
73+
```
74+
4275
## Example usage to store a data table
4376

4477
```julia

0 commit comments

Comments
 (0)