Skip to content

Commit 765d9eb

Browse files
committed
fix itr in Properties
1 parent c2855e9 commit 765d9eb

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

src/optics.jl

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,11 +271,19 @@ function mapproperties(f, obj::O, handler=Construct(), itr=nothing) where O
271271
if isempty(pnames)
272272
return obj
273273
else
274-
new_props = map(pnames) do p
275-
f(getproperty(obj, p))
274+
# TODO: this is too slow
275+
new_props, itr = reduce(pnames; init=((), itr)) do (vals, itr), p
276+
prop = getproperty(obj, p)
277+
if itr isa Nothing
278+
val = f(prop)
279+
(vals..., val), itr
280+
else
281+
val, itr = f(prop, itr)
282+
(vals..., val), itr
283+
end
276284
end
277285
ctr = _constructor(handler, O)
278-
return ctr(new_props...)
286+
return _maybeitr(ctr(new_props...), itr)
279287
end
280288
end
281289

test/test_queries.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ vals = (1.0, 2.0, 3.0, 4.0)
55

66
# Fields is the default
77
lens = Query(; select=x -> x isa Float64, descend=x -> x isa NamedTuple)
8-
slowlens = Query(x -> x isa Float64, x -> x isa Tuple, Accessors.Properties())
8+
slowlens = Query(x -> x isa Float64, x -> x isa NamedTuple, Accessors.Properties())
99

1010
@code_typed lens(obj)
1111
@code_typed slowlens(obj)
@@ -16,13 +16,15 @@ slowlens = Query(x -> x isa Float64, x -> x isa Tuple, Accessors.Properties())
1616
println("get")
1717
@btime $lens($obj)
1818
@btime $slowlens($obj)
19-
@test lens(obj) == (17.0, 6.0)
19+
@test lens(obj) == slowlens(obj) == (17.0, 6.0)
2020

2121
println("set")
2222
# Need a wrapper so we don't have to pass in the starting iterator
2323
@btime Accessors.set($obj, $lens, $vals)
24-
# @btime Accessors.set($obj, $slowlens, $vals)
25-
@test Accessors.set(obj, lens, vals) == (7, (a=1.0, b=2.0f0), ("3", 4, 5.0), (x=2.0,), )
24+
@btime Accessors.set($obj, $slowlens, $vals)
25+
@test Accessors.set(obj, lens, vals) ==
26+
Accessors.set(obj, lens, vals) ==
27+
(7, (a=1.0, b=2.0f0), ("3", 4, 5.0), (x=2.0,), )
2628

2729
# using ProfileView
2830
# @profview for i in 1:1000000 Accessors.set(obj, lens, vals) end

0 commit comments

Comments
 (0)