Skip to content

Instantly share code, notes, and snippets.

Avatar

Silvan Dähn daehn

View GitHub Profile
@daehn
daehn / SystemLog.swift
Created Feb 24, 2016 — forked from kristopherjohnson/SystemLog.swift
Demo of using the Apple System Log (ASL) API from Swift
View SystemLog.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 / script.swift
Last active Sep 18, 2015 — forked from zats/script.swift
Update all your plugins for the latest Xcode beta with a single
View script.swift
#!/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 / optional-assignment.swift
Last active Aug 29, 2015 — 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.
View optional-assignment.swift
infix operator ?= {
associativity right
precedence 90
assignment
}
func ?=<T>(inout variable: T?, expr: @autoclosure () -> T) {
if variable == nil {
variable = expr()
}
View matrix.swift
public struct MatrixIndex: BidirectionalIndexType {
public let x, y : Int
private let columns: Int
public func successor() -> MatrixIndex {
return (x + 1 == columns) ?
MatrixIndex(x: 0, y: y + 1, columns: columns) :
MatrixIndex(x: x + 1, y: y, columns: columns)
View PrettyAnyAll.Swift
public extension IteratorProtocol {
mutating public func any(_ predicate: (Element) throws -> Bool) rethrows -> Bool {
guard let current = next() else { return false }
return try predicate(current) || any(predicate)
}
mutating public func all(_ predicate: (Element) throws -> Bool) rethrows -> Bool {
guard let current = next() else { return true }
return try predicate(current) && all(predicate)