Skip to content

Commit e94073b

Browse files
committed
Add array docs
1 parent 0984674 commit e94073b

File tree

4 files changed

+202
-15
lines changed

4 files changed

+202
-15
lines changed

docs/guide/language/enum-match.md renamed to docs/guide/language/enum-match.mdx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Badge } from "rspress/theme";
2+
13
# Enum and Match
24

35
Enums (short for enumerations) and match expressions are powerful features in AIScript that work together to provide type-safe pattern matching. These features are inspired by Rust's enum and match system, offering an elegant way to handle variants and control flow.
@@ -187,7 +189,7 @@ match value {
187189
// Double digits: 42
188190
```
189191

190-
### Match with Variable Binding
192+
### Match with Variable Binding <Badge text="Not supported yet" type="warning" />
191193

192194
You can bind values to variables in match patterns:
193195

docs/std/builtin/_meta.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
"name": "functions",
55
"label": "Functions"
66
},
7+
{
8+
"type": "file",
9+
"name": "array",
10+
"label": "Array"
11+
},
712
{
813
"type": "file",
914
"name": "string",

docs/std/builtin/array.md

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
# Array
2+
3+
The `Array` type provides various methods for array manipulation, searching, and ordering.
4+
5+
## append(item)
6+
7+
Adds an item to the end of the array and returns the modified array.
8+
9+
**Parameters**:
10+
- `item`: The item to add to the end of the array.
11+
12+
**Return Type**: `Array`
13+
14+
**Example**:
15+
```js
16+
let numbers = [1, 2, 3];
17+
numbers.append(4); // [1, 2, 3, 4]
18+
```
19+
20+
## extend(array)
21+
22+
Extends the array by appending all items from another array and returns the modified array.
23+
24+
**Parameters**:
25+
- `array`: The array containing items to append.
26+
27+
**Return Type**: `Array`
28+
29+
**Example**:
30+
```js
31+
let numbers = [1, 2, 3];
32+
numbers.extend([4, 5]); // [1, 2, 3, 4, 5]
33+
```
34+
35+
## insert(index, item)
36+
37+
Inserts an item at the specified position in the array and returns the modified array.
38+
39+
**Parameters**:
40+
- `index`: The index before which to insert the item.
41+
- `item`: The item to insert.
42+
43+
**Return Type**: `Array`
44+
45+
**Example**:
46+
```js
47+
let numbers = [1, 2, 4];
48+
numbers.insert(2, 3); // [1, 2, 3, 4]
49+
```
50+
51+
## remove(item)
52+
53+
Removes the first occurrence of the specified item from the array and returns the modified array.
54+
55+
**Parameters**:
56+
- `item`: The item to remove.
57+
58+
**Return Type**: `Array`
59+
60+
**Example**:
61+
```js
62+
let numbers = [1, 2, 3, 2];
63+
numbers.remove(2); // [1, 3, 2]
64+
```
65+
66+
## pop(index?)
67+
68+
Removes and returns the item at the specified position. If no index is provided, removes and returns the last item.
69+
70+
**Parameters**:
71+
- `index`: The index of the item to remove (optional).
72+
73+
**Return Type**: The removed item.
74+
75+
**Example**:
76+
```js
77+
let numbers = [1, 2, 3];
78+
let last = numbers.pop(); // 3, numbers becomes [1, 2]
79+
let first = numbers.pop(0); // 1, numbers becomes [2]
80+
```
81+
82+
## clear()
83+
84+
Removes all items from the array and returns the modified (empty) array.
85+
86+
**Return Type**: `Array`
87+
88+
**Example**:
89+
```js
90+
let numbers = [1, 2, 3];
91+
numbers.clear(); // []
92+
```
93+
94+
## index(item, start?, end?)
95+
96+
Returns the index of the first occurrence of the specified item within the optional start and end range.
97+
98+
**Parameters**:
99+
- `item`: The item to search for.
100+
- `start`: The index to start the search from (optional).
101+
- `end`: The index to end the search at (optional).
102+
103+
**Return Type**: `Number` (Returns -1 if the item is not found)
104+
105+
**Example**:
106+
```js
107+
let numbers = [10, 20, 30, 20];
108+
numbers.index(20); // 1
109+
numbers.index(20, 2); // 3
110+
```
111+
112+
## count(item)
113+
114+
Returns the number of occurrences of the specified item in the array.
115+
116+
**Parameters**:
117+
- `item`: The item to count.
118+
119+
**Return Type**: `Number`
120+
121+
**Example**:
122+
```js
123+
let numbers = [1, 2, 3, 2, 1];
124+
numbers.count(2); // 2
125+
```
126+
127+
## sort(reverse?)
128+
129+
Sorts the items of the array in place and returns the modified array.
130+
131+
**Parameters**:
132+
- `reverse`: Whether to sort in descending order (optional, default: false).
133+
134+
**Return Type**: `Array`
135+
136+
**Example**:
137+
```js
138+
let numbers = [3, 1, 4, 2];
139+
numbers.sort(); // [1, 2, 3, 4]
140+
numbers.sort(true); // [4, 3, 2, 1]
141+
```
142+
143+
## reverse()
144+
145+
Reverses the elements of the array in place and returns the modified array.
146+
147+
**Return Type**: `Array`
148+
149+
**Example**:
150+
```js
151+
let numbers = [1, 2, 3];
152+
numbers.reverse(); // [3, 2, 1]
153+
```
154+
155+
## slice(start, end?)
156+
157+
Returns a shallow copy of a portion of the array into a new array, without modifying the original array.
158+
159+
**Parameters**:
160+
- `start`: The beginning index (can be negative to index from the end).
161+
- `end`: The end index (optional, can be negative to index from the end).
162+
163+
**Return Type**: `Array`
164+
165+
**Example**:
166+
```js
167+
let numbers = [1, 2, 3, 4, 5];
168+
numbers.slice(1, 4); // [2, 3, 4]
169+
numbers.slice(-2); // [4, 5]
170+
```

theme/components/Landingpage/features.yaml

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ web:
137137
name: str
138138
}
139139
140-
return "repo: " + name;
140+
return f"repo: {query.name}";
141141
}
142142
filename: get.ai
143143
- code: |
@@ -152,7 +152,7 @@ web:
152152
name: str
153153
}
154154
155-
return "create repo: " + name;
155+
return f"create repo: {body.name}";
156156
}
157157
filename: post.ai
158158
- code: |
@@ -166,14 +166,14 @@ web:
166166
name: str
167167
}
168168
169-
return "update repo: " + name;
169+
return f"update repo: {body.name}";
170170
}
171171
filename: put.ai
172172
- code: |
173173
delete /repo/<id:int> {
174174
"""Delete a repository"""
175175
176-
return "delete repo: " + id;
176+
return f"delete repo: {id}";
177177
}
178178
filename: delete.ai
179179
- title: Validator
@@ -237,7 +237,7 @@ web:
237237
message: str,
238238
}
239239
240-
return f"Input: {body.message}!";
240+
return f"Input: {body.message}!";
241241
}
242242
filename: openapi.ai
243243
- img: /toucan.png
@@ -716,13 +716,23 @@ std-library:
716716
print(text.split(" ")); // ["hello", "world"]
717717
718718
// Array operations
719-
let numbers = [1, 2, 3, 4, 5];
720-
print(numbers.sum()); // 15
721-
print(numbers.average()); // 3
722-
print(numbers.map(fn(x) => x * 2)); // [2, 4, 6, 8, 10]
723-
724-
// Date and time
725-
let now = datetime.now();
726-
print(now.format("YYYY-MM-DD")); // 2024-03-21
727-
print(now.add_days(7)); // 2024-03-28
719+
// Initialize a test array
720+
let numbers = [10, 5, 8, 3, 1];
721+
print(numbers); // expect: [10, 5, 8, 3, 1]
722+
723+
// append - Add an item to the end
724+
numbers.append(20);
725+
print(numbers); // expect: [10, 5, 8, 3, 1, 20]
726+
727+
// extend - Extend with another array
728+
numbers.extend([30, 40]);
729+
print(numbers); // expect: [10, 5, 8, 3, 1, 20, 30, 40]
730+
731+
// insert - Insert an item at position
732+
numbers.insert(2, 15);
733+
print(numbers); // expect: [10, 5, 15, 8, 3, 1, 20, 30, 40]
734+
735+
// sort - Sort the array
736+
let sorted = numbers.sort();
737+
print(sorted); // expect: [1, 3, 3, 5, 8, 10, 15, 20, 30, 40]
728738
filename: builtin.ai

0 commit comments

Comments
 (0)