Skip to content

Instantly share code, notes, and snippets.

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)
@daehn
daehn / matrix.swift
Last active August 29, 2015 14:26 — forked from oisdk/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)
@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 / 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 / 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]