Skip to content

Commit 2ba189a

Browse files
committed
docs: update documentation for objective method syntax
BREAKING CHANGE: Document removal of arr_*/str_* builtin functions - Remove arr_append, arr_pop, arr_extend, arr_chunk, arr_get, arr_len docs - Remove str_len, str_find, str_get docs - Add Array methods reference (27 methods) - Add String methods reference (25+ methods) - Add Number methods reference (28 methods) - Add Boolean methods reference (9 methods) - Add HashMap methods reference (18 methods) - Mark array/string stdlib modules as legacy - Add 'Primitive Methods' section to built-in-functions.md - Update quick-start.md example to use .length()
1 parent 3326e4a commit 2ba189a

6 files changed

Lines changed: 444 additions & 106 deletions

File tree

docs/arrays.md

Lines changed: 107 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,100 @@
11
# Arrays
22

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 |
1162

1263
```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]
1575
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
1882
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]]
2186
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]
2490
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]
2793
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
2998
```
3099

31100
## Array slicing
@@ -55,40 +124,31 @@ print(arr1 + arr2) # [1, 2, 3, 4, 5, 6]
55124
print(arr1 * 2) # [1, 2, 3, 1, 2, 3]
56125
```
57126

58-
## Array standard library
127+
## Array standard library (Legacy)
59128

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.
72131

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]
75135
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]"
78139
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"]
83142
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]
85145
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]
88148
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]
92151
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]
94154
```

docs/built-in-functions.md

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,6 @@ Radon exposes a fixed set of interpreter built-ins. The list below reflects the
3737
- `is_fun(value)` returns whether the value is a function.
3838
- `is_null(value)` returns whether the value is null.
3939

40-
## Array Helpers
41-
42-
- `arr_append(array, value)` appends a value to an array.
43-
- `arr_pop(array, index=-1)` removes and returns an element.
44-
- `arr_extend(arrayA, arrayB)` appends all elements from one array to another.
45-
- `arr_chunk(array, size)` groups an array into chunks.
46-
- `arr_get(array, index)` returns the element at an index.
47-
- `arr_len(array)` returns the number of elements.
48-
49-
## String Helpers
50-
51-
- `str_len(string)` returns string length.
52-
- `str_find(string, value)` returns the index of a substring, or `-1`.
53-
- `str_get(string, index)` returns the character at an index.
54-
5540
## Runtime and Interop
5641

5742
- `require(module)` executes a standard library module name or a `.rn` file path.
@@ -113,3 +98,45 @@ print(res.text)
11398
### `builtins`
11499

115100
Provides access to the built-in scope as an object. Used for reflection and tooling; not normally needed in application code.
101+
102+
---
103+
104+
## Primitive Methods
105+
106+
All primitive types (Array, String, Number, Boolean, HashMap) support objective method syntax via dot notation. Methods are called directly on values without needing any imports.
107+
108+
```rn linenums="1" title="primitive_methods.rn"
109+
# Array methods
110+
var arr = [1, 2, 3]
111+
arr.append(4) # [1, 2, 3, 4]
112+
arr.length() # 4
113+
arr.pop() # removes and returns 4
114+
115+
# String methods
116+
var s = "hello world"
117+
s.upper() # "HELLO WORLD"
118+
s.split(" ") # ["hello", "world"]
119+
s.length() # 11
120+
121+
# Number methods
122+
var n = 42
123+
n.is_even() # true
124+
n.sqrt() # 6.48...
125+
(-5).abs() # 5
126+
127+
# Boolean methods
128+
var b = true
129+
b.toggle() # false
130+
b.to_string() # "true"
131+
132+
# HashMap methods
133+
var hm = {"a": 1, "b": 2}
134+
hm.keys() # ["a", "b"]
135+
hm.has("a") # true
136+
```
137+
138+
For complete method references, see:
139+
140+
- [Arrays](arrays.md) - 27 methods
141+
- [Strings](strings.md) - 25+ methods
142+
- [Data Types](data-types.md) - Number (28), Boolean (9), HashMap (18) methods

0 commit comments

Comments
 (0)