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 / CrossProduct.swift
Created November 27, 2022 01:33
Cross product collection adapter.
struct CrossProduct2<Base0: Collection, Base1: Collection>: Collection {
public private(set) var base0: Base0
public private(set) var base1: Base1
init(_ base0: Base0, _ base1: Base1) {
self.base0 = base0
self.base1 = base1
}
struct Index: Comparable {
@dabrahams
dabrahams / Grammar+nullSymbolSets.swift
Created July 12, 2022 01:41
Example that would be better if we could create a mixin with additional stored members
extension Grammar {
/// Returns the set of nullable symbols (which sometimes derive 𝝐) and the subset of nulling
/// symbols (which always derive 𝝐) in a grammar that is not yet in nihilist normal form.
func nullSymbolSets(rulesByRHS: MultiMap<Symbol, Rule>)
-> (nullable: Set<Symbol>, nulling: Set<Symbol>)
{
// - Common setup.
// Mapping from symbol to the set of ruleIDs having that symbol as a LHS.
let rulesByLHS = MultiMap(grouping: ruleIDs, by: lhs)
@dabrahams
dabrahams / BitSet.swift
Created July 9, 2022 15:16
UNTESTED: One of many possible bitset implementations.
private typealias Word = UInt
/*
/// Returns the offset at which the `i`th bit can be found in an array of
/// `Word`s.
private func wordOffset(ofBit i: Int) -> Int {
precondition(i >= 0)
return i / Word.bitWidth
}
/// Returns a mask that isolates the `i`th bit within its `Word` in an array of
@dabrahams
dabrahams / ChartParser.swift
Last active May 20, 2022 20:16
Quick/Dirty Chart Parser.
/// A simple bottom-up chart parser.
///
/// - The chart is a lookup table of sets of partial parses (a.k.a. dotted
/// rules), indexed by (predicted-symbol, source-location) pairs.
///
/// - A partial parse is a triple (B, R, N) where:
/// - B is the position in the token stream where the partially-parsed input begins.
/// - R is the partially-recognized BNF rule.
/// - N is the number of RHS symbols of R that have been recognized.
///
@dabrahams
dabrahams / KeynoteTextNotes.md
Created December 13, 2021 02:09
Notes on how Keynote lays out text boxes, and translating that into CSS

Line spacing

In keynote, line spacing is specified per-paragraph. Paragraphs are line-wrapped into physical lines.

For now, we're only interested in text boxes where spacing is specified in “lines.”

For a physical line β and a character γ:

@dabrahams
dabrahams / CoW.swift
Last active April 5, 2021 00:00
Copy-on-write big-O efficiency tester
/// 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
dabrahams / a_eval0.sml
Last active March 19, 2021 18:11
From §2.1 of Sig Ager Biernacki Danvy & Midgaard: A Functional Correspondence Between Evaluators and Abstract Machines
(* 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
dabrahams / EfficientMutableProjection.swift
Last active May 6, 2024 18:30
How to efficiently create a mutable projection without inducing CoW
/// 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() }
@dabrahams
dabrahams / LearningRateSchedule.swift
Last active December 3, 2020 19:48
Thing for Xihui
#if os(Linux)
import Glibc
#else
import Darwin
#endif
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