Slices in Swift

import Foundation

func main() {
    // Unlike arrays, slices are typed only by the
    // elements they contain (not the number of elements).
    // An uninitialized slice equals to nil and has
    // length 0.
    var s: [String]?
    print("uninit:", s as Any, s == nil, s?.count == 0)

    // To create an empty slice with non-zero length, use
    // Array(repeating:count:). Here we make a slice of
    // Strings of length 3 (initially empty strings).
    s = Array(repeating: "", count: 3)
    print("emp:", s as Any, "len:", s?.count ?? 0, "cap:", s?.capacity ?? 0)

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

    // `count` returns the length of the slice as expected.
    print("len:", s?.count ?? 0)

    // In addition to these basic operations, slices
    // support several more that make them richer than
    // arrays. One is the `append` method, which
    // returns a slice containing one or more new values.
    s?.append("d")
    s?.append(contentsOf: ["e", "f"])
    print("apd:", s as Any)

    // Slices can also be copied. Here we create a
    // new slice `c` and copy the contents of `s` into it.
    let c = s
    print("cpy:", c as Any)

    // Slices support a "slice" operator with the syntax
    // slice[low...high]. For example, this gets a slice
    // of the elements s[2], s[3], and s[4].
    let l = s?[2...4]
    print("sl1:", l as Any)

    // This slices up to (but excluding) s[5].
    let l2 = s?[..<5]
    print("sl2:", l2 as Any)

    // And this slices up from (and including) s[2].
    let l3 = s?[2...]
    print("sl3:", l3 as Any)

    // We can declare and initialize a variable for slice
    // in a single line as well.
    let t = ["g", "h", "i"]
    print("dcl:", t)

    // Swift doesn't have a built-in `slices` package, but we can
    // compare arrays directly.
    let t2 = ["g", "h", "i"]
    if t == t2 {
        print("t == t2")
    }

    // Arrays can be composed into multi-dimensional data
    // structures. The length of the inner arrays can
    // vary, unlike with multi-dimensional arrays.
    var twoD: [[Int]] = []
    for i in 0..<3 {
        let innerLen = i + 1
        var inner: [Int] = []
        for j in 0..<innerLen {
            inner.append(i + j)
        }
        twoD.append(inner)
    }
    print("2d: ", twoD)
}

main()

Note that while Swift uses arrays instead of slices, they behave similarly to Go slices in many ways. Here are some key differences and adaptations:

  1. Swift uses [Type] for array/slice declaration instead of []Type.
  2. Swift uses Array(repeating:count:) to create an array with a specific size and default value, similar to Go’s make([]Type, length).
  3. Swift uses .append() and .append(contentsOf:) methods instead of the append() function.
  4. Swift uses ranges (... and ..<) for slicing instead of Go’s [:] syntax.
  5. Swift doesn’t have a direct equivalent to Go’s cap() function, but we can use the capacity property.
  6. Swift doesn’t have a built-in slices package, so we compare arrays directly.

When you run this program, you’ll see output similar to the Go version, demonstrating the various operations on Swift arrays (which are used here in place of Go slices).

Swift arrays provide similar functionality to Go slices, allowing for dynamic sizing and various slicing operations. However, the syntax and some specific behaviors may differ between the two languages.