Is there a builtin type in julia equivalent to python `None`?

Is there a builtin type in julia equivalent to python None in Julia?

[0, 1, false, 2, "", 3, 'a', 's', 34,[],missing] |> filter(func) 

# [ 1, 2, 3, 'a', 's', 34]

What func could be. Why empty character '' is disallowed in Julia?

nothing

3 Likes
julia> [0, 1, false, 2, "", 3, 'a', 's', 34,[],missing] |> filter(isnothing)

Any[]
julia> [0, 1, false, 2, "", 3, 'a', 's', 34,[],missing] |> filter(!isnothing)
11-element Vector{Any}:                                                      
     0                                                                       
     1                                                                       
 false                                                                       
     2                                                                       
      ""                                                                     
     3                                                                       
      'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)             
      's': ASCII/Unicode U+0073 (category Ll: Letter, lowercase)             
    34                                                                       
      Any[]                                                                  
      missing                                                                

I don’t know what pattern you’re looking for, but maybe:

julia> is_whatever(x::Number) = iszero(x)

julia> is_whatever(x::AbstractString) = isempty(x)

julia> is_whatever(x::AbstractArray) = isempty(x)

julia> is_whatever(x::Missing) = true

julia> is_whatever(x::Nothing) = true

julia> is_whatever(x::Bool) = !x

julia> is_whatever(x::Char) = false

julia> [0, 1, false, 2, "", 3, 'a', 's', 34,[],missing] |> filter(!is_whatever)
6-element Vector{Any}:
  1
  2
  3
   'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)
   's': ASCII/Unicode U+0073 (category Ll: Letter, lowercase)
 34
3 Likes

There is no such thing as an empty character. There is an empty string which is a string with no characters.

8 Likes

The example code you gave uses the Python concept of “truthy”/“falsy” values, in which bool(x) returns false for empty containers and None and 0. Julia doesn’t have this concept: instead you can explicitly check the property you want, such as isempty or iszero.

8 Likes

Related discussion:

5 Likes

The high-level point that applies here is that I have no idea what you want those filter operations to do. Which elements did you want to keep? Why? I’m also a little confused what the example has to do with the Python None type…

6 Likes

An example might help, too: A string is roughly analogous to an array of integers. It makes sense to have an empty array of integers. For example, if it is mutable, remove all the elements. But it doesn’t make sense to talk about an empty integer. Both strings and arrays are variable length containers, and that length can be zero

(This really applies to utf8 strings, but if you prefer, consider just ascii strings.)

It makes sense that the OP might be confused about empty characters, but not about empty integers. This is clearly an artifact of how characters are constructed, with two single quotes, eg 'c'. You might think since it looks a lot like a string, maybe it shares a lot of properties with a string.

2 Likes

In addition to the previous answers, note that for most functions:

  • missing silently propagate to missing (e.g. sum([1,2, missing]) evaluates to missing)
  • nothing results in an error (e.g. sum([1,2, nothing]) results in an error when evaluated)
1 Like

In Python (and Matlab, and perhaps others) strings can be created with single quotes, as well as with double quotes, unlike in Julia, where characters and strings are distinct. In Python or Matlab, '' is a valid value.

5 Likes

I want to extract valid data points from a list, since null-like/empty-like entries contains no information.

julia> func(val) = typeof(val) in (Int, Char) 
func (generic function with 1 method)

julia> [0, 1, false, 2, "", 3, 'a', 's', 34,[],missing] |> filter(func)
7-element Vector{Any}:
  0
  1
  2
  3
   'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)
   's': ASCII/Unicode U+0073 (category Ll: Letter, lowercase)
 34


1 Like

I think if you define clearly what empty means, we could help. For example, I get a grasp of what you want, but then you also consider false as an empty value.

Following @lmiq, you could try something more specific like this

func2(val) = (val isa Union{Number, String}) && !isempty(val)
[0, 1, false, 2,  "hello", 'a', "", nothing, [],missing] |> filter(func2)

which eliminates what you could consider “empty” (in the sense of not providing concrete information?)

5-element Vector{Any}:
     0
     1
 false
     2
      "hello"

but, again, the downside of this is that you need to specify the objects you want first, before indicating that you don’t want empty values. That’s why I used String instead of Char, but you should also add Char, AbstractArray, etc.

this is “false” according to OP

But 0 or false definitely contain information! Otherwise, you could compress any binary sequence (like eg. a file) by removing the zeros and keeping only the ones.

4 Likes

Hey, that’s a brilliant idea for a new compression algorithm. You could then take it even further- since now it’s just a sequence of ones you can compress that to just the number of ones. Now we can compress any file of any size to just a single integer. It might be a bit lossy but surely the amazing compression factor is worth it!

3 Likes