Skip to content

Instantly share code, notes, and snippets.

View danielctull's full-sized avatar

Daniel Tull danielctull

View GitHub Profile
import Combine
extension URLSession {
/// Returns a publisher that wraps a URL session download task for a given
/// URL.
///
/// - Parameter url: The URL for which to create a download task.
/// - Returns: A publisher that wraps a download task for the URL.
/// A sequence that presents the elements of the base sequence with the
/// separator inserted between each element.
public struct InterspersedSequence<Base: Sequence> {
let base: Base
let separator: Base.Element
/// Creates a sequence that presents the elements of `base` sequence with the
/// `separator` inserted between each element.
@danielctull
danielctull / EncodingMadness.swift
Created September 9, 2018 20:40
Why have one encoding type when you can have three??
extension String.Encoding {
init?(charset: String) {
let cfencoding = CFStringConvertIANACharSetNameToEncoding(charset as CFString)
guard cfencoding != kCFStringEncodingInvalidId else {
return nil
}
@danielctull
danielctull / NSImageName+Amazing.swift
Last active June 6, 2018 16:18
The following would error on 10.13, because NSImage.Name was a struct so the static property defined here was not available on String, with line 10 causing an error. With the macOS 10.14 SDK, NSImage.Name is a typealias to String, so additions to NSImage.Name via extensions are now actually extensions on String. 🤔
import Cocoa
extension NSImage.Name {
static var amazing: NSImage.Name {
return NSImage.Name("AmazingImage!")
}
}
let imageName = String.amazing
@danielctull
danielctull / Primes.swift
Last active May 24, 2018 10:55
Function to find the nth prime.
extension Int {
func isDivisible(by value: Int) -> Bool {
return self % value == 0
}
}
extension Array where Element == Int {
import Foundation
import Accelerate
struct Float16 {
fileprivate var value: UInt16
init(_ float: Float) {
@danielctull
danielctull / UICollectionViewFlowLayout+DelegateAccess.swift
Last active May 16, 2019 14:00
Wraps the six UICollectionViewFlowLayout delegate methods and their equivalent properties using functional programming techniques, so that values are easier to retrieve. Full details at: http://danieltull.co.uk/blog/2018/04/13/simplifying-uicollectionviewflowlayout-delegate-method-usage-with-functional-programming/
extension UICollectionViewFlowLayout {
typealias DelegateMethod<Key, Value> = ((UICollectionView, UICollectionViewLayout, Key) -> Value)
private var delegate: UICollectionViewDelegateFlowLayout? {
return collectionView?.delegate as? UICollectionViewDelegateFlowLayout
}
func retrieve<Key, Value>(
@danielctull
danielctull / NSCopying.swift
Last active April 10, 2018 14:31
NSCopying's copy() returns Any by default, which is terribly annoying. Here I force it to be of the same type as Self.
import Foundation
extension NSCopying {
func copy() -> Self {
// Use copy(with zone:) here to disambiguate between this
// overload and the one we actually want to call.
let c = copy(with: nil)
extension Array {
/// Wrapping types for the map function.
enum Wrapping {
/// Doesn't wrap at all.
///
/// When used the resulting array will be one
/// element shorter than the input array.
case none
@danielctull
danielctull / Coalescer.swift
Last active March 22, 2018 11:52
A small class to coalesce multiple calls to trigger a block once.
import Foundation
/// This manages coalescing multiple calls per frame into a
/// single execution of the given block.
final class Coalescer {
private let notificationCenter: NotificationCenter
private let notificationQueue: NotificationQueue
private let notificationName = Notification.Name(rawValue: "CoalescingNotification")