Skip to content

Instantly share code, notes, and snippets.

@ToucheSir
Created January 24, 2022 04:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ToucheSir/bf9a3e47338746df2aae4de1fc85cdd5 to your computer and use it in GitHub Desktop.
Save ToucheSir/bf9a3e47338746df2aae4de1fc85cdd5 to your computer and use it in GitHub Desktop.
structintersect.jl
# courtesy of Mason Protter on the Julia Slack
@generated function structintersect(a::NamedTuple{a_names}, b::NamedTuple{b_names}) where {a_names, b_names}
names = Tuple(intersect(a_names, b_names))
fields = Expr(:tuple, (:(getproperty(a, $(QuoteNode(name))), getproperty(b, $(QuoteNode(name)))) for name in names)...)
quote
NamedTuple{$names}($fields)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment