Arrays
Complete array guide with literals, indexing, slicing, and all priyoArray helper functions.
Theory
Arrays are ordered collections used to store multiple values under one variable name. They solve repeated-data problems such as lists of marks, names, prices, matrix rows, and filtered search results. In PriyoScript, arrays are first-class values: you can create them, mutate them, slice them, iterate through them, and process them with helper methods. A strong array foundation is essential for writing practical programs.
Why arrays matter
If you are handling repeated values, arrays usually make the code shorter, safer, and easier to maintain.
Array Basics Table
| Feature | Syntax | Description |
|---|---|---|
| Literal | [1, 2, 3] | Create an array |
| Index read | arr[0] | Read value at index |
| Index write | arr[1] = 10 | Update index value |
| Slice | arr[1:3] | Extract subarray |
| For-each loop | prakritiCount (item priyoInside arr) | Iterate over elements |
priyoArray Helper Functions Table
Core helpers
| Helper | Signature | Returns |
|---|---|---|
length | priyoArray.length(arr) | Number |
push | priyoArray.push(arr, value) | New length |
pop | priyoArray.pop(arr) | Removed value or priyoEmpty-like result |
first | priyoArray.first(arr) | First value or null |
last | priyoArray.last(arr) | Last value or null |
at | priyoArray.at(arr, index) | Value at index or null |
slice | priyoArray.slice(arr, start?, end?) | New array |
reverse | priyoArray.reverse(arr) | New reversed array |
includes | priyoArray.includes(arr, value) | Boolean |
indexOf | priyoArray.indexOf(arr, value) | Index or -1 |
join | priyoArray.join(arr, sep?) | String |
Callback helpers
| Helper | Signature | Returns |
|---|---|---|
map | priyoArray.map(arr, callback) | New mapped array |
filter | priyoArray.filter(arr, callback) | New filtered array |
reduce | priyoArray.reduce(arr, callback, initial?) | Accumulated value |
forEach | priyoArray.forEach(arr, callback) | null |
find | priyoArray.find(arr, callback) | Found value or null |
some | priyoArray.some(arr, callback) | Boolean |
every | priyoArray.every(arr, callback) | Boolean |
Basic Array Syntax Examples
Example 1: Literal + index read/write
monalisa {
priyoKeep nums = [10, 20, 30]
priyoTell(nums[0])
nums[1] = 25
priyoTell(nums[1])
}Output:
10
25Example 2: Slicing
monalisa {
priyoKeep nums = [10, 20, 30, 40]
priyoTell(nums[1:3])
}Output:
[20, 30]Core Helper Examples (all functions)
monalisa {
priyoKeep arr = [10, 20, 30]
priyoTell(priyoArray.length(arr))
priyoTell(priyoArray.push(arr, 40))
priyoTell(priyoArray.pop(arr))
priyoTell(priyoArray.first(arr))
priyoTell(priyoArray.last(arr))
priyoTell(priyoArray.at(arr, 1))
priyoTell(priyoArray.at(arr, 99))
priyoTell(priyoArray.slice(arr, 0, 2))
priyoTell(priyoArray.reverse(arr))
priyoTell(priyoArray.includes(arr, 20))
priyoTell(priyoArray.indexOf(arr, 30))
priyoTell(priyoArray.join(arr, "-"))
}Output:
3
4
40
10
30
20
null
[10, 20]
[30, 20, 10]
priyoTrue
2
10-20-30Callback Helper Examples (all functions)
monalisa {
lisaaTask double(x) {
priyoGiveBack x * 2
}
lisaaTask isEven(x) {
priyoGiveBack x % 2 == 0
}
lisaaTask add(acc, x) {
priyoGiveBack acc + x
}
lisaaTask printItem(x) {
priyoTell("item=" + x)
}
priyoKeep vals = [1, 2, 3, 4]
priyoTell(priyoArray.map(vals, double))
priyoTell(priyoArray.filter(vals, isEven))
priyoTell(priyoArray.reduce(vals, add, 0))
priyoArray.forEach(vals, printItem)
priyoTell(priyoArray.find(vals, isEven))
priyoTell(priyoArray.some(vals, isEven))
priyoTell(priyoArray.every(vals, isEven))
}Output:
[2, 4, 6, 8]
[2, 4]
10
item=1
item=2
item=3
item=4
2
priyoTrue
priyoFalseInvalid Usage Example
monalisa {
priyoTell(priyoArray.length("not-array"))
}Why invalid: first argument must be an array.
Expected error output:
priyoArray.length expects an array as the first argumentPractice Questions
Concept Check
- What is the difference between
priyoArray.slice(...)andpriyoArray.reverse(...)in mutation behavior? 2. When should you usemapvsforEach? 3. Why doesfindreturnnullwhen nothing matches?
Related Docs
Last updated on