Skip to content

Instantly share code, notes, and snippets.

View austinzheng's full-sized avatar

Austin Zheng austinzheng

View GitHub Profile
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 / screaming.swift
Last active May 28, 2016 08:28
aaaaaaaaaaaaaaaaaaaaa
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 {
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
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)
/*
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
@austinzheng
austinzheng / bench_before.txt
Created June 23, 2016 07:55
Swift benchmarks, before dictionary index change
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