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 Sequence where Element: Hashable { | |
func frequencies() -> [Element : Int] { | |
return Dictionary(map { ($0, 1) }, uniquingKeysWith: { $0 + $1 }) | |
} | |
} | |
assert("hello".frequencies() == ["e": 1, "o": 1, "l": 2, "h": 1]) |
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
final class ReadOnly<T> { | |
private let value: T | |
init(_ value: T) { | |
self.value = value | |
} | |
subscript<U>(keyPath: KeyPath<T, U>) -> U { | |
return self.value[keyPath: keyPath] | |
} |
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 CoreLocation | |
extension CLLocation { | |
public static func +(lhs: CLLocation, rhs: CLLocation) -> CLLocation { | |
let summedLat = lhs.coordinate.latitude + rhs.coordinate.latitude | |
let summedLong = lhs.coordinate.longitude + rhs.coordinate.longitude | |
return CLLocation(latitude: summedLat, longitude: summedLong) | |
} | |
public static func /(lhs: CLLocation, rhs: Double) -> CLLocation { |
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 AVFoundation | |
/// The `PlayerLoader` class provides a callback mechanism for consumers of `AVPlayer` when loading a resource. | |
/// AVFoundation works by an inconvenient series of APIs that rely on KVO for asynchronous operations. This class | |
/// eliminates the boilerplate that KVO imposes and makes the callsite much more clean. | |
public final class PlayerLoader: NSObject { | |
public typealias Callback = (AVPlayer) -> () | |
/// The callback will initialize to a non-nil value. The callback is set to nil once it's invoked, at which time the | |
/// KVO observation is removed. This is necessary to avoid double-removing the KVO observation. |
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 AVFoundation | |
import Foundation | |
extension FileManager { | |
func removeFileIfNecessary(at url: URL) throws { | |
guard fileExists(atPath: url.path) else { | |
return | |
} | |
do { |
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 | |
/// `Disposable` encapsulates the steps necessary to tear down some unit of work, typically an asynchronous operation | |
/// like a network task. | |
public protocol Disposable { | |
/// Disposes the object. This may be invoked multiple times, but only performs its side-effects once. | |
func dispose() | |
} | |
extension URLSessionTask: Disposable { |
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 Collection where Iterator.Element: Equatable { | |
/// Computes the indices of different elements between two collections. This is useful for updating table views | |
/// with row deletions and insertion | |
/// - Parameter other: the collection to compute the index difference | |
/// - Returns: a tuple with indices to delete and insert on `self` to create a collection equivalent to `other` | |
/// - Discussion: This algorithm does not work for collections with duplicate elements. | |
public func indexDifference(with other: Self) -> (deleted: [Index], inserted: [Index]) { | |
var (deleted, inserted): ([Index], [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
extension RawRepresentable where RawValue: Numeric { | |
/// Returns all the values of a RawRepresentable whose RawValue is Numeric. This static property expressly exists | |
/// for enumerations, though can be used elsewhere. Works only for contiguous values. | |
/// Supposing we have: | |
/// ``` | |
/// enum Counter { | |
/// case one, two | |
/// } | |
/// ``` | |
/// then `Counter.allValues == [.one, .two]`. |
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 URL { | |
static var WXYCStream: URL { | |
return URL(string: "http://audio-mp3.ibiblio.org:8000/wxyc.mp3")! | |
} | |
} | |
class DataStreamer: NSObject, URLSessionDataDelegate { | |
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) { | |
dump(data) | |
} |
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 | |
public extension Numeric { | |
static func arc4random() -> Self { | |
var r: Self = 0 | |
arc4random_buf(&r, Int(MemoryLayout<Self>.size)) | |
return r | |
} | |
} |