Slices in ActionScript

// Unlike arrays, vectors are typed only by the
// elements they contain (not the number of elements).
// An uninitialized vector equals to null and has
// length 0.
var s:Vector.<String> = null;
trace("uninit:", s, s == null, s == null ? 0 : s.length == 0);

// To create an empty vector with non-zero length, use
// the Vector constructor. Here we make a vector of
// Strings of length 3 (initially null-valued).
s = new Vector.<String>(3);
trace("emp:", s, "len:", s.length, "cap:", s.length);

// We can set and get just like with arrays.
s[0] = "a";
s[1] = "b";
s[2] = "c";
trace("set:", s);
trace("get:", s[2]);

// length returns the length of the vector as expected.
trace("len:", s.length);

// In addition to these basic operations, vectors
// support several more that make them richer than
// arrays. One is the push method, which
// adds one or more new values to the end of the vector.
s.push("d");
s.push("e", "f");
trace("push:", s);

// Vectors can also be copied. Here we create a
// new vector c and copy the contents of s into it.
var c:Vector.<String> = s.concat();
trace("cpy:", c);

// Vectors support a "slice" operator with the slice method.
// For example, this gets a slice of the elements s[2], s[3], and s[4].
var l:Vector.<String> = s.slice(2, 5);
trace("sl1:", l);

// This slices up to (but excluding) s[5].
l = s.slice(0, 5);
trace("sl2:", l);

// And this slices up from (and including) s[2].
l = s.slice(2);
trace("sl3:", l);

// We can declare and initialize a variable for vector
// in a single line as well.
var t:Vector.<String> = new <String>["g", "h", "i"];
trace("dcl:", t);

// ActionScript doesn't have a built-in slices utility package,
// so we'll implement a simple equality check ourselves.
function vectorsEqual(v1:Vector.<String>, v2:Vector.<String>):Boolean {
    if (v1.length != v2.length) return false;
    for (var i:int = 0; i < v1.length; i++) {
        if (v1[i] != v2[i]) return false;
    }
    return true;
}

var t2:Vector.<String> = new <String>["g", "h", "i"];
if (vectorsEqual(t, t2)) {
    trace("t == t2");
}

// Vectors can be composed into multi-dimensional data
// structures. The length of the inner vectors can
// vary, unlike with multi-dimensional arrays.
var twoD:Vector.<Vector.<int>> = new Vector.<Vector.<int>>(3);
for (var i:int = 0; i < 3; i++) {
    var innerLen:int = i + 1;
    twoD[i] = new Vector.<int>(innerLen);
    for (var j:int = 0; j < innerLen; j++) {
        twoD[i][j] = i + j;
    }
}
trace("2d: ", twoD);

This ActionScript code demonstrates the usage of Vector, which is similar to slices in Go. Vectors in ActionScript are dynamic arrays that can grow or shrink in size. They provide functionality similar to slices, including the ability to create sub-vectors (slices) and append elements.

Note that ActionScript doesn’t have a direct equivalent to Go’s make function for creating slices, so we use the Vector constructor instead. Also, ActionScript doesn’t have a built-in package for vector utilities like Go’s slices package, so we implemented a simple equality check function ourselves.

The multi-dimensional vector example at the end shows how vectors can be nested to create more complex data structures, similar to multi-dimensional slices in Go.

When you run this ActionScript code, you should see output similar to the Go example, demonstrating the various operations on vectors.