Skip to content

Instantly share code, notes, and snippets.



Swift’s type system supports a number of different ways of taking a function or type and abstracting it. Usually, this is done by adding a generic parameter and an associated set of constraints. Similarly, a function that takes a particular type of argument can be abstracted to any number of those arguments by making it variadic with triple-dot (...) syntax. Today, both of these features are permitted separately: you can define a generic function that takes a variable number of arguments, such as

func debugPrint<T>(_ items: T...) 
  where T: CustomDebugStringConvertible
  for (item: T) in items {
View AsyncOperation.swift
class AsyncOperation: Operation {
private(set) var taskHandle: Task.Handle<Void, Error>?
fileprivate enum State: Equatable {
case ready
case executing
case finished
fileprivate var state: State = .ready {
View gist:44b46747c294090020dd3edad0e2e040
# NOTE: You need sccache first - but if you already have it, this'll void your cache.
env SCCACHE_RECACHE=1 caffeinate ./swift/utils/build-script -R --cmake-cxx-launcher (which sccache) --cmake-c-launcher (which sccache) --skip-build-benchmark
View gist:56034199577f057c52ef3e0cdd8b7631
./utils/build-script -r --foundation --swiftpm --llbuild --libdispatch --install-swift --install-swiftpm --install-llbuild --install-foundation --install-libdispatch --xctest --install-xctest --install-swift --install-swiftpm --install-llbuild --install-foundation --install-libdispatch --xctest --install-xctest --test --skip-test-swift --skip-test-foundation --indexstore-db '--llvm-install-components=llvm-cov;llvm-profdata;IndexStore;clang;clang-resource-headers;compiler-rt;clangd;lld'
./utils/build-script -r --foundation --swiftpm --llbuild --libdispatch --install-swift --install-swiftpm --install-llbuild --install-foundation --install-libdispatch --xctest --install-xctest --install-swift --install-swiftpm --install-llbuild --install-foundation --install-libdispatch --xctest --install-xctest --test --skip-test-swift --skip-test-foundation --indexstore-db '--llvm-install-components=llvm-cov;llvm-profdata;IndexStore;clang;clang-resource-headers;compiler-rt;clangd;lld' --skip-test-libdispatch --ski
CodaFi / MBE.swift
Created Jul 8, 2018
Macro-by-Example: Deriving Syntactic Transformations from their Specifications, Re-Typeset
View MBE.swift
//: Foreword
//: This playground is the culmination of a week of digging around
//: for the oldest papers I could find about (Lisp) macros and their evolution.
//: I found this paper particularly enlightening as it is the place where Scheme
//: and Rust-style "Macro-by-Example" macros first came into their own. In
//: addition, this paper discusses the implementation challenges faced by
//: previous macro systems and Kohlbecker's first go at MBEs.
//: I have re-typeset this paper because [the one hosted by IU](
View SevenInOne.swift
//: Playground - noun: a place where people can play
//: Roots of Unity Often Lie
indirect enum BTree {
case empty
case branch(BTree, BTree)
func encode(_ t: (BTree, BTree, BTree, BTree, BTree, BTree, BTree)) -> BTree {
switch t {
CodaFi / Nub.agda
Created Apr 20, 2018
Agda wrote nub for me (with some prodding)
View Nub.agda
module Untitled where
open import Data.Nat
open import Data.Nat.Properties
open import Data.Char hiding (_≟_)
open import Data.Bool hiding (_≟_)
open import Data.List
open import Data.Unit hiding (_≟_)
open import Data.Empty
open import Relation.Nullary
View turing-fake.swift
struct State0 {}; struct State1 {}; struct FinalState {} /*States*/
struct Symbol {}; struct Blank {} /*Symbols*/
struct Left {}; struct Right {} /*Directions*/
struct EndOfTape {} /*End of Tape*/
struct Transition<StartState, StartHead, EndState, EndHead, Direction> {
static func transitionOne() -> Transition<State0, Blank, State1, Symbol, Left> { fatalError() }
static func transitionTwo() -> Transition<State1, Blank, State0, Symbol, Right> { fatalError() }
static func transitionThree<State>() -> Transition<State, Symbol, FinalState, Symbol, Left> { fatalError() }
View AlgaSketch.swift
struct G<A> {
let vertices : [A]
let edges: [(A, A)]
let gex = G(vertices: [1, 2, 3], edges: [(1, 2), (2, 3)])
let gbad = G(vertices: [1], edges: [(1, 2)])
View MonadsAsModules.agda
module MonadsAsModules where
import Agda.Primitive using (Level)
id : {k}{X : Set k} X X
id x = x
const : {a b} {A : Set a} {B : Set b} A B A
const x _ = x