Skip to content

Instantly share code, notes, and snippets.

// 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)
}
}
// 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 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
}
@natecook1000
natecook1000 / joke.swift
Last active October 22, 2017 16:00 — forked from airspeedswift/joke.swift
Multiline Literals
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)
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]
}
}
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, _)):
@natecook1000
natecook1000 / open_current.script
Created October 8, 2017 20:39
Open new Terminal tab with frontmost Finder window
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
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
@natecook1000
natecook1000 / ContainmentSet.swift
Last active November 12, 2018 12:28
ContainmentSet & PredicateSet
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
}
extension Dictionary {
var storageID: UInt {
var copy = self
return withUnsafeBytes(of: &copy) {
$0.baseAddress!.load(as: UInt.self)
}
}
}
var a = [1: 1, 2: 2]