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 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
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
extension Array { | |
var decompose: (head: Element, tail: [Element])? { | |
return count > 0 ? (self[0], Array(self[1..<count])) : nil | |
} | |
} | |
extension Array { | |
func interleave(x: Element) -> [[Element]] { | |
guard let (head, tail) = decompose else { return [[x]] } | |
let start = [x] + 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
import Foundation | |
extension Array where Element : IntegerType { | |
var sum: Element { | |
return reduce(0, combine: +) | |
} | |
var odds: [Element] { | |
return filter { $0 % 2 != 0 } | |
} |
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) |
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
// Note: This must be used in an Xcode project that contains a bridging header | |
// that includes <asl.h> | |
import Foundation | |
/// Provides high-level methods to access the raw data in | |
/// an ASL message. | |
struct SystemLogEntry { | |
/// Key-value pairs read from ASL message | |
let data: [String : String] |
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 | |
struct Change<T>: CustomDebugStringConvertible { | |
let oldValue, newValue: T? | |
var debugDescription: String { | |
let prettyString: T? -> String = { return $0 != nil ? "\($0!)" : ".None" } | |
return "ChangeType:\n\tOld value: \(prettyString(oldValue))\n\tNew value: \(prettyString(newValue))" | |
} | |
} |