Much more efficient than Iterators.flatten, more generic, and supports arbitrary nesting:
julia> using AccessorsExtra
# get all numbers from anywhere in the object:
julia> getall(mynt, RecursiveOfType(Number))
(0, 1, 2, NaN)
# check that there are nans among them:
julia> any(isnan, getall(mynt, RecursiveOfType(Number)))
true
julia> isnan_rec(n::NamedTuple) = any(isnan_rec, n)
isnan_rec (generic function with 1 method)
julia> isnan_rec(n::Number) = isnan(n)
isnan_rec (generic function with 2 methods)
julia> isnan_rec((; a = 1, b = (; c = 2, d = (; e = NaN))))
true
julia> isnan_rec((; a = 1, b = (; c = 2, d = (; e = 4.0))))
false
Edit: this one doesn’t allocate (not sure why any does, probably doesn’t unroll automatically even though I would have assumed that for short NamedTuples)
@generated function isnan_rec(n::NamedTuple)
quote
Base.Cartesian.@nany $(fieldcount(n)) i -> isnan_rec(n[i])
end
end