Skip to content

Instantly share code, notes, and snippets.

Avatar

Austin Zheng austinzheng

View GitHub Profile
@austinzheng
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
#,TEST,SAMPLES,MIN(us),MAX(us),MEAN(us),SD(us),MEDIAN(us)
1,AngryPhonebook,10,8649,9407,8877,240,8838
2,Array2D,10,1517900,1556704,1536566,12334,1538343
3,ArrayAppend,10,93643,95789,94422,653,94141
4,ArrayAppendReserved,10,93166,95597,93900,861,93892
5,ArrayInClass,10,22015,22923,22497,248,22536
6,ArrayLiteral,10,2238,2351,2272,35,2273
7,ArrayOfGenericPOD,10,4296,4914,4512,191,4517
8,ArrayOfGenericRef,10,16309,17348,16727,350,16624
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
means.
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 = s.next() else {
return nil
}
return predicate(next) ? next : nil
@austinzheng
austinzheng / screaming.swift
Last active May 28, 2016
aaaaaaaaaaaaaaaaaaaaa
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 = collectionIndex.next(collection)
}
let returnValue : T.Element /* e.g. String */ = collection[collectionIndex]
return returnValue
@austinzheng
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
austinzheng / generics_manifesto.md
Last active Jun 11, 2020
Douglas Gregor's Swift Generics Manifesto, with added markdown formatting
View generics_manifesto.md

(Source: https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160229/011666.html)

Introduction

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() {
print("Starting")
// 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.