-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature request] Set multiple fields at once #119
Comments
Are you mostly looking for performance or convenience? If the former, then "copying immutable struct" should be free in most cases: julia> nt = (a=1, b=2, c=3)
julia> @btime let
x = @set $nt.a = 10
y = @set x.b = 20
end
1.333 ns (0 allocations: 0 bytes)
(a = 10, b = 20, c = 3) If convenience, then:
|
I am mostly looking for performance. |
Often, immutable structs are not really "created" in this sense, they don't physically exist in memory. |
Yes! I can confirm @aplavin statement. In all cases I used Accessors.jl, I saw no performance penalty by setting multiple parameters at once with @resets nt begin
nt.a = 1
nt.b = 2
nt.c = 3
....
end This idea is to replace all expressions with |
That's how I tend to write sequences of Accessors (or any other!) operations: julia> using Accessors, DataPipes
julia> nt = (a=1, b=2)
julia> @p let
nt
@set __.b = 3
@set __.a += 4
@insert __.c = 9
@set √(last(__)) += 1
end
(a = 5, b = 3, c = 16.0)
# or as a part of a larger pipeline:
julia> nts = [nt, nt]
julia> @p let
nts
map() do __
@insert __.c = 9
@set __.a += 4
end
end
2-element Vector{@NamedTuple{a::Int64, b::Int64, c::Int64}}:
(a = 5, b = 2, c = 9)
(a = 5, b = 2, c = 9) Looking at these examples, not sure if the |
Closing as duplicate of #15 |
Setting several fields at once would reduce the number of times the immutable struct is copied. For example,
instead of
The text was updated successfully, but these errors were encountered: