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

**URL:** <https://discourse.julialang.org/t/is-there-a-built-in-function-for-the-natural-length-of-a-segment-represented-by-an-iterator/112414>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [April 2, 2024, 11:44am UTC](https://discourse.julialang.org/t/is-there-a-built-in-function-for-the-natural-length-of-a-segment-represented-by-an-iterator/112414 "2024-04-02T11:44:40Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![homocomputeris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/homocomputeris/32/8933_2.png) [@homocomputeris](https://discourse.julialang.org/u/homocomputeris)\
**Post date:** [April 2, 2024, 11:44am UTC](https://discourse.julialang.org/t/is-there-a-built-in-function-for-the-natural-length-of-a-segment-represented-by-an-iterator/112414/1 "2024-04-02T11:44:41Z")

</div>

Well, it’s rather simple, but maybe there is already a built-in function to do calculate this?

```julia
natural_length(s) = s[end]-s[begin]

natural_length((1,10))
natural_length((1:10))

```

It annoying to copy-paste this snippet which is not large enough to bother putting it into a package.

---

<div class="post-metadata">

**Author:** ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)\
**Post date:** [April 2, 2024, 12:13pm UTC](https://discourse.julialang.org/t/is-there-a-built-in-function-for-the-natural-length-of-a-segment-represented-by-an-iterator/112414/2 "2024-04-02T12:13:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![homocomputeris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/homocomputeris/32/8933_2.png) [@homocomputeris](https://discourse.julialang.org/u/homocomputeris)\
**Post date:** [April 2, 2024, 12:14pm UTC](https://discourse.julialang.org/t/is-there-a-built-in-function-for-the-natural-length-of-a-segment-represented-by-an-iterator/112414/3 "2024-04-02T12:14:12Z")

</div>

Well, it’s more or less the same as my function.

---

<div class="post-metadata">

**Author:** ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)\
**Post date:** [April 2, 2024, 12:18pm UTC](https://discourse.julialang.org/t/is-there-a-built-in-function-for-the-natural-length-of-a-segment-represented-by-an-iterator/112414/4 "2024-04-02T12:18:39Z")

</div>

Yes (`last`/`first` might be a tiny bit more performant). I wanted to point it out in case you disliked the indexing in your function.

---

<div class="post-metadata">

**Author:** ![homocomputeris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/homocomputeris/32/8933_2.png) [@homocomputeris](https://discourse.julialang.org/u/homocomputeris)\
**Post date:** [April 2, 2024, 12:32pm UTC](https://discourse.julialang.org/t/is-there-a-built-in-function-for-the-natural-length-of-a-segment-represented-by-an-iterator/112414/5 "2024-04-02T12:32:35Z")

</div>

Or maybe there is something like `diameter` for sets that can be applied to iterators?

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [April 2, 2024, 1:33pm UTC](https://discourse.julialang.org/t/is-there-a-built-in-function-for-the-natural-length-of-a-segment-represented-by-an-iterator/112414/6 "2024-04-02T13:33:10Z")

</div>

This does not work for all iterators.

```julia-repl
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)})

```

Why are you using a range for this though?

How about this:

```julia-repl
julia> seg = [1,10]
2-element Vector{Int64}:
  1
 10

julia> only(diff(seg))
9

```

---

<div class="post-metadata">

**Author:** ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)\
**Post date:** [April 2, 2024, 1:37pm UTC](https://discourse.julialang.org/t/is-there-a-built-in-function-for-the-natural-length-of-a-segment-represented-by-an-iterator/112414/7 "2024-04-02T13:37:38Z")

</div>

> [@mkitti](#):
>
> This does not work for all iterators.

Huh, yet another difference to `first`/`last`; they are defined for more types, including your example.

(Btw. `diff`, on the other hand, is not defined for tuples.)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 2, 2024, 1:52pm UTC](https://discourse.julialang.org/t/is-there-a-built-in-function-for-the-natural-length-of-a-segment-represented-by-an-iterator/112414/8 "2024-04-02T13:52:38Z")

</div>

> [@mkitti](#):
>
> This does not work for all iterators.

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](https://docs.julialang.org/en/v1/base/collections/#Base.extrema):

```julia
diameter(itr) = -(reverse(extrema(itr))...)

```

which is equivalent to the more verbose (but more readable) implementation:

```julia
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`.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [April 2, 2024, 6:26pm UTC](https://discourse.julialang.org/t/is-there-a-built-in-function-for-the-natural-length-of-a-segment-represented-by-an-iterator/112414/9 "2024-04-02T18:26:46Z")

</div>

Some variations of the same idea

`diamet(itr) = -(-(extrema(itr)...))`

```julia
diameter(itr) = [-1 1]*[extrema(itr)...]

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 7, 2024, 7:22pm UTC](https://discourse.julialang.org/t/is-there-a-built-in-function-for-the-natural-length-of-a-segment-represented-by-an-iterator/112414/10 "2024-04-07T19:22:36Z")

</div>

3 posts were split to a new topic: [Transposes vs 1-row matrices](https://discourse.julialang.org/t/transposes-vs-1-row-matrices/112652)

---

<div class="post-metadata">

**Author:** ![homocomputeris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/homocomputeris/32/8933_2.png) [@homocomputeris](https://discourse.julialang.org/u/homocomputeris)\
**Post date:** [April 3, 2024, 8:36pm UTC](https://discourse.julialang.org/t/is-there-a-built-in-function-for-the-natural-length-of-a-segment-represented-by-an-iterator/112414/11 "2024-04-03T20:36:48Z")

</div>

> [@stevengj](#):
>
> `diameter(itr) = -(reverse(extrema(itr))...)`

That’s neat.

> [@stevengj](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 3, 2024, 8:41pm UTC](https://discourse.julialang.org/t/is-there-a-built-in-function-for-the-natural-length-of-a-segment-represented-by-an-iterator/112414/12 "2024-04-03T20:41:07Z")

</div>

> [@homocomputeris](#):
>
> 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.

Because it’s an extraordinarily common function to call.

`diameter` as defined here does not seem to be commonly needed. Is there _any_ language that provides such a function in its standard library?

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [April 4, 2024, 1:24am UTC](https://discourse.julialang.org/t/is-there-a-built-in-function-for-the-natural-length-of-a-segment-represented-by-an-iterator/112414/13 "2024-04-04T01:24:44Z")

</div>

> [@stevengj](#):
>
> Is there _any_ language that provides such a function in its standard library?

If we include Numpy, I think it’s [`numpy.ptp`](https://numpy.org/doc/stable/reference/generated/numpy.ptp.html#numpy.ptp) (“peak to peak”).

---

<div class="post-metadata">

**Author:** ![homocomputeris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/homocomputeris/32/8933_2.png) [@homocomputeris](https://discourse.julialang.org/u/homocomputeris)\
**Post date:** [April 4, 2024, 10:26am UTC](https://discourse.julialang.org/t/is-there-a-built-in-function-for-the-natural-length-of-a-segment-represented-by-an-iterator/112414/14 "2024-04-04T10:26:09Z")

</div>

Matlab also has [Maximum-to-minimum difference - MATLAB peak2peak](https://www.mathworks.com/help/signal/ref/peak2peak.html)
