This file contains 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
// Test for uniqueness of hash values within a custom Hashable type | |
// Closer to 1.0 == fewer key collisions in a set or dictionary | |
extension Set { | |
public var hashQuality: Double { | |
let hashCount = Set<Int>(map({ $0.hashValue })).count | |
return Double(hashCount) / Double(count) | |
} | |
} |
This file contains 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
// Credit to @aligatr for this idea | |
struct ContainmentMatcher<T: Hashable> { | |
let set: Set<T> | |
static func ~=(lhs: ContainmentMatcher<T>, rhs: Set<T>) -> Bool { | |
return rhs.isSuperset(of: lhs.set) | |
} | |
} | |
func containing<T>(_ set: Set<T>) -> ContainmentMatcher<T> { |
This file contains 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
// This makes it possible to use an `Indices` in a collection's subscript. | |
// Since indices are generally their own slice type, this makes using | |
// the subsequence of an indices useful... | |
extension DefaultRandomAccessIndices : RangeExpression { | |
public func relative<C>(to collection: C) -> Range<Element> | |
where C : _Indexable, Element == C.Index | |
{ | |
if isEmpty { | |
return collection.startIndex ..< collection.startIndex | |
} |
This file contains 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
func tellJoke(name: String, character: Character) { | |
let punchline = String(name.filter { $0 != character }) | |
let n = name.count - punchline.count | |
let joke = """ | |
Q: Why does \(name) have \(n) \(character)'s in their name? | |
A: I don't know, why does \(name) have \(n) \(character)'s in their name? | |
Q: Because otherwise they'd be called \(punchline). | |
""" | |
print(joke) |
This file contains 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] | |
} | |
} |
This file contains 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 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 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 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 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] |