-
Meet the Swift Algorithms and Collections packages
Discover two of the latest additions to the list of open-source Swift packages from Apple: Swift Algorithms and Swift Collections. Not only can you use these packages immediately, they also incubate new algorithms and data structures for eventual inclusion in the Swift Standard Library. We'll show you how you can integrate these packages into your projects and select the right algorithms and data structures to make your code clearer and faster.
Ressources
Vidéos connexes
WWDC22
WWDC21
WWDC20
-
Rechercher dans cette vidéo…
-
-
1:00 - The map algorithm
// Raw loop: var selectedMessages: [Message] = [] for indexPath in indexPathsForSelectedRows { selectedMessages.append(messages[indexPath.row]) } // Using `map` makes this clearer and faster. indexPathsForSelectedRows.map { messages[$0.row] } -
1:36 - The compactMap algorithm
// Raw loop: var attachments: [Attachment] = [] for message in messages { if let attachment = message.attachment { attachments.append(attachment) } } // The above is just a `map` and a `filter`. messages .filter { $0.attachment != nil } .map { $0.attachment! } // This pattern is so common we have a special name and algorithm for it. messages.compactMap { $0.attachment } -
2:06 - The flatMap algorithm
extension Message { func makeMessageParts() -> [TranscriptItem] } messages // [Message] .map { $0.makeMessageParts() } // [[TranscriptItem]] .joined() // [TranscriptItem] // This pattern is so common that we have another special kind of map for it. messages // [Message] .flatMap { $0.makeMessageParts() } // [TranscriptItem] -
3:00 - Chaining together algorithms
// Raw loop: var photos: [PhotoItem] = [] for item in transcript.reversed() { if let photo = item as? PhotoItem { photos.append(photo) if photos.count == 6 { break } } } // The above can be expressed more concisely by chaining together algorithms. transcript .reversed() // [TranscriptItem] .compactMap { $0 as? PhotoItem } // [PhotoItem] .prefix(6) // [PhotoItem] // This gives us more flexibility to express this code more clearly. transcript .compactMap { $0 as? PhotoItem } // [PhotoItem] .suffix(6) // [PhotoItem] .reversed() // [PhotoItem] -
4:19 - Lazy adapters
extension Message { func makeMessageParts() -> [TranscriptItem] } messages .map { $0.makeMessageParts() } // [[TranscriptItem]] .joined() // FlattenSequence<[[TranscriptItem]]> -
4:58 - Lazy algorithm chains
transcript .lazy // LazySequence<[TranscriptItem]> .compactMap { $0 as? PhotoItem } // LazyCompactMap<[TranscriptItem], PhotoItem> .suffix(6) // LazyCompactMap<ArraySlice<TranscriptItem>, PhotoItem> .reversed() // ReversedCollection<LazyCompactMap<ArraySlice<TranscriptItem>, PhotoItem>> -
5:48 - Wrapping a lazy algorithm chain in an Array initializer
Array( transcript .lazy .compactMap { $0 as? PhotoItem } .suffix(6) .reversed() ) -
7:13 - windows(ofCount:)
import Algorithms let x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] for window in x.windows(ofCount: 3) { print(window) } // Prints [0, 1, 2] // Prints [1, 2, 3] // Prints [2, 3, 4] // Prints [3, 4, 5] -
7:30 - adjacentPairs()
import Algorithms let x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] for (prev, next) in x.adjacentPairs() { print((prev, next)) } // Prints (0, 1) // Prints (1, 2) // Prints (2, 3) // Prints (3, 4) -
7:45 - chunks(ofCount:)
import Algorithms let x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] for chunk in x.chunks(ofCount: 3) { print(chunk) } // Prints [0, 1, 2] // Prints [3, 4, 5] // Prints [6, 7, 8] // Prints [9] -
8:08 - chunked(on:)
import Algorithms let x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] for (isPrime, chunk) in x.chunked(on: \.isPrime) { print((isPrime, chunk)) } // Prints (false, [0, 1]) // Prints (true, [2, 3]) // Prints (false, [4]) // Prints (true, [5]) -
8:33 - Recognizing the chunked(on:) pattern
// Raw loop: var prev: Element? for element in collection { if prev?.value != element.value { // do work } prev = element } // The above is just `chunked(on:)`. for (value, chunk) in collection.chunked(on: \.value) { // do work } -
8:49 - Mapping, chunking, and joining
import Algorithms extension Message { func makeMessageParts() -> [TranscriptItem] } transcript = Array( messages .lazy .flatMap { $0.makeMessageParts() } .chunked { $1.date.timeIntervalSince($0.date) < 60 * 60 } .joined { DateItem(date: $1.first!.date) } ) -
14:56 - Double-ended queues
var queue: Deque = ["A", "B", "C"] queue.append("D") queue.append("E") queue.removeFirst() // "A" queue.removeFirst() // "B" queue.prepend("F") queue.prepend("G") queue.removeLast() // "E" queue.removeLast() // "D" -
15:46 - Deque protocol conformances
var items: Deque = ["D", "E", "f"] print(items[1]) // "E" items[2] = "F" items.insert(contentsOf: ["A", "B", "C"], at: 0) print(items[1]) // "B" -
17:31 - Accessing elements is still efficient
var items: Deque = ["D", "E", "F"] print(items[1]) // "E" items.insert(contentsOf: ["A", "B", "C"], at: 0) print(items[1]) // "B" -
18:39 - Removing elements at random is twice as fast on average
var items: Deque = ["A", "B", "C", "D", "E", "F"] items.removeSubrange(1 ..< 3) -
19:33 - Unordered sets
let first: Set = ["A", "B", "C", "D", "E", "F"] print(first) // ["B", "E", "C", "F", "D", "A"] let second: Set = ["A", "B", "C", "D", "E", "F"] print(second) // ["A", "D", "E", "F", "C", "B"] print(first == second) // true -
20:26 - Ordered sets
let first: OrderedSet = ["A", "B", "C", "D", "E", "F"] print(first) // ["A", "B", "C", "D", "E", "F"] let second: OrderedSet = ["F", "E", "D", "C", "B", "A"] print(first == second) // false print(first.unordered == second.unordered) // true -
21:04 - Ordered sets resemble how arrays work
var items: OrderedSet = ["E", "D", "C", "B", "A"] items[3] // "B" items.append("F") // (inserted: true, index: 5) items.insert("B", at: 1) // (inserted: false, index: 3) items.remove("E") items.sort() items.shuffle() -
22:32 - Ordered sets implement high-level set operations
var items: OrderedSet = ["B", "D", "E"] items.formUnion(["A", "B", "C", "F"]) items.subtract(["A", "B", "G"]) let other: OrderedSet = ["C", "D", "E", "F"] print(items == other) // false print(items.unordered == other.unordered) // true -
26:46 - Ordered dictionaries
var dict: OrderedDictionary = [2: "two", 1: "one", 0: "zero"] print(dict[1]) // Optional("one") print(dict) // [2: "two", 1: "one", 0: "zero"] dict[3] = "three" dict[1] = nil print(dict) // [2: "two", 0: "zero", 3: "three"] -
27:38 - Subscripting always means the keying subscript
var dict: OrderedDictionary = [2: "two", 0: "zero", 3: "three"] print(dict[0]) // Optional("zero") print(dict.elements[0]) // (key: 2, value: "two")
-