This proposal comprises a variety of commonly (and less commonly) suggested improvements to the standard library's Dictionary type, from merging initializers to dictionary-specific filter and mapValues methods. The proposed additions to Dictionary, and the corresponding changes to Set, are detailed in the sections below.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extension Dictionary { | |
| var storageID: UInt { | |
| var copy = self | |
| return withUnsafeBytes(of: ©) { | |
| $0.baseAddress!.load(as: UInt.self) | |
| } | |
| } | |
| } | |
| var a = [1: 1, 2: 2] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extension Sequence { | |
| /// Returns an array with the transformed elements of the sequence if all | |
| /// the transformations succeed. | |
| func flatMapAll<T>(_ transform: (Element) -> T?) -> [T]? { | |
| var result: [T] = [] | |
| result.reserveCapacity(underestimatedCount) | |
| for element in self { | |
| if let transformed = transform(element) { | |
| result.append(transformed) | |
| } else { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extension Collection { | |
| func sliced( | |
| where predicate: (Element, Element) -> Bool | |
| ) -> [SubSequence] { | |
| var result: [SubSequence] = [] | |
| var start = startIndex | |
| for (i, j) in zip(indices, indices.dropFirst()) { | |
| if predicate(self[i], self[j]) { | |
| result.append(self[start..<j]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| prefix func !<T>(predicate: @escaping (T) -> Bool) -> (T) -> Bool { | |
| return { !predicate($0) } | |
| } | |
| func isEven(_ x: Int) -> Bool { | |
| return x % 2 == 0 | |
| } | |
| let nums = [5, 7, 3, 4, 2, 3, 4] | |
| nums.first(where: isEven) // 4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Foundation | |
| extension Decimal.RoundingMode { | |
| init(_ rule: FloatingPointRoundingRule, for value: Decimal) { | |
| switch rule { | |
| case .down: self = .down | |
| case .up: self = .up | |
| case .awayFromZero: self = value < 0 ? .down : .up | |
| case .towardZero: self = value < 0 ? .up : .down |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// A sequence of weakly held elements. | |
| /// | |
| /// Iterating over this sequence yields the elements that still exist at the | |
| /// moment of iteration. Subsequent iterations may not yield the same elements, | |
| /// as some elements may have been removed from memory. | |
| public struct WeakSequence<Element: AnyObject> : Sequence { | |
| var _contents: [Box] = [] | |
| /// A box for weakly holding a class instance. | |
| class Box { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Additions to make the unsafe pointer APIs more consistent | |
| extension UnsafePointer { | |
| func deallocate(capacity: Int) { | |
| UnsafeMutablePointer(mutating: self).deallocate(capacity: capacity) | |
| } | |
| } | |
| extension UnsafeBufferPointer { | |
| func deallocate() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Foundation | |
| public class CoRoutine<T> { | |
| private var index = 0 | |
| private var sequence : [T] | |
| private var routine : (T)->() | |
| private let step : Int? | |
| private let deltaTime : TimeInterval? | |
| private(set) public var isCanceled : Bool = false | |
| private(set) public var isDone : Bool = false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Darwin | |
| import CoreFoundation | |
| // A variable we can use with exit() to ensure that the optimizer | |
| // doesn't remove code we want to time | |
| var undead = 0 | |
| // A filtered collection that can be more efficiently counted than the stock one. | |
| struct LazyFilterBidirectionalCollection2<Base : BidirectionalCollection> : Collection { | |
| typealias Impl = LazyFilterBidirectionalCollection<Base> |