Skip to content

Commit

Permalink
Add intro for arrays concept
Browse files Browse the repository at this point in the history
  • Loading branch information
m-dango committed Sep 8, 2023
1 parent e144a6c commit d603cf5
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions concepts/arrays-lists-sequences/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
A `List` is an ordered sequence of values.
A `List` is immutable, but if any of the values in a `List` are containers, the values of those containers can be changed.

An `Array` is a type of List which is mutable, i.e., the contents and size of an array can both be changed.

An `Array` can have a type constraint.

`Array`s and `List`s have the `Positional` role, which allows for using the postcircumfix `[]` operator to access elements by their indexes.
This is also known as [positional subscripting][positional-subscripting].

Indexes start at 0.
Raku does not support negative indexes, but instead a code block can be used inside a positional subscript to emulate the same behavior.
The argument to this code block will be the number of elements in the `List`.

```raku
# All of the following will give 'c'
('a', 'b', 'c')[*-1];
('a', 'b', 'c')[{ $_ - 1 }];
('a', 'b', 'c')[-> $elems { $elems - 1 }];
```

[positional-subscripting]: https://docs.raku.org/language/subscripts#Positional_subscripting

0 comments on commit d603cf5

Please sign in to comment.