Skip to content

Instantly share code, notes, and snippets.


Dave Abrahams dabrahams

View GitHub Profile
dabrahams / CoW.swift
Last active Apr 5, 2021
Copy-on-write big-O efficiency tester
View CoW.swift
/// Example of a recursive sum type representing a tree.
/// The compiler uses copy-on-write under the covers to give instances value
/// semantics.
indirect enum Tree<Body> {
case o(_ body: Body, _ left: Tree?, _ right: Tree?)
extension Tree : Equatable where Body: Equatable {}
dabrahams / a_eval0.sml
Last active Mar 19, 2021
From §2.1 of Sig Ager Biernacki Danvy & Midgaard: A Functional Correspondence Between Evaluators and Abstract Machines
View a_eval0.sml
(* Exactly what appears in the paper *)
datatype term = IND of int (* de Bruijn index *)
| ABS of term
| APP of term * term
structure Eval0
= struct
datatype denval = THUNK of unit -> expval
and expval = FUNCT of denval -> expval

Literate Markdown For C++ Programmers

lmt is a tool for extracting text from the code blocks in markdown files. This file demonstrates all the lmt features in a C++-centric way.

Installing lmt

First, install the go language if you don't have it (homebrew: brew install go).

dabrahams / EfficientMutableProjection.swift
Last active Dec 27, 2020
How to efficiently create a mutable projection without inducing CoW
View EfficientMutableProjection.swift
/// Returns `(source, transform(source.pointee))` and destroys `source.pointee`.
/// This is a low-level utility for creating mutable projections of values without
/// causing needless copy-on-write.
/// Typical usage:
/// func f(x: inout X) { // inout means we have exclusive access to `x`.
/// var (xAddress, xFrobnication)
/// = unsafeMoveMap(destroying: &x) { x.frobnication() }
View LearningRateSchedule.swift
#if os(Linux)
import Glibc
import Darwin
extension Collection {
/// Returns the index of the first element that matches the predicate.
/// The collection must already be partitioned according to the predicate, as if
dabrahams / UnsafeReference.swift
Last active Oct 22, 2020
Property wrappers that emulate C++ references
View UnsafeReference.swift
struct UnsafeReference<T> {
public let address: UnsafePointer<T>
var projectedValue: Self { self }
init(_ address: UnsafePointer<T>) {
self.address = address
var wrappedValue: T {
get { address.pointee }
dabrahams / EfficientAny.swift
Last active Jun 14, 2021
An efficient storage foundation for type erasure.
View EfficientAny.swift
// Copyright 2020 Penguin Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
dabrahams / TypeErasureSurvey.swift
Last active Aug 29, 2020
Swift type erasure survey / performance analysis. Build with `swiftc -O -g -whole-module-optimization`
View TypeErasureSurvey.swift
// Protocol to use for type erasure.
protocol Summable {
func sum() -> Int
// Concrete model. Type
extension Int: Summable {
func sum() -> Int { self }
dabrahams / Example1.swift
Last active Jul 22, 2020
Ambiguity problems with giving generic types the right behavior with conditional conformances
View Example1.swift
protocol P { var pop: String { get } }
extension P {
var pop: String { "slow" }
var pop1: String { pop }
protocol Q: P {}
extension Q { var pop: String { "fastQ" } }
protocol R: P {}
dabrahams / FactoryInitialization.swift
Last active Sep 3, 2020
Class factory initializers
View FactoryInitialization.swift
/// Classes whose initializers actually create derived classes
protocol FactoryInitializable {
/// The type of the least-derived class declared to be FactoryInitializable.
/// - Warning: Do not define this in your FactoryInitializable type!
associatedtype FactoryBase: AnyObject, FactoryInitializable = Self
// This associatedtype is a trick that captures `Self` at the point where
// `FactoryInitializable` enters a class hierarchy; in other contexts, `Self`
// refers to the most-derived type.