Skip to content

Instantly share code, notes, and snippets.

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])
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]
}
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 {
@jakebromberg
jakebromberg / PlayerLoader.swift
Created October 3, 2017 03:38
Takes out some of the pain from loading an AVPlayer object.
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.
@jakebromberg
jakebromberg / AVAssetTrim.swift
Last active October 4, 2023 23:57 — forked from acj/TrimVideo.swift
Trim video using AVFoundation in Swift
import AVFoundation
import Foundation
extension FileManager {
func removeFileIfNecessary(at url: URL) throws {
guard fileExists(atPath: url.path) else {
return
}
do {
@jakebromberg
jakebromberg / Disposable.swift
Created October 14, 2017 20:47
Encapsulates the steps necessary to tear down some unit of work, typically an asynchronous operation like a network task.
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 {
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]) = ([], [])
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]`.
@jakebromberg
jakebromberg / DataStreamer.swift
Created November 19, 2017 17:24
`DataStreamer` class is a `URLSessionDataTask` delegate that logs out incoming data
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)
}
@jakebromberg
jakebromberg / Numeric+arc4random.swift
Last active November 25, 2017 22:21
Swifty wrapper around arc4random
import Foundation
public extension Numeric {
static func arc4random() -> Self {
var r: Self = 0
arc4random_buf(&r, Int(MemoryLayout<Self>.size))
return r
}
}