Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are empty Raku shaped Arrays truthy?

Tags:

raku

I was a bit surprised to see this:

> say my @i.so
False
> say my @i[3].so
True

Can anyone explain why, in raku, empty Arrays are falsey, yet empty shaped Arrays are truthy?

like image 853
p6steve Avatar asked May 03 '21 18:05

p6steve


Video Answer


1 Answers

If the description "empty" refers to "no elements", then there's no such thing as an empty fixed-sized (aka "shaped") array:

say my @unshaped-array;                   # []
say @unshaped-array.shape;                # (*) ("whatever" shape, or "unshaped")

say @unshaped-array.elems;                # 0
say @unshaped-array.so;                   # False
say @unshaped-array[0,1,2]:exists;        # (False False False)
say @unshaped-array[0,1,2]:delete;        # ((Any) (Any) (Any))
say @unshaped-array[0,1,2]:exists;        # (False False False)
say @unshaped-array;                      # []

say @unshaped-array = [Any,Any,Any];      # [(Any) (Any) (Any)]
say @unshaped-array.elems;                # 3
say @unshaped-array.so;                   # True
say @unshaped-array[0,1,2]:exists;        # (True True True)
say @unshaped-array[0,1,2]:delete;        # ((Any) (Any) (Any))
say @unshaped-array[0,1,2]:exists;        # (False False False)
say @unshaped-array;                      # []

say my @shaped-array[3];                  # [(Any) (Any) (Any)]
say @shaped-array.shape;                  # (3)

say @shaped-array.elems;                  # 3
say @shaped-array.so;                     # True
say @shaped-array[0,1,2]:exists;          # (False False False)
say @shaped-array[0,1,2]:delete;          # (Nil Nil Nil)
say @shaped-array[0,1,2]:exists;          # (False False False)
say @shaped-array;                        # [(Any) (Any) (Any)]

say @shaped-array = [42];                 # [42 (Any) (Any)]
say @shaped-array.elems;                  # 3
say @shaped-array.so;                     # True
say @shaped-array[0,1,2]:exists;          # (True False False)
say @shaped-array[0,1,2]:delete;          # (42 Nil Nil)
say @shaped-array[0,1,2]:exists;          # (False False False)
say @shaped-array = [Nil];                # [(Any) (Any) (Any)]

# Just because I named the array "shaped", doesn't mean its shape is fixed:

say @shaped-array := [Nil];               # [(Any)]
say @shaped-array.shape;                  # (*) ("whatever" shape, or "unshaped")
say @shaped-array := [];                  # []
say @shaped-array.elems;                  # 0
say @shaped-array.so;                     # False

my @i[0];                                 # Illegal ...

I don't know if there's currently a way to declare a variable that's permanently bound to a fixed shape array.

like image 87
raiph Avatar answered Oct 13 '22 22:10

raiph