Stream: helpdesk (published)

Topic: ✔ length(x) == length(eachindex(x)) ?


view this post on Zulip Leandro Martínez (Sep 28 2022 at 14:40):

Is it always true that

length(x) == length(eachindex(x))

?

view this post on Zulip Expanding Man (Sep 28 2022 at 14:42):

I think so, but I usually do prod(size(x)) for arrays just because I never remember what length is supposed to be on them.

view this post on Zulip Michael Fiano (Sep 28 2022 at 14:44):

length and eachindex are generic, so no?

view this post on Zulip Gunnar Farnebäck (Sep 28 2022 at 15:14):

According to the docstrings:

I would interpret this as yes, assuming the methods follow the documentation.

view this post on Zulip Michael Fiano (Sep 28 2022 at 15:20):

Well that is only part of the description of eachindex. It can be defined for anything iterable, including unbounded sequences.

view this post on Zulip Michael Fiano (Sep 28 2022 at 15:21):

Just today I implemented it for a non-AbstractArray type as recommended by @Sukera. There's nothing wrong with that.

view this post on Zulip Sukera (Sep 28 2022 at 15:22):

do note that I just recommended it due to being a convenient name, not for implying your Rect was an AbstractArray

view this post on Zulip Michael Fiano (Sep 28 2022 at 15:22):

Exactly.

view this post on Zulip Sukera (Sep 28 2022 at 15:22):

(though the docstring does support "other iterables" as well)

view this post on Zulip Michael Fiano (Sep 28 2022 at 15:23):

Right, that is my point, that it is not exclusive to arrays.

view this post on Zulip Leandro Martínez (Sep 28 2022 at 15:52):

So, if the sequence is bounded, then "yes"? Or do you see any other pathological case?

view this post on Zulip Jakob Nybo Nissen (Sep 28 2022 at 19:11):

I think they should be the same in all cases.

view this post on Zulip Mason Protter (Sep 28 2022 at 19:18):

Michael Fiano said:

Right, that is my point, that it is not exclusive to arrays.

What does that have to do with it though? The question isn't about arrays in particular, it's about whether length of an object should agree with the length of it's indices, and I'd say that in every case where indices exist, they should absolutely be the same.

view this post on Zulip Mason Protter (Sep 28 2022 at 19:20):

Of course one can always just define a method that does something whacky, but I think defining an object where it's length doesn't agree with the length of it's indices is far enough beyond the pale that it can be ignored.

view this post on Zulip Michael Fiano (Sep 28 2022 at 19:21):

Gunnar Farnebäck said:

According to the docstrings:

I would interpret this as yes, assuming the methods follow the documentation.

That wasn't in response to the original question, moreso than it was in response to the partial documentation quoting here.

view this post on Zulip Michael Fiano (Sep 28 2022 at 19:24):

I agree with that assumption. I did however implement eachindex on a non-array (without an accompanying length implementation) to make iteration on a type more concise, so in that particular case, which satisfied my needs, they are technically different, seeing how one is a MethodError.

view this post on Zulip Notification Bot (Sep 29 2022 at 00:47):

Leandro Martínez has marked this topic as resolved.

view this post on Zulip Leandro Martínez (Sep 29 2022 at 00:47):

Thank you all!


Last updated: Nov 22 2024 at 04:41 UTC