Skip to content

Commit

Permalink
Add docs for lists
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Livesey committed Jun 30, 2024
1 parent 0e5085a commit 6c8d513
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
72 changes: 72 additions & 0 deletions docs/en/voxel/lists.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Lists
A **list** is a data type which can contain a sequence of values.

Lists can be created in Voxel using the following syntax:

```voxel
var item = "banana";
var list = [123, true, null, "hello", item, [3, 2, 1], {object: true}];
```

List items are enclosed in square brackets (`[` and `]`), and are delimeted by commas (`,`). Items can be of any type.

## Reference

### `List`
The list data type.

#### `length: Number`
The number of items in the list.

#### `push(value: *): Number`
Push an item (`value`) onto the end of the list and return the new list length.

#### `pop(): *`
Pop the last item from the list (which removes it) and return that item.

#### `unshift(value: *): Number`
Insert an item (`value`) at the start of the list and return the new list length.

#### `shift(): *`
Remove the first item from the list and return that item.

#### `insert(index: Number, value: *): *`
Insert an item (`value`) onto the list at a specified index (`index`) and return that item.

#### `removeAt(index: Number): *`
Remove an item from the list at a specified index (`index`) and return that item.

#### `indexOf(value: *): Number`
Find the index of the first occurrence of a value (`value`) in the list and return it.

If the item cannot be found, `-1` will be returned instead.

#### `contains(value: *): Boolean`
Return `true` if a value (`value`) is in the list; otherwise, return `false`.

#### `remove(value: *): Boolean`
Remove the first occurrence of a value (`value`) in the list. Return `true` if it was found and removed; otherwise, return `false`.

#### `forEach(callback: function(item: *, index: Number))`
Iterate through the list and call a callback function (`callback`) for every item with arguments that reference the item (`item`) and the item's index (`index`).

#### `map(callback: function(item: *, index: Number): *): List<*>`
Iterate through the list and call a callback function (`callback`) for every item with arguments that reference the item (`item`) and the item's index (`index`).

The callback function must return a new value. A new list will be returned when calling `map` containing all values returned from calling the callback function for every item.

#### `filter(callback: function(item: *, index: Number): Boolean): List<*>`
Iterate through the list and call a callback function (`callback`) for every item with arguments that reference the item (`item`) and the item's index (`index`).

The callback function must return a boolean value. A new list will be returned when calling `filter` containing all values from the original list where the callback function returned `true`.

#### `find(callback: function(item: *): Boolean): *`
Iterate through the list until the callback function (`callback`) called for every item returns `true`. Return the list item that caused the callback function to return `true`. The callback function is called with an argument that references the item (`item`).

#### `findIndex(callback: function(item: *): Boolean): Number`
Iterate through the list until the callback function (`callback`) called for every item returns `true`. Return the index of the list item that caused the callback function to return `true`. The callback function is called with an argument that references the item (`item`).

#### `reduce(callback: function(accumulator: *, item: *): *, initialValue: *): *`

Iterate through the list and call a callback function (`callback`) for every item. The callback will be called with arguments for the previous call's return value or the initial provided value (`initialValue`) if it is the first call (`accumulator`), and the current list item (`item`). The last call's return value will be used as the return value of `reduce`.
4 changes: 3 additions & 1 deletion stdlib/core.vxl
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,9 @@ function getProperty(thing, property) {

if (#usedprop(unshift) &&& property == #prop(unshift)) {
return function(value) {
return syscall Li(value, thing, 0);
syscall Li(value, thing, 0);

return syscall Ll(thing);
};
}

Expand Down

0 comments on commit 6c8d513

Please sign in to comment.