Skip to content

Instantly share code, notes, and snippets.


Austin Zheng austinzheng

View GitHub Profile
austinzheng / bench_before.txt
Created Jun 23, 2016
Swift benchmarks, before dictionary index change
View bench_before.txt
KUNPENG:bin austinzheng$ ./Benchmark_Onone --num-samples=10
View r2-swift2.swift
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
View r2.swift
struct StringTupleCollection : Collection {
let backing : [String : [String]]
var startIndex : StringTupleIndex {
// Find the first key with a non-empty array
var idx = backing.startIndex
while idx != backing.endIndex {
let array = backing[idx].1
if !array.isEmpty {
return StringTupleIndex(dictKey: idx, arrayKey: 0)
View r1.swift
extension Sequence {
typealias E = Iterator.Element
typealias I = Iterator
public func prefix(while predicate: (E) -> Bool) -> UnfoldSequence<E, I> {
return sequence(state: makeIterator(), next: { (s: inout I) -> E? in
guard let next = else {
return nil
return predicate(next) ? next : nil
austinzheng / screaming.swift
Last active May 28, 2016
View screaming.swift
func ==(lhs: (), rhs: ()) -> Bool {
return true
func ==<...T : Equatable>(lhs: (T...), rhs: (T...)) -> Bool {
let firstLeft : #First(T...) = #first(lhs)
let firstRight : #First(T...) = #first(rhs)
let firstAreEqual = (firstLeft == firstRight)
if !firstAreEqual {
View existential-opening-2.swift
func getString(collection: Any<Collection where .Element == String>, index: Int) -> String {
do collection openas T {
// T is available as a type.
// (Is this true?) T's associated types are also available, since T is opened into an explicit type.
var collectionIndex : T.Index = input.startIndex
for _ = 0..<index {
collectionIndex =
let returnValue : T.Element /* e.g. String */ = collection[collectionIndex]
return returnValue
austinzheng / swift-kvc.swift
Last active May 27, 2016
An exploration of a possible KVC-like API for a future version of Swift.
View swift-kvc.swift
/// A statically typed view into a get-only property.
struct TypedGetPropertyView<T> {
/// The name of the property, as a string.
let name : String
/// The actual metatype of the property.
let metatype : T.Type
/// Get the value of the property.
func get() -> T
View existential_opening.swift
func compare(a: Any, b: Any) -> Bool {
if let a = a as? Equatable, a openas T {
if let b = b as? Equatable, b openas T {
return a == b
fatalError("a and b aren't comparable")
austinzheng /
Last active Jun 11, 2020
Douglas Gregor's Swift Generics Manifesto, with added markdown formatting



The “Complete Generics” goal for Swift 3 has been fairly ill-defined thus fair, with just this short blurb in the list of goals:

Complete generics: Generics are used pervasively in a number of Swift libraries, especially the standard library. However, there are a number of generics features the standard library requires to fully realize its vision, including recursive protocol constraints, the ability to make a constrained extension conform to a new protocol (i.e., an array of Equatable elements is Equatable), and so on. Swift 3.0 should provide those generics features needed by the standard library, because they affect the standard library's ABI.

This message expands upon the notion of “completing generics”. It is not a plan for Swift 3, nor an official core team communication, but it collects the results of numerous discussions among the core team and Swift developers, both of the compiler an

View gcdexample.swift
import Foundation
func main() {
// GCD works by dispatching chunks of code (represented by closures, and called 'blocks') onto things called 'dispatch
// queues'. These queues run the blocks, either on the main thread or in a background thread. (Queues don't correspond
// to threads on a one-to-one basis; GCD is responsible for spinning up threads to service queues.)
// Here's a variable representing shared state I want to manipulate from different threads.
You can’t perform that action at this time.