Skip to content

Instantly share code, notes, and snippets.

View dabrahams's full-sized avatar
🤩
STLabbin’

Dave Abrahams dabrahams

🤩
STLabbin’
View GitHub Profile
extension RangeReplaceableCollection {
public fun remove<E>(where exclude: [E](Element)->Bool) inout {
let end = end_position()
var i = first(where: exclude)
if i == end { return }
let include =
var j = self[self.position(after: i)...]
.first(where: x => !exclude(x) )
var k = j
@dabrahams
dabrahams / NoRecursiveConstraint.swift
Last active January 24, 2024 19:48
Avoiding Recursive Constraints
// Everything but the slicing-related requirements of Collection
protocol CollectionCore {
associatedtype Element
associatedtype Index
func startIndex() -> Index
func endIndex() -> Index
func index(after i: Index) -> Index
@dabrahams
dabrahams / Memo.swift
Created July 28, 2023 21:33
Mock-up of program transformation with value semantics and memoization
// This file demonstrates how memoization might work in the transformation of a `ScopedProgram` into
// a `TypedProgram`, which additionally represents type information.
public struct TypedProgram {
/// The prior representation of the program, without analysis of types.
public let base: ScopedProgram
/// A type.
public struct Type_: Hashable {
let id: Int
@dabrahams
dabrahams / ValueMemoize.swift
Last active July 26, 2023 03:12
Value semantic memoization in Swift
// Just to give a sense of reality.
struct AST {}
/// The wrapper over a dictionary that maps keys onto non-optional
/// values, expecting them to have already been computed.
struct ImmutablePropertyMap<Key: Hashable, Value> {
let storage: Dictionary<Key, Value>
subscript(k: Key) -> Value { storage[k]! }
}
@dabrahams
dabrahams / repl.txt
Created February 17, 2023 00:26
Swift repl / LLDB printing horror
➜ val git:(interpreter) ✗ swift repl
Welcome to Apple Swift version 5.7.2 (swiftlang-5.7.2.135.5 clang-1400.0.29.51).
Type :help for assistance.
1> let x = [[1], [2, 3]]
x: [[Int]] = 2 values {
[0] = 1 value {
[0] = 1
}
[1] = 2 values {
@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 γ: