Skip to content

Commit d326c9a

Browse files
authored
Document lazy things (#55)
* lazy docs * Document lazy things * More care with indexing
1 parent de95d68 commit d326c9a

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,29 @@ julia> push!(t, (a = 3, b = "z"))
138138
(a = 3, b = "z")
139139
```
140140

141+
### Lazy row iteration
142+
143+
StructArrays also provides a `LazyRow` wrapper for lazy row iteration. `LazyRow(t, i)` does not materialize the i-th row but returns a lazy wrapper around it on which `getproperty` does the correct thing. This is useful when the row has many fields only some of which are necessary. It also allows changing columns in place.
144+
145+
```julia
146+
julia> LazyRow(t, 2).a
147+
2
148+
149+
julia> LazyRow(t, 2).a = 123
150+
123
151+
152+
julia> t
153+
2-element StructArray{NamedTuple{(:a, :b),Tuple{Int64,String}},1,NamedTuple{(:a, :b),Tu
154+
ple{Array{Int64,1},Array{String,1}}}}:
155+
(a = 1, b = "x")
156+
(a = 123, b = "y")
157+
```
158+
159+
To iterate in a lazy way one can simply iterate `LazyRows`:
160+
161+
```julia
162+
julia> map(t -> t.b ^ t.a, LazyRows(t))
163+
2-element Array{String,1}:
164+
"x"
165+
"yy"
166+
```

src/StructArrays.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module StructArrays
22

33
import Requires
4-
export StructArray, StructVector
4+
export StructArray, StructVector, LazyRow, LazyRows
55
export collect_structarray, fieldarrays
66

77
include("interface.jl")

src/lazy.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,17 @@ iscompatible(::Type{<:LazyRow{S}}, ::Type{StructArray{T, N, C}}) where {S, T, N,
1414
iscompatible(S, StructArray{T, N, C})
1515

1616
(s::ArrayInitializer)(::Type{<:LazyRow{T}}, d) where {T} = buildfromschema(typ -> s(typ, d), T)
17+
18+
struct LazyRows{T, N, C, I} <: AbstractArray{LazyRow{T, N, C, I}, N}
19+
columns::StructArray{T, N, C}
20+
end
21+
LazyRows(s::S) where {S<:StructArray} = LazyRows(IndexStyle(S), s)
22+
LazyRows(::IndexLinear, s::StructArray{T, N, C}) where {T, N, C} = LazyRows{T, N, C, Int}(s)
23+
LazyRows(::IndexCartesian, s::StructArray{T, N, C}) where {T, N, C} = LazyRows{T, N, C, CartesianIndex{N}}(s)
24+
Base.parent(v::LazyRows) = getfield(v, 1)
25+
26+
Base.size(v::LazyRows) = size(parent(v))
27+
Base.getindex(v::LazyRows{<:Any, <:Any, <:Any, <:Integer}, i::Integer) = LazyRow(parent(v), i)
28+
Base.getindex(v::LazyRows{<:Any, <:Any, <:Any, <:CartesianIndex}, i::Integer...) = LazyRow(parent(v), CartesianIndex(i))
29+
30+
Base.IndexStyle(::Type{<:LazyRows{<:Any, <:Any, <:Any, <:Integer}}) = IndexLinear()

test/runtests.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using StructArrays
2-
using StructArrays: LazyRow
32
import Tables, PooledArrays, WeakRefStrings
43
using Test
54

@@ -416,3 +415,14 @@ end
416415
@test v.a == [i for i in 1:3, j in 1:4]
417416
@test v.b == [j for i in 1:3, j in 1:4]
418417
end
418+
419+
@testset "lazy" begin
420+
s = StructArray(rand(ComplexF64, 10, 10))
421+
rows = LazyRows(s)
422+
@test IndexStyle(rows) isa IndexLinear
423+
@test all(t -> t.re >= 0, s)
424+
@test all(t -> t.re >= 0, rows)
425+
rows[13].re = -12
426+
@test !all(t -> t.re >= 0, s)
427+
@test !all(t -> t.re >= 0, rows)
428+
end

0 commit comments

Comments
 (0)