PriyoScriptPriyoScript
Basics

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

FeatureSyntaxDescription
Literal[1, 2, 3]Create an array
Index readarr[0]Read value at index
Index writearr[1] = 10Update index value
Slicearr[1:3]Extract subarray
For-each loopprakritiCount (item priyoInside arr)Iterate over elements

priyoArray Helper Functions Table

Core helpers

HelperSignatureReturns
lengthpriyoArray.length(arr)Number
pushpriyoArray.push(arr, value)New length
poppriyoArray.pop(arr)Removed value or priyoEmpty-like result
firstpriyoArray.first(arr)First value or null
lastpriyoArray.last(arr)Last value or null
atpriyoArray.at(arr, index)Value at index or null
slicepriyoArray.slice(arr, start?, end?)New array
reversepriyoArray.reverse(arr)New reversed array
includespriyoArray.includes(arr, value)Boolean
indexOfpriyoArray.indexOf(arr, value)Index or -1
joinpriyoArray.join(arr, sep?)String

Callback helpers

HelperSignatureReturns
mappriyoArray.map(arr, callback)New mapped array
filterpriyoArray.filter(arr, callback)New filtered array
reducepriyoArray.reduce(arr, callback, initial?)Accumulated value
forEachpriyoArray.forEach(arr, callback)null
findpriyoArray.find(arr, callback)Found value or null
somepriyoArray.some(arr, callback)Boolean
everypriyoArray.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
25

Example 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-30

Callback 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
priyoFalse

Invalid 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 argument

Practice Questions

Concept Check

  1. What is the difference between priyoArray.slice(...) and priyoArray.reverse(...) in mutation behavior? 2. When should you use map vs forEach? 3. Why does find return null when nothing matches?

Last updated on

On this page