Is there a built-in function for the natural length of a segment represented by an iterator?

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.

5 Likes