User:Sravani Jampana/sandbox/ArrayTools

Extension: ArrayTools

Purpose

The ArrayTools extension provides a comprehensive set of pure, Parsoid-compatible parser functions designed to facilitate advanced array manipulations. These parser functions are pure, ensuring that they do not alter any pre-existing arrays and only produce results based on their input arguments.

Features

ArrayTools offers a wide range of functionalities including but not limited to: - Constructing and initializing arrays - Retrieving and extracting data from arrays - Transforming arrays to create new arrays - Performing operations on each element of an array - Utility functions for common array operations

Functions

The ArrayTools extension includes the following types of functions:

1. Array Construction - **createArray**: Initializes a new array with specified elements. - **initArray**: Creates an array with a given size and initial value.

2. Data Extraction - **getElement**: Retrieves an element at a specified index. - **findElement**: Searches for an element and returns its index.

3. Array Transformation - **mapArray**: Applies a function to each element of the array and returns a new array. - **filterArray**: Creates a new array with elements that meet specified criteria.

4. Iteration - **forEachElement**: Executes a function for each element in the array without returning a new array. - **reduceArray**: Reduces the array to a single value using a specified accumulator function.

5. Utility Functions - **arrayLength**: Returns the number of elements in an array. - **concatArrays**: Combines multiple arrays into one. - **sliceArray**: Extracts a section of an array and returns it as a new array.

Example Usage

```lua -- Constructing a new array local arr = createArray(1, 2, 3, 4, 5)

-- Getting an element at index 2 local elem = getElement(arr, 2)

-- Mapping array to double each element local doubledArr = mapArray(arr, function(x) return x * 2 end)

-- Filtering array for elements greater than 3 local filteredArr = filterArray(arr, function(x) return x > 3 end)

-- Reducing array to sum all elements local sum = reduceArray(arr, function(acc, x) return acc + x end, 0)