Arrays are generic collections, so helper functions can preserve the element type they receive.

Work with element type

repeatCount
generic_collections.swift
Replay: real traced execution (multi-file project)
func repeated<T>(_ value: T, count: Int) -> [T] {
    var output: [T] = []
    for _ in 0..<count {
        output.append(value)
    }
    return output
}

let repeatCount = 3
let values = repeated("go", count: repeatCount)
let summary = values.joined(separator: ",")

print(summary)
func repeated<T>(_ value: T, count: Int) -> [T] {
    var output: [T] = []
    for _ in 0..<count {
        output.append(value)
    }
    return output
}

let repeatCount = 1
let values = repeated("go", count: repeatCount)
let summary = values.joined(separator: ",")

print(summary)
func repeated<T>(_ value: T, count: Int) -> [T] {
    var output: [T] = []
    for _ in 0..<count {
        output.append(value)
    }
    return output
}

let repeatCount = 5
let values = repeated("go", count: repeatCount)
let summary = values.joined(separator: ",")

print(summary)
  1. repeatCount ← 3

    9let repeatCount→ 3 = 3  //@repeatCount=1, 510let values = repeated("go", count: repeatCount3)11let summary = values.joined(separator: ",")
  2. output ← []

    1func repeated<T>(_ valuego: T, count3: Int) -> [T] {2    var output→ []: [T] = []3    for _ in 0..<count {
  3. output ← ["go"]

    pass 1 of 3
    2var output: [T] = []3for _ in 0..<count3 {4    output→ ["go"].append(valuego)5}
    All 3 passes — pass 1 is the card above
    passoutput
    1[] ["go"]
    2["go"] ["go", "go"]
    3["go", "go"] ["go", "go", "go"]
  4. return output

    5    }6    return output["go", "go", "go"]7}
  5. values ← ["go", "go", "go"], summary ← go,go,go

    9let repeatCount = 3  //@repeatCount=1, 510let values→ ["go", "go", "go"] = repeated("go", count: repeatCount3)11let summary→ go,go,go = values["go", "go", "go"].joined(separator: ",")1213print(summarygo,go,go)
    outputgo,go,go
  1. repeatCount ← 1

    9let repeatCount→ 1 = 110let values = repeated("go", count: repeatCount1)11let summary = values.joined(separator: ",")
  2. output ← []

    1func repeated<T>(_ valuego: T, count1: Int) -> [T] {2    var output→ []: [T] = []3    for _ in 0..<count {
  3. output ← ["go"]

    2var output: [T] = []3for _ in 0..<count1 {4    output→ ["go"].append(valuego)5}
  4. return output

    5    }6    return output["go"]7}
  5. values ← ["go"], summary ← go

    9let repeatCount = 110let values→ ["go"] = repeated("go", count: repeatCount1)11let summary→ go = values["go"].joined(separator: ",")1213print(summarygo)
    outputgo
  1. repeatCount ← 5

    9let repeatCount→ 5 = 510let values = repeated("go", count: repeatCount5)11let summary = values.joined(separator: ",")
  2. output ← []

    1func repeated<T>(_ valuego: T, count5: Int) -> [T] {2    var output→ []: [T] = []3    for _ in 0..<count {
  3. output ← ["go"]

    pass 1 of 5
    2var output: [T] = []3for _ in 0..<count5 {4    output→ ["go"].append(valuego)5}
    All 5 passes — pass 1 is the card above
    passoutput
    1[] ["go"]
    2["go"] ["go", "go"]
    3["go", "go"] ["go", "go", "go"]
    4["go", "go", "go"] ["go", "go", "go", "go"]
    5["go", "go", "go", "go"] ["go", "go", "go", "go", "go"]
  4. return output

    5    }6    return output["go", "go", "go", "go", "go"]7}
  5. values ← ["go", "go", "go", "go", "go"], summary ← go,go,go,go,go

    9let repeatCount = 510let values→ ["go", "go", "go", "go", "go"] = repeated("go", count: repeatCount5)11let summary→ go,go,go,go,go = values["go", "go", "go", "go", "go"].joined(separator: ",")1213print(summarygo,go,go,go,go)
    outputgo,go,go,go,go
generic collection Generic collection helpers describe a relationship between the element type and the returned value.