|
1 | 1 | # Arrays |
2 | 2 |
|
3 | | -## Built-in Array functions |
4 | | - |
5 | | -- `arr_len(array)` or `len(array)` — returns the length of the array |
6 | | -- `arr_append(array, item)` — adds an item to the end of the array |
7 | | -- `arr_pop(array, index)` — removes and returns the item at the specified index |
8 | | -- `arr_extend(array1, array2)` — appends all items from `array2` to `array1` |
9 | | -- `arr_get(array, index)` — returns the item at the specified index |
10 | | -- `arr_chunk(array, size)` — groups the array into chunks of the given size |
| 3 | +## Array Methods |
| 4 | + |
| 5 | +Arrays have built-in methods accessible via dot notation. No imports required. |
| 6 | + |
| 7 | +### Mutating Methods |
| 8 | + |
| 9 | +| Method | Description | |
| 10 | +|--------|-------------| |
| 11 | +| `.append(value)` | Add element to end of array | |
| 12 | +| `.pop(index=-1)` | Remove and return element at index (default: last) | |
| 13 | +| `.extend(array)` | Append all elements from another array | |
| 14 | +| `.insert(index, value)` | Insert element at specified index | |
| 15 | +| `.remove(value)` | Remove first occurrence of value | |
| 16 | +| `.set(index, value)` | Set element at index | |
| 17 | +| `.clear()` | Remove all elements | |
| 18 | +| `.reverse()` | Reverse array in place | |
| 19 | +| `.sort(reverse=false)` | Sort array in place | |
| 20 | + |
| 21 | +### Query Methods |
| 22 | + |
| 23 | +| Method | Description | |
| 24 | +|--------|-------------| |
| 25 | +| `.length()` | Get number of elements | |
| 26 | +| `.get(index)` | Get element at index | |
| 27 | +| `.find(element)` | Find index of element (-1 if not found) | |
| 28 | +| `.index_of(element)` | Alias for find | |
| 29 | +| `.includes(element)` | Check if element exists (returns boolean) | |
| 30 | +| `.first()` | Get first element | |
| 31 | +| `.last()` | Get last element | |
| 32 | +| `.count(element)` | Count occurrences of element | |
| 33 | +| `.is_empty()` | Check if array is empty | |
| 34 | + |
| 35 | +### Transform Methods |
| 36 | + |
| 37 | +| Method | Description | |
| 38 | +|--------|-------------| |
| 39 | +| `.slice(start, end)` | Get sub-array from start to end | |
| 40 | +| `.chunk(size)` | Split into sub-arrays of given size | |
| 41 | +| `.copy()` | Create shallow copy | |
| 42 | +| `.unique()` | Return array with duplicates removed | |
| 43 | +| `.join(separator="")` | Join elements into string | |
| 44 | +| `.map(func)` | Transform each element with function | |
| 45 | +| `.filter(func)` | Filter elements by predicate function | |
| 46 | + |
| 47 | +### Aggregation Methods |
| 48 | + |
| 49 | +| Method | Description | |
| 50 | +|--------|-------------| |
| 51 | +| `.sum()` | Sum all numeric elements | |
| 52 | +| `.min()` | Get minimum element | |
| 53 | +| `.max()` | Get maximum element | |
| 54 | +| `.every(func)` | Check if all elements satisfy predicate | |
| 55 | +| `.some(func)` | Check if any element satisfies predicate | |
| 56 | + |
| 57 | +### Conversion Methods |
| 58 | + |
| 59 | +| Method | Description | |
| 60 | +|--------|-------------| |
| 61 | +| `.to_string()` | Convert to string representation | |
11 | 62 |
|
12 | 63 | ```rn linenums="1" title="methods.rn" |
13 | | -const arr = [1, 2, 3, 4, 5] |
14 | | -print(arr_len(arr)) # 5 |
| 64 | +var arr = [1, 2, 3, 4, 5] |
| 65 | +
|
| 66 | +# Mutating |
| 67 | +arr.append(6) |
| 68 | +print(arr) # [1, 2, 3, 4, 5, 6] |
| 69 | +
|
| 70 | +arr.pop() |
| 71 | +print(arr) # [1, 2, 3, 4, 5] |
| 72 | +
|
| 73 | +arr.extend([6, 7, 8]) |
| 74 | +print(arr) # [1, 2, 3, 4, 5, 6, 7, 8] |
15 | 75 |
|
16 | | -arr_append(arr, 6) |
17 | | -print(arr) # [1, 2, 3, 4, 5, 6] |
| 76 | +# Querying |
| 77 | +print(arr.length()) # 8 |
| 78 | +print(arr.get(0)) # 1 |
| 79 | +print(arr.includes(5)) # true |
| 80 | +print(arr.first()) # 1 |
| 81 | +print(arr.last()) # 8 |
18 | 82 |
|
19 | | -arr_pop(arr, 5) |
20 | | -print(arr) # [1, 2, 3, 4, 5] |
| 83 | +# Transforming |
| 84 | +print(arr.slice(0, 3)) # [1, 2, 3] |
| 85 | +print(arr.chunk(3)) # [[1, 2, 3], [4, 5, 6], [7, 8]] |
21 | 86 |
|
22 | | -arr_extend(arr, [6, 7, 8]) |
23 | | -print(arr) # [1, 2, 3, 4, 5, 6, 7, 8] |
| 87 | +# Functional |
| 88 | +var doubled = arr.map(fun(x) -> x * 2) |
| 89 | +print(doubled) # [2, 4, 6, 8, 10, 12, 14, 16] |
24 | 90 |
|
25 | | -print(arr_get(arr, 0)) # 1 |
26 | | -print(arr_get(arr, 1)) # 2 |
| 91 | +var evens = arr.filter(fun(x) -> x % 2 == 0) |
| 92 | +print(evens) # [2, 4, 6, 8] |
27 | 93 |
|
28 | | -print(arr_chunk(arr, 3)) # [[1, 2, 3], [4, 5, 6], [7, 8]] |
| 94 | +# Aggregation |
| 95 | +print(arr.sum()) # 36 |
| 96 | +print(arr.min()) # 1 |
| 97 | +print(arr.max()) # 8 |
29 | 98 | ``` |
30 | 99 |
|
31 | 100 | ## Array slicing |
@@ -55,40 +124,31 @@ print(arr1 + arr2) # [1, 2, 3, 4, 5, 6] |
55 | 124 | print(arr1 * 2) # [1, 2, 3, 1, 2, 3] |
56 | 125 | ``` |
57 | 126 |
|
58 | | -## Array standard library |
| 127 | +## Array standard library (Legacy) |
59 | 128 |
|
60 | | -- `map(func)` - returns a new array with the result of calling the specified |
61 | | - function on each item of the array |
62 | | -- `append(item)` - adds an item to the end of the array |
63 | | -- `pop(index)` - removes an item from the end of the array |
64 | | -- `extend(list)` - adds all the items of an array to the end of the array |
65 | | -- `find(index)` - returns the item at the specified index |
66 | | -- `slice(start, end)` - returns the items from the specified start index to |
67 | | - the specified end index |
68 | | -- `len()` - returns the length of the array |
69 | | -- `is_empty()` - returns `true` if the array is empty, otherwise `false` |
70 | | -- `to_string()` - returns the string representation of the array |
71 | | -- `is_array()` - returns `true` if the value is an array, otherwise `false` |
| 129 | +!!! note "Built-in Methods" |
| 130 | + Array methods are now built-in on all arrays. The `import array` module is kept for backwards compatibility but is no longer required. |
72 | 131 |
|
73 | | -```rn linenums="1" title="array-standard-library.rn" |
74 | | -import array # Include the array standard library |
| 132 | +```rn linenums="1" title="array-methods.rn" |
| 133 | +# No import needed - methods work directly on arrays |
| 134 | +var arr = [1, 2, 3, 4, 5] |
75 | 135 |
|
76 | | -# Create an array instance using the Array class |
77 | | -const arr = array.Array([1, 2, 3, 4, 5]) |
| 136 | +print(arr.length()) # 5 |
| 137 | +print(arr.is_empty()) # false |
| 138 | +print(arr.to_string()) # "[1, 2, 3, 4, 5]" |
78 | 139 |
|
79 | | -print(len(arr)) # 5 |
80 | | -print(arr.is_empty()) # false |
81 | | -print(arr.to_string()) # "[1, 2, 3, 4, 5]" |
82 | | -print(arr.is_array()) # true |
| 140 | +# Functional methods |
| 141 | +print(arr.map(fun(x) -> String(x))) # ["1", "2", "3", "4", "5"] |
83 | 142 |
|
84 | | -print(arr.map(fun (item) -> str(item))) # ["1", "2", "3", "4", "5"] |
| 143 | +arr.append(6) |
| 144 | +print(arr) # [1, 2, 3, 4, 5, 6] |
85 | 145 |
|
86 | | -print(arr.append(6)) # [1, 2, 3, 4, 5, 6] |
87 | | -print(arr.pop(5)) # [1, 2, 3, 4, 5] |
| 146 | +arr.pop() |
| 147 | +print(arr) # [1, 2, 3, 4, 5] |
88 | 148 |
|
89 | | -print(arr.extend([6, 7, 8])) # [1, 2, 3, 4, 5, 6, 7, 8] |
90 | | -print(arr.find(0)) # 1 |
91 | | -print(arr.find(1)) # 2 |
| 149 | +arr.extend([6, 7, 8]) |
| 150 | +print(arr) # [1, 2, 3, 4, 5, 6, 7, 8] |
92 | 151 |
|
93 | | -print(arr.slice(0, 5)) # [1, 2, 3, 4, 5] |
| 152 | +print(arr.find(3)) # 2 (index of element 3) |
| 153 | +print(arr.slice(0, 5)) # [1, 2, 3, 4, 5] |
94 | 154 | ``` |
0 commit comments