Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there cases when I have to use `elems` with an array to make it work?

After reading this answer I looked through my code and found some places where I use elems with arrays.

I could remove all elems without affecting the code:

my @a = 1, 2, 3, 4, 5;
my $b = [ 1, 2, 3, 4 ];

my $i = 2;
say $i + @a.elems;
say $i + @a;

say "===============";
say @a.elems;
say 0 + @a;

say "===============";
say $b.elems / 2;
say $b / 2;

say "===============";
while state $c++ > $b.elems {
    say $c;
}
while state $d++ > $b {
    say $d;
}

That led me to wonder if there are situations where arrays have to be called with the elems function to make the code work.

like image 957
sid_com Avatar asked Apr 04 '20 08:04

sid_com


People also ask

How do you define an array called array1 in JavaScript?

Creating an Array Using an array literal is the easiest way to create a JavaScript Array. Syntax: const array_name = [item1, item2, ...]; It is a common practice to declare arrays with the const keyword.

What is array explain with example?

An array is a variable that can store multiple values. For example, if you want to store 100 integers, you can create an array for it. int data[100];

What is array in C programming PDF?

Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value. To create an array, define the data type (like int ) and specify the name of the array followed by square brackets [].

How many types of arrays are there in JavaScript?

Arrays are just regular objects In Javascript, there are only 6 data types defined – the primitives (boolean, number, string, null, undefined) and object (the only reference type).

What is an array?

1 Array stores data elements of the same data type. 2 Arrays can be used for CPU scheduling. 3 Used to Implement other data structures like Stacks, Queues, Heaps, Hash tables, etc. More ...

What are the advantages and disadvantages of using arrays?

Advantages of using arrays: 1 Arrays allow random access to elements. This makes accessing elements by position faster. 2 Arrays have better cache locality that makes a pretty big difference in performance. 3 Arrays represent multiple data items of the same type using a single name.

How to access all elements of an array in C?

If you need to access all elements of an array we can use loops. The best for sequential access is the In line 3, the for loop, starting at the first element of the loop, sequentially gets each value for the array. This works well if you just need the value.

How to create an array and seed it with values?

We can create an array and seed it with values just by placing them in the @ () parentheses. This array has 4 items. When we call the $data variable, we see the list of our items. If it's an array of strings, then we get one line per string. We can declare an array on multiple lines.


1 Answers

Whenever a numeric operator sees an Iterable as one of its operands, it will call the .elems method on it. Sometimes that will result in a Failure or an Exception being thrown:

$ raku -e 'say (1 ... *) + 42'
Cannot .elems a lazy list

Mind you, using the .elems in your code has 2 advantages:

  • it makes your intention explicit, good for future maintainers
  • it will be slightly more efficient, because it will skip the operator call
like image 134
Elizabeth Mattijsen Avatar answered Oct 08 '22 22:10

Elizabeth Mattijsen