Through some Discourse posts and my own personal experience, I’ve seen a pattern for isempty that repeats a lot, but there is no easy way around it. It is the case for when the result of a function call returns an empty array (it may happen with nothing also), and a “default” return is desired instead in that case.
Here is a MWE:
# 'f' is a computationally expensive function, so it should not be called twice.
# f(x)::Vector{Int64}
default_ = [-1, 1, 0]
# Call to function
a = f(t)
# Return a or 'default' if a is empty
a = isempty(a) ? default_ : a
I cannot use map because it does not work for empty collections. Is there an easier way to do this?
Or at least, a way to do it idiomatically (in one line/function call).
You could define your own helper function for the isempty case, analogue to something.
my_something(x,y) = (x === nothing || isempty(x)) ? y : x
Note that it is not best practice in Julia to have a function return different types depending on run-time information (different output types for different input types is fine) because this is not type-stable.
Thus, better write your function f such that it always returns the same type, i.e. an array which might be empty.
It’s not quite what you’re asking for, but Maybe.jl addresses a similar concern. More often than not, the reason you care about emptiness of an array is because you want to use functions like first which assume a nonempty array, and Maybe helps with that problem.
On a side note, I put it in Internals because I thought it could spark a conversation about it and maybe push it further down to Base, but yeah, I agree that General Usage is better.