Skip to content

Instantly share code, notes, and snippets.

@dduan
dduan / IntRandom.swift
Created October 17, 2015 20:37
An extension on Int to generate random Ints and alphanumeric strings.
extension Int {
func asUpperBoundForRandom() -> Int {
return Int(arc4random_uniform(UInt32(self)))
}
func asLengthForRandomAlphanumeric() -> String {
let alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".characters
var chars: [Character] = []
for _ in 1...self {
@dduan
dduan / app.scss
Created January 14, 2013 21:46
An app.scss that works with the app generated by Sencha Cmd with Sencha Touch 2.2 alpha. Replace `resources/sass/app.scss` with it to make `compass compile` work.
@import 'sencha-touch/base';
@import 'sencha-touch/base/all';
@import 'sencha-touch/default';
@import 'sencha-touch/default/all';
// You may remove any of the following modules that you
// do not use in order to create a smaller css file.
@import 'sencha-touch/default/panel';
@import 'sencha-touch/default/button';
@import 'sencha-touch/default/sheet';
@dduan
dduan / intersectInPlacePerformance.swift
Created December 12, 2015 02:03
A simple program to benchmark Set.intersectInPlace() in Swift
import Foundation
let smallSet = Set(Array(1..<999).sort {_, _ in arc4random() % 2 == 0})
let largeSet = Set(Array(1..<99999).sort {_, _ in arc4random() % 2 == 0})
var _count = 999999
let unique: () -> Int = { _count += 1; return _count }
var before = NSDate()
var after = NSDate()
@dduan
dduan / nativeRemoveAllPerformance.swift
Last active December 14, 2015 07:34
A simple program to benchmark performance of removeAll() method for Set
import Foundation
var before = NSDate()
var after = NSDate()
var duration: NSTimeInterval = 0
var a = Set(Array(1..<9999).sort {_, _ in arc4random() % 2 == 0})
(1...10000).forEach { _ in
var b = a
before = NSDate()
SASS_PATH = resources/sass
COFFEE_PATH = app
all: sass coffee
develop: watch server
server:
python3 -m http.server
@dduan
dduan / experimental-slice.swift
Last active February 21, 2016 00:00
An rudimentary experiment on Swift slice syntax.
// Experimental:
// '%' replaces the more ideal '$'
// where a open ended range is in the design, `nil` is used its place
// force casting between Index.Distance and 'Int'
// only on CollectionType, but a version for string is not hard to do
public struct Offset { let value: Int }
prefix operator %+ {}
prefix operator %- {}
@dduan
dduan / d-slicing-syntax.swift
Last active February 28, 2016 04:13
D-like slicing syntax tailored for Swift.
// this is almost good, if only Swift allows specifying precedence for pre/postfix operators.
struct Dollar {}
let $ = Dollar()
struct BInt: IntegerLiteralConvertible {
let bool: Bool
let int: UInt
func relativeTo(total: Int) -> Int {
return bool ? total - Int(int) : Int(int)
}
@dduan
dduan / radar.sh
Created March 23, 2016 07:42
A command to find radars referenced in Swift's code base that aren't public.
# run this in the swift source code folder to get a list of radars
for i in $(grep -hor -E 'rdar://problem/[0-9]+' stdlib lib | sed 's/rdar:\/\/problem\///'); do if [[ ! -z $(curl -s "http://www.openradar.me/$i" | grep 'Tell us more!') ]]; then echo rdar://problem/$i; fi; done
func f<T, U>(x: T.Type, _ y: U) -> Bool {
return y is T
}
func g<T, U>(x: T, _ y: U) -> Bool {
return y is T
}
protocol P {}
class B: P {}
@dduan
dduan / DefaultDictionary.swift
Last active May 16, 2016 22:06
A handy Swift dictionary that returns a specified default value for non-existing keys.
public struct DefaultDictionary<Key : Hashable, Value>: CollectionType {
private var _storage: [Key: Value]
private var _defaultValue: () -> Value
public typealias Element = (Key, Value)
public typealias Index = DictionaryIndex<Key, Value>
public var startIndex: Index {
return _storage.startIndex