Way to filter out #undef values from list?

For example, you have:

> x = Vector(4)

4-element Array{Any,1}:
 #undef
 #undef
 #undef
 #undef

Now if you add some entries, you get:

> x[2] = 4
> x[4] = 7
> x

4-element Array{Any,1}:
 #undef
   4
 #undef
   7

How do you quickly filter out #undef input in one line?

#in pseudocode, 

> custom_filter(x)

2-element Array{Any,1}:
   4
   7
filter(i -> isdefined(x, i), 1:length(x))

another example of why I’m really looking forward to when we have function currying in 1.1.

Not sure leaving undefined references in your array is a good practice, however. This is what missing was made for (or nothing, if you want it to throw errors).

1 Like

Did you actually run the code?

  1. isdefined will not tell you anything useful here.
  2. This returns the indices, not the element.

[x[i] for i in 1:length(x) if isassigned(x, i)] (or Any[...]/eltype(x)[...] if you need to maintain the eltype)

Even in your example that’s saving only 3 non-white space characters and harder to understand.

2 Likes

And that’s the only thing you can do if you need to support Any.

Sorry, that was really stupid of me, I meant to do x[filter(...)] but even so yes, isdefined is the wrong function.

How so?

x = Vector{Any}(missing, n)
x = Vector{Any}(nothing, n)

both work. Though, perhaps there’s nothing wrong with leaving them undefined.

Because you are not supporting Any anymore. You are supporting all the types except nothing and missing.

1 Like

Well, sure taken literally that’s certainly true, but this seems like an intended use case of missing or nothing.

No. If you have anything that’s no Any that’s fine. With any that’s not doable.

What I think you are saying is “You can’t do that because the OP wants the set of things which are considered ‘null’ to be the empty set’.” And I’m saying: Julia provides us with objects which are specifically intended to be considered “null”, so why not take one of those as the only element in the set of things which should be considered “null”?

I only bother to mention it because (at least in my experience) it’s not a common practice for a package to provide (as output) arrays containing undefined references and expecting someone to handle that, but it is not so unusual for them to provide arrays contianing nothing or missing.

Of course, I have no idea what the OP is trying to do, so I don’t know what the appropriate choice is here.

No I didn’t. I said that you shouldn’t (edited…) consider everything about “leaving undefined references in your array” being “bad practice” (that’s what I specifically replied to). I never once mentioned what he want since he didn’t mention and I don’t want to guess. I’m only saying that you shouldn’t just see undefined reference and claim they should all be replaced by missing or nothing and I even agree that unless you need to handle Any (i.e. all types) missing or nothing could be fine.

3 Likes