Xojo ArrayUtils API2

Jeannot Muller
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:

  • Add
  • 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).

Image for post
Image for post

Learn Xojo

Everything About Developing with Xojo.com

Jeannot Muller

Written by

Managing director at teccompanion.com, physician, founder, developer and blogger.

Learn Xojo

Code snippets, tips and tricks to develop like a pro with the fantastic xojo.com tool on Windows, macOS, iOS, Web and Linux

Jeannot Muller

Written by

Managing director at teccompanion.com, physician, founder, developer and blogger.

Learn Xojo

Code snippets, tips and tricks to develop like a pro with the fantastic xojo.com tool on Windows, macOS, iOS, Web and Linux

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store