11# Arrays
22
3- ## Built-in Array methods
4-
5- - ` arr_len() ` or ` len(arr) ` - returns the length of the array
6- - ` arr_push(array, item) ` - adds an item to the end of the array
7- - ` arr_pop(array, index) ` - removes an item from the end of the array
8- - ` arr_append(array, item) ` - adds an item to the end of the array
9- - ` arr_extend(array1, array2) ` - adds all the items of an array to
10- the end of the array
11- - ` arr_find(array, index) ` - returns the item at the specified index
12- - ` arr_slice(array, start, end) ` - returns the items from the specified
13- start index to the specified end index
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 |
1462
1563``` rn linenums="1" title="methods.rn"
16- const arr = [1, 2, 3, 4, 5]
17- print(arr_len(arr)) # 5
64+ var arr = [1, 2, 3, 4, 5]
1865
19- arr_push(arr, 6)
20- print(arr) # [1, 2, 3, 4, 5, 6]
66+ # Mutating
67+ arr.append(6)
68+ print(arr) # [1, 2, 3, 4, 5, 6]
2169
22- arr_pop( arr)
23- print(arr) # [1, 2, 3, 4, 5]
70+ arr.pop( )
71+ print(arr) # [1, 2, 3, 4, 5]
2472
25- arr_append( arr, 6 )
26- print(arr) # [1, 2, 3, 4, 5, 6]
73+ arr.extend([6, 7, 8] )
74+ print(arr) # [1, 2, 3, 4, 5, 6, 7, 8 ]
2775
28- arr_extend(arr, [7, 8, 9])
29- print(arr) # [1, 2, 3, 4, 5, 6, 7, 8, 9]
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
3082
31- print(arr_find(arr, 0)) # 1
32- print(arr_find(arr, 1)) # 2
83+ # Transforming
84+ print(arr.slice(0, 3)) # [1, 2, 3]
85+ print(arr.chunk(3)) # [[1, 2, 3], [4, 5, 6], [7, 8]]
3386
34- print(arr_slice(arr, 0, 5)) # [1, 2, 3, 4, 5]
87+ # Functional
88+ var doubled = arr.map(fun(x) -> x * 2)
89+ print(doubled) # [2, 4, 6, 8, 10, 12, 14, 16]
90+
91+ var evens = arr.filter(fun(x) -> x % 2 == 0)
92+ print(evens) # [2, 4, 6, 8]
93+
94+ # Aggregation
95+ print(arr.sum()) # 36
96+ print(arr.min()) # 1
97+ print(arr.max()) # 8
98+ ```
99+
100+ ## Array slicing
101+
102+ Arrays support slice syntax ` [start:end:step] ` . Any part can be omitted.
103+
104+ ``` rn linenums="1" title="slicing.rn"
105+ const arr = [0, 1, 2, 3, 4, 5]
106+
107+ print(arr[1:4]) # [1, 2, 3]
108+ print(arr[:3]) # [0, 1, 2]
109+ print(arr[3:]) # [3, 4, 5]
110+ print(arr[::2]) # [0, 2, 4]
111+ print(arr[::-1]) # [5, 4, 3, 2, 1, 0]
35112```
36113
37114## Array operators
@@ -47,40 +124,31 @@ print(arr1 + arr2) # [1, 2, 3, 4, 5, 6]
47124print(arr1 * 2) # [1, 2, 3, 1, 2, 3]
48125```
49126
50- ## Array standard library
127+ ## Array standard library (Legacy)
51128
52- - ` map(func) ` - returns a new array with the result of calling the specified
53- function on each item of the array
54- - ` append(item) ` - adds an item to the end of the array
55- - ` pop(index) ` - removes an item from the end of the array
56- - ` extend(list) ` - adds all the items of an array to the end of the array
57- - ` find(index) ` - returns the item at the specified index
58- - ` slice(start, end) ` - returns the items from the specified start index to
59- the specified end index
60- - ` len() ` - returns the length of the array
61- - ` is_empty() ` - returns ` true ` if the array is empty, otherwise ` false `
62- - ` to_string() ` - returns the string representation of the array
63- - ` 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.
64131
65- ``` rn linenums="1" title="array-standard-library.rn"
66- 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]
67135
68- # Create an array instance using the Array class
69- arr = 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]"
70139
71- print(len(arr)) # 5
72- print(arr.is_empty()) # false
73- print(arr.to_string()) # "[1, 2, 3, 4, 5]"
74- print(arr.is_array()) # true
140+ # Functional methods
141+ print(arr.map(fun(x) -> String(x))) # ["1", "2", "3", "4", "5"]
75142
76- 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]
77145
78- print( arr.append(6)) # [1, 2, 3, 4, 5, 6]
79- print(arr.pop(5)) # [1, 2, 3, 4, 5]
146+ arr.pop()
147+ print(arr) # [1, 2, 3, 4, 5]
80148
81- print(arr.extend([6, 7, 8])) # [1, 2, 3, 4, 5, 6, 7, 8]
82- print(arr.find(0)) # 1
83- print(arr.find(1)) # 2
149+ arr.extend([6, 7, 8])
150+ print(arr) # [1, 2, 3, 4, 5, 6, 7, 8]
84151
85- 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]
86154```
0 commit comments