# Xojo ArrayUtils API2

Dec 27, 2020 · 2 min read

After my conversion of Joe Strout’s “StringUtils Harness” to API2 it probably doesn’t come as a surprise that I converted as well his library for manipulating Arrays to Xojo’s API version 2. You can find the repository here:

The available methods are:

• Average
• Concat
• Clone
• MaxAndMin
• RemoveSlice
• Reverse
• Slice
• Splice
• Sum

I renamed “append” to “add” to ensure compatibility with the new naming conventions of API2. So please take care of this if you have used this library in older projects you want to convert.

# Slice Indexing

Several of the methods in this module take two indexes to specify a subset
off an array. Such a subset is called a “slice” and is involved in methods
such as RemoveSlice, Slice, and Splice.

In such cases, the first index specifies the first element in the slice. It may
be either an ordinary, non-negative index, in which case 0 means the first
element of the array, 1 means the second element, and so on. Or it may be
a negative number, in which case we count from the end of the array; -1 is
the last element, -2 is the next-to-last, and so on.

The second index specifies the element AFTER the last one to be included
in the slice. Again this may be positive or negative, but 0 in this case means
one past the end of the array.

This indexing scheme seems a bit odd at first (unless you are familiar with
Python), but it has a lot of advantages. Suppose you have an array ‘a’ with
five elements. Then you have:

a.Slice(0,0) is the entire array
a.Slice(0,-1) is all but the last element
a.Slice(1,0) is all but the first element
a.Slice(0,2) is the first two elements
a.Slice(2,0) is everything except the first two elements

Indeed, you can generalize and note that a.Slice(0,n).Concat( a.Slice(n,0) )
always comes out equal to n. Another advantage is that if you have a loop,
where either the first index is counting down to 0 or the last index is
counting up (from negative numbers) to 0, it will do the right thing in the
boundary cases (i.e. when the counter hits 0).

Written by

Written by