JuliaMath / JuliaMath/MeasureTheory.jl

Canonical ordering for NamedTuple keys

Open
#12 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Julia
Stars
401
Forks
31
PR merge metrics
No merged PRs in 30d

Description

@simeonschaub suggested

```julia
function canonicalize(nt::NamedTuple{_k,T}) where {_k,T}
if @generated
k = collect(_k)
sp = sortperm(k)
return quote
sp = ($(sp...),)
keys = ($(Meta.quot.(k[sp])...),)
types = Tuple{$(T.parameters[sp]...)}
NamedTuple{keys,types}(map(Base.Fix1(getfield, values(nt)), sp))
end
else
sp = sortperm(collect(keys(nt)))
return NamedTuple{keys(nt)[sp]}(values(nt)[sp])
end
end
```

_Originally posted by @simeonschaub in https://github.com/cscherrer/MeasureTheory.jl/issues/7#issuecomment-698863535_

We should have something like this in MeasureTheory.jl directly or use KeywordDispatch.jl. It looks like this shouldn't introduce much overhead, but let's keep an eye out for that anyway.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.