Skip to content

Instantly share code, notes, and snippets.

@daehn
daehn / optional-assignment.swift
Last active August 29, 2015 14:27 — forked from radex/optional-assignment.swift
Optional assignment operator implementation. `foo ?= bar` will evaluate and assign bar to foo if and only if foo is nil. Equivalent to `||=` in Ruby and other languages.
infix operator ?= {
associativity right
precedence 90
assignment
}
func ?=<T>(inout variable: T?, expr: @autoclosure () -> T) {
if variable == nil {
variable = expr()
}
@daehn
daehn / Subset.swift
Last active August 29, 2015 14:27
Finds the largest ascending subset and their indices contained in an Array<Int>
import Foundation
typealias IndexedSlice = (slice: ArraySlice<Int>, index: Int)
let ascending: (Int, Int) -> Bool = { $0 < $1 }
func solution(input: [Int]) -> [Int] {
assert(input.count > 0, "Cannot calculate for empty array")
return largestAscendingSlicesWithIndex(input).map { $0.index }
}
@daehn
daehn / FibonacciGenerator.swift
Last active August 25, 2015 22:19
Swift Generator that produces Fibonacci numbers
import UIKit
struct FibonacciGenerator: GeneratorType, SequenceType {
var (f1, f2) = (1, 1)
mutating func next() -> Int? {
let result = f1
(f1, f2) = (f2, f1 + f2)
return result
@daehn
daehn / CDHelper.swift
Last active August 31, 2015 15:20
Small NSManagedObject extension to simplify fetching and creating objects.
import Foundation
import CoreData
extension NSManagedObject {
class var entityName: String {
return NSStringFromClass(self).componentsSeparatedByString(".").last!
}
convenience init(context: NSManagedObjectContext) {
@daehn
daehn / Levenshtein.swift
Last active August 28, 2017 02:19
Calculate the Levenshtein-Distance between two Strings in Swift
extension String {
subscript(index: Int) -> Character {
return self[startIndex.advancedBy(index)]
}
subscript(range: Range<Int>) -> String {
let start = startIndex.advancedBy(range.startIndex)
let end = startIndex.advancedBy(range.endIndex)
enum Beat {
case NoBeat, BPM(Int), Other(Int)
var value : Int? {
return Mirror(reflecting: self).children.first?.value as? Int
}
}
let enums: [Beat] = [.NoBeat, .BPM(105), .Other(120)]
enums.forEach { print($0.value) }
extension SequenceType {
func first(@noescape predicate: Generator.Element -> Bool) -> Generator.Element? {
for element in self {
if predicate(element) {
return element
}
}
return nil
}
}
@daehn
daehn / script.swift
Last active September 18, 2015 17:41 — forked from zats/script.swift
Update all your plugins for the latest Xcode beta with a single
#!/usr/bin/env xcrun swift
// $ chmod +x script.swift
// $ ./script.swift
// or $ ./script.swift -xcode=/Applications/Xcode-beta.app
import Foundation
@noreturn private func failWithError(message: String) {
print("🚫 \(message)")
@daehn
daehn / Searchable.swift
Last active April 23, 2019 03:05
Swift protocols and protocol extensions to easily add and delete conforming instances to Spotlight. Blog post can be found here: http://silvandaehn.com/2015/09/25/Simplifying-CoreSpotlight/
import UIKit
import CoreSpotlight
import MobileCoreServices
public typealias Completion = NSError? -> Void
// MARK: - Searchable Protocol
public protocol Searchable {
static var spotlightDomainIdentifier: String { get }
@daehn
daehn / CountElement.Swift
Last active January 30, 2016 19:50
Extension on SequenceType to count the occurrence of an element.
extension SequenceType where Generator.Element : Equatable {
func count(other: Generator.Element) -> Int {
var sum = 0
for case other in self { sum++ }
return sum
}
}
func ~=<T>(pattern: T -> Bool, value: T) -> Bool {
return pattern(value)