Generics
Generic Collections
Arrays are generic collections, so helper functions can preserve the element type they receive.
Work with element type
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)
repeatCount ← 3
9let repeatCount→ 3 = 3 //@repeatCount=1, 510let values = repeated("go", count: repeatCount3)11let summary = values.joined(separator: ",")output ← []
1func repeated<T>(_ valuego: T, count3: Int) -> [T] {2 var output→ []: [T] = []3 for _ in 0..<count {output ← ["go"]
pass 1 of 32var output: [T] = []3for _ in 0..<count3 {4 output→ ["go"].append(valuego)5}All 3 passes — pass 1 is the card above pass output1 [] → ["go"] 2 ["go"] → ["go", "go"] 3 ["go", "go"] → ["go", "go", "go"] return output
5 }6 return output["go", "go", "go"]7}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
repeatCount ← 1
9let repeatCount→ 1 = 110let values = repeated("go", count: repeatCount1)11let summary = values.joined(separator: ",")output ← []
1func repeated<T>(_ valuego: T, count1: Int) -> [T] {2 var output→ []: [T] = []3 for _ in 0..<count {output ← ["go"]
2var output: [T] = []3for _ in 0..<count1 {4 output→ ["go"].append(valuego)5}return output
5 }6 return output["go"]7}values ← ["go"], summary ← go
9let repeatCount = 110let values→ ["go"] = repeated("go", count: repeatCount1)11let summary→ go = values["go"].joined(separator: ",")1213print(summarygo)outputgo
repeatCount ← 5
9let repeatCount→ 5 = 510let values = repeated("go", count: repeatCount5)11let summary = values.joined(separator: ",")output ← []
1func repeated<T>(_ valuego: T, count5: Int) -> [T] {2 var output→ []: [T] = []3 for _ in 0..<count {output ← ["go"]
pass 1 of 52var output: [T] = []3for _ in 0..<count5 {4 output→ ["go"].append(valuego)5}All 5 passes — pass 1 is the card above pass output1 [] → ["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"] return output
5 }6 return output["go", "go", "go", "go", "go"]7}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.