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
infix operator ?= { | |
associativity right | |
precedence 90 | |
assignment | |
} | |
func ?=<T>(inout variable: T?, expr: @autoclosure () -> T) { | |
if variable == nil { | |
variable = expr() | |
} |
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
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 } | |
} |
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
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 |
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
import Foundation | |
import CoreData | |
extension NSManagedObject { | |
class var entityName: String { | |
return NSStringFromClass(self).componentsSeparatedByString(".").last! | |
} | |
convenience init(context: NSManagedObjectContext) { |
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 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) |
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
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) } |
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 SequenceType { | |
func first(@noescape predicate: Generator.Element -> Bool) -> Generator.Element? { | |
for element in self { | |
if predicate(element) { | |
return element | |
} | |
} | |
return nil | |
} | |
} |
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
#!/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)") |
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
import UIKit | |
import CoreSpotlight | |
import MobileCoreServices | |
public typealias Completion = NSError? -> Void | |
// MARK: - Searchable Protocol | |
public protocol Searchable { | |
static var spotlightDomainIdentifier: String { get } |
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 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) |