Skip to content

Instantly share code, notes, and snippets.

View dabrahams's full-sized avatar
🤩
STLabbin’

Dave Abrahams dabrahams

🤩
STLabbin’
View GitHub Profile
@dabrahams
dabrahams / UnsafeReference.swift
Created May 15, 2020 13:32
Rescued very old proposal to redesign Unmanaged
#if false
Changes to Unmanaged
-public struct Unmanaged<Instance : AnyObject> {
// New API: `UnsafeReference(bitPattern:)`.
- public static func fromOpaque(value: COpaquePointer) -> Unmanaged
// New API: `OpaquePointer(bitPattern:)`.
- public func toOpaque() -> COpaquePointer
@dabrahams
dabrahams / NominalElementDictionary.swift
Created May 15, 2020 01:59
Plain Swift Dictionary uses a tuple as its Element type, which prevents the Element from conforming to anything
/// A Dictionary with a nominal `Element` type, that can conform to things.
@frozen public struct Dictionary2<Key: Hashable, Value> {
public typealias Base = [Key : Value]
/// A view of a dictionary's keys.
public typealias Keys = Base.Keys
/// A view of a dictionary's values.
public typealias Values = Base.Values
// (A) Definition
protocol P {
static var isEquatable: Bool { get }
}
// (B) Default implementation
extension P {
static var isEquatable: Bool { false }
}
@dabrahams
dabrahams / ASimplerExample.swift
Last active June 18, 2020 14:58
(https://bugs.swift.org/browse/SR-12692) Demonstrates that the current model for protocol extensions and conditional conformances is… unreasonable.
extension Sequence {
var array: Array<Element> {
print("preallocating", self.underestimatedCount)
return Array(self)
}
}
_ = Array(0..<1000).reversed().array // preallocating 1000
_ = repeatElement(1..<10, count: 200).joined().array // preallocating 0
@dabrahams
dabrahams / Dispatch.swift
Created April 25, 2020 13:37
Post-hoc specialized behavior based on refinement
/// A repository of functionality depending on the conformances of `Model`.
///
/// Conditional conformances provide implementation functions that take a
/// generic argument type with the safe assumption that the argument's concrete
/// type is `Model`.
struct Dispatch<Model> {
/// Returns `f(a as! Model) as! R1`
///
/// Used by implementation functions to avoid the clutter of casting
/// explicitly.
@dabrahams
dabrahams / IsNonExistentialTest.swift
Created April 23, 2020 04:59
Test for conformance to non-existential protocol
protocol True {}
struct IsBidirectional<C> {}
extension IsBidirectional: True where C : BidirectionalCollection { }
extension Collection {
var isBidirectional: Bool {
return IsBidirectional<Self>() is True
}
}
func testIsBidirectional() {
/// A simple pseudo-random number generator.
struct LinearCongruential: RandomNumberGenerator {
/// The last value returned by `self.next`, or what `self` was seeded with.
private var lastValue: UInt64
/// Creates an instance with the given seed.
///
/// Instances created with the same seed produce the same sequence of
/// pseuedo-random results from `next()`.
init(seed: UInt64 = 0) {
@dabrahams
dabrahams / DivideAndConquerHack.swift
Last active April 6, 2020 04:58
Attempt to get simple divide and conquer algorithms to avoid reallocation.
// Please see https://github.com/dabrahams/DivideAndConquer where this has moved.
@dabrahams
dabrahams / ClosureFusion.swift
Last active March 23, 2020 16:37
Demonstrates successful optimization across composition of type-erased calculations using functions.
struct Tensor<Scalar> {
let count: Int
let indexer: (Int) -> Scalar
init<Base: RandomAccessCollection>(_ base: Base)
where Base.Element == Scalar
{
count = base.count
indexer = { base[base.index(base.startIndex, offsetBy: $0)] }
}
@dabrahams
dabrahams / BoxMinimization.swift
Created March 17, 2020 16:02
Unsuccessful attempt to create optimization in the presence of type erasure by reconstructing type info.
protocol TensorElement {
static func box<T: LazyTensor>(_:T) -> TensorBoxBase<Self>
where T.Scalar == Self
}
extension TensorElement {
static func box<T: LazyTensor>(_ x:T) -> TensorBoxBase<Self>
where T.Scalar == Self
{
TensorBox(x)