Skip to content

Instantly share code, notes, and snippets.

@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 / 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) {
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 / Permutations.swift
Created September 27, 2015 16:36
Swift Array extension to create interleaved arrays and permutations.
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
import Foundation
extension Array where Element : IntegerType {
var sum: Element {
return reduce(0, combine: +)
}
var odds: [Element] {
return filter { $0 % 2 != 0 }
}
@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)
@daehn
daehn / SystemLog.swift
Created February 24, 2016 19:10 — forked from kristopherjohnson/SystemLog.swift
Demo of using the Apple System Log (ASL) API from Swift
// 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]
@daehn
daehn / TypedKVOClosure.swift
Last active July 26, 2016 22:43
Helper class to provide a typed, closure based KVO interface in Swift
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))"
}
}