FunctionArrayNextPrevious
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © RicardoSantos

//@version=5

// @description Methods to iterate through a array by a fixed anchor point.
library(title='FunctionArrayNextPrevious')

indexer (index=-1) => var int _s = index, _s += 1

array_next_method (array, int start_index=-1) => //{
    array.get(array, indexer(start_index) % array.size(array))
// @function retrieves the next value of the internal pointer index.
// @param array (any array type), array to iterate.
// @param start_index int, anchor index to start indexing.
export array_next (bool[] array, int start_index=-1)    => array_next_method(array, start_index)
export array_next (box[] array, int start_index=-1)     => array_next_method(array, start_index)
export array_next (color[] array, int start_index=-1)   => array_next_method(array, start_index)
export array_next (float[] array, int start_index=-1)   => array_next_method(array, start_index)
export array_next (int[] array, int start_index=-1)     => array_next_method(array, start_index)
export array_next (label[] array, int start_index=-1)   => array_next_method(array, start_index)
export array_next (line[] array, int start_index=-1)    => array_next_method(array, start_index)
export array_next (linefill[] array, int start_index=-1)=> array_next_method(array, start_index)
export array_next (string[] array, int start_index=-1)  => array_next_method(array, start_index)
export array_next (table[] array, int start_index=-1)   => array_next_method(array, start_index)
//{ usage:
sample = array.from(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
plot(array_next(sample))
plot(array_next(sample, dayofweek))
//}}

array_previous_method (array, int start_index=-1) => //{
    int _size = array.size(array)
    array.get(array, (_size - 1) - (indexer(start_index) % _size))
// @function retrieves the previous value of the internal pointer index.
// @param array (any array type), array to iterate.
// @param start_index int, anchor index to start indexing.
export array_previous (bool[] array, int start_index=-1)    => array_previous_method(array, start_index)
export array_previous (box[] array, int start_index=-1)     => array_previous_method(array, start_index)
export array_previous (color[] array, int start_index=-1)   => array_previous_method(array, start_index)
export array_previous (float[] array, int start_index=-1)   => array_previous_method(array, start_index)
export array_previous (int[] array, int start_index=-1)     => array_previous_method(array, start_index)
export array_previous (label[] array, int start_index=-1)   => array_previous_method(array, start_index)
export array_previous (line[] array, int start_index=-1)    => array_previous_method(array, start_index)
export array_previous (linefill[] array, int start_index=-1)=> array_previous_method(array, start_index)
export array_previous (string[] array, int start_index=-1)  => array_previous_method(array, start_index)
export array_previous (table[] array, int start_index=-1)   => array_previous_method(array, start_index)
//{ usage:
plot(array_previous(sample))
plot(array_previous(sample, dayofweek))
//}}

str = '>: '
for _i = 1 to 5
    str += str.tostring(array_next(sample))
str += '\n<: '
for _i = 1 to 5
    str += str.tostring(array_previous(sample))
label.new(bar_index, 0.0, str)