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
| 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
| 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
| 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 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
| protocol ContainmentSet { | |
| associatedtype SetElement | |
| func contains(_ element: SetElement) -> Bool | |
| func intersection(_: Self) -> Self | |
| func union(_: Self) -> Self | |
| func subtracting(_: Self) -> Self | |
| func symmetricDifference(_: Self) -> Self | |
| } |
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
| struct LazySplitSequence<Base: Sequence> : Sequence, LazySequenceProtocol { | |
| struct Iterator : IteratorProtocol { | |
| mutating func next() -> [Base.Element]? { | |
| var result: [Base.Element] = [] | |
| if splits == 0 { | |
| while let element = iterator.next() { | |
| result.append(element) | |
| } | |
| return result.isEmpty ? nil : result |
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
| on run | |
| tell application "Finder" to set myDir to POSIX path of (insertion location as alias) | |
| tell application "Terminal" | |
| activate | |
| if not (exists window 1) then | |
| reopen | |
| else | |
| tell application "System Events" to keystroke "t" using command down | |
| end if | |
| do script "cd " & quoted form of myDir in window 1 |
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
| struct Zip2Collection<C1: Collection, C2: Collection> : Collection { | |
| enum Index: Comparable { | |
| case index(C1.Index, C2.Index) | |
| case end | |
| static func <(lhs: Index, rhs: Index) -> Bool { | |
| switch (lhs, rhs) { | |
| case (.end, _): return false | |
| case (_, .end): return true | |
| case let (.index(l, _), .index(r, _)): |
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 nth(_ n: Int) -> Element? { | |
| assert(n >= 0, "Can't get a negative-th element") | |
| guard let i = index(startIndex, offsetBy: numericCast(n), limitedBy: endIndex), | |
| i != endIndex | |
| else { return nil } | |
| return self[i] | |
| } | |
| } |