Author: Chris Lattner
import Prelude | |
public struct Lens<A, B> { | |
private let get: A -> B | |
private let set: (A, B) -> A | |
public init(get: A -> B, set: (A, B) -> A) { | |
self.get = get | |
self.set = set | |
} |
public struct AnyEquatable: Equatable { | |
private let value: Any | |
private let equals: Any -> Bool | |
public init<E: Equatable>(_ value: E) { | |
self.value = value | |
self.equals = { ($0 as? E == value) ?? false } | |
} | |
} |
import UIKit | |
class ChatCollectionViewFlowLayout: UICollectionViewFlowLayout { | |
private var topMostVisibleItem = Int.max | |
private var bottomMostVisibleItem = -Int.max | |
private var offset: CGFloat = 0.0 | |
private var visibleAttributes: [UICollectionViewLayoutAttributes]? |
import Foundation | |
protocol Channel: IteratorProtocol { | |
func send(_ value: Element?) | |
} | |
/// A blocking channel for sending values. | |
/// | |
/// `send` and `receive` must run in separate separate execution contexts, otherwise you get a deadlock. | |
final class BlockingChannel<A>: Channel { |
The libdispatch is one of the most misused API due to the way it was presented to us when it was introduced and for many years after that, and due to the confusing documentation and API. This page is a compilation of important things to know if you're going to use this library. Many references are available at the end of this document pointing to comments from Apple's very own libdispatch maintainer (Pierre Habouzit).
My take-aways are:
-
You should create very few, long-lived, well-defined queues. These queues should be seen as execution contexts in your program (gui, background work, ...) that benefit from executing in parallel. An important thing to note is that if these queues are all active at once, you will get as many threads running. In most apps, you probably do not need to create more than 3 or 4 queues.
-
Go serial first, and as you find performance bottle necks, measure why, and if concurrency helps, apply with care, always validating under system pressure. Reuse
import SwiftUI | |
import PlaygroundSupport | |
extension Double { | |
func toRadians() -> Double { | |
return self * Double.pi / 180 | |
} | |
func toCGFloat() -> CGFloat { | |
return CGFloat(self) |