Learning Xojo

After my conversion of Joe Strout’s

Xojo StringUtils Harness API2
Joe Strout wrote ages ago a toolset for string manipilations beyond Xojo’s out-of-the-box functionality…

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.

Repository

You can find the repository here:

Jeannot-Muller/Xojo-ArrayUtils-API2
Contribute to Jeannot-Muller/Xojo-ArrayUtils-API2 development by creating an account on GitHub.

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

Comments

Sign in or become a blog.xojoDOCs.com member to join the conversation.
Just enter your email below to get a log in link.



You’ve successfully subscribed to blog.xojoDOCs.com
Welcome back! You’ve successfully signed in.
Great! You’ve successfully signed up.
Your link has expired
Success! Check your email for magic link to sign-in.