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