Function methods based solely on element type of array of arbitrary dimension

That is expressed as

function contains(base::AbstractArray, arrays::AbstractVector{<:AbstractArray{<:Real}})
    any(isapprox(base, a) for a in arrays)
end

or, more verbosely

function contains(base::AbstractArray, arrays::AbstractVector{A}) where {A<:AbstractArray{T} where T<:Real}
    any(isapprox(base, a) for a in arrays)
end

Note that materializing the generator in a vector is not necessary. Generators are iterable, so functions like any, all, sum etc. may work on them directly.

Enforcing very strict input type constraints is not very Julian, though. A style more likely to find is

contains(item, itr; eq = isequal) = any(x -> eq(item, x), itr)

Then, you can use it as contains(array, arrays, eq = isapprox). As Julia is not statically compiled, neither type-constrained nor duck-typed function are going to produce compile-time errors anyway.

5 Likes