It seems quite specific so I doubt there is a built-in function (I might be wrong though). However, last(x) - first(x) seems short enough and self-explanatory.
julia> g = (i for i in 2:10)
Base.Generator{UnitRange{Int64}, typeof(identity)}(identity, 2:10)
julia> natural_length(x) = x[end] -x[begin]
natural_length (generic function with 1 method)
julia> natural_length(g)
ERROR: MethodError: no method matching lastindex(::Base.Generator{UnitRange{Int64}, typeof(identity)})
Even last(a) - first(a) seems wrong to me for a non-sorted iterator. A more general solution would be to use the extrema function:
diameter(itr) = -(reverse(extrema(itr))...)
which is equivalent to the more verbose (but more readable) implementation:
function diameter(itr)
min, max = extrema(itr)
return max - min
end
(This requires looping over the entire iterator in general, though for a range there is an optimized extrema method that just looks at the endpoints.)
extrema is quite a useful function (which takes some effort to implement well), and provides strictly more information than diameter. So, I don’t think there is much need for a built-in (or package) function to compute diameter, since computing diameter is a one-liner given extrema.
Well, length is also a one-liner that “defaults to prod(size(A))”, and it’s ever shorter than -(reverse(extrema(itr))...), yet it’s included into the stdlib.
In some sense, diameter is a length for an iterator’s values rather than the container itself.