This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
public func profiling<R>(label: String, @noescape _ block: () -> R) -> R { | |
NSLog("*** %@...", label) | |
let start = NSDate() | |
defer { | |
let end = NSDate() | |
NSLog("*** %@ took %5.3g seconds", label, end.timeIntervalSinceDate(start)) | |
} | |
return block() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
infix operator => { associativity left precedence 100 } | |
infix operator =>? { associativity left precedence 100 } | |
/// Perform side effects on `x` and then return it. Example: | |
/// | |
/// self.label = UILabel(frame: frame) => { l in | |
/// l.text = "Hello?" | |
/// } | |
func =><T>(x: T, f: T -> ()) -> T { | |
f(x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<meta charset="utf-8"> | |
<script src="orders.js"></script> | |
<style> | |
#display { | |
white-space: pre-wrap; | |
font-family: 'Menlo', monospace; | |
font-size: 12px; | |
line-height: 16px; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Random | |
random(0 ..< 10) // Int | |
random(1 ... 1000_000) // Int | |
random(-1000_000 ... 1000_000) // Int | |
random(Int.min ... Int.max) // Int | |
randomMax(UInt64.max) // UInt64 | |
random(0 ... UInt64.max) // UInt64 | |
random(1 ... UInt(10)) // UInt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// The improved `if let` syntax in Swift 1.2 is all cool. But there's | |
/// one pattern it doesn't solve great: writing a block of error | |
/// handling right where `nil` is returned: | |
func validate1(json: AnyObject) -> String { | |
var error: String? | |
if let name = json["name"] as? String ?? { error = "missing name"; return nil }, | |
let year = json["year"] as? Int ?? { error = "missing year"; return nil } | |
{ | |
// ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func foldr<A : SequenceType, B>(xs: A, y: B, f: (A.Generator.Element, () -> B) -> B) -> B { | |
var g = xs.generate() | |
var next: () -> B = {y} | |
next = { return g.next().map {x in f(x, next)} ?? y } | |
return next() | |
} | |
foldr([1, 2, 3, -4, -5, 6], "") {a, b in | |
println(a) | |
return a < 0 ? toString(a) : b() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func StringFromURL (url : NSURL, | |
error errorPointer : NSErrorPointer) -> (NSString?) { | |
var error : NSError? | |
// Request data | |
let data: NSData | |
if let some = NSData(contentsOfURL:url, options: NSDataReadingOptions(rawValue:0), error: &error) { | |
data = some | |
} else { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// What people often ask DefaultDict for, could be done with just one | |
// extension method for Optional. | |
extension Optional { | |
mutating func mappend(z: Wrapped, _ f: (Wrapped, Wrapped) -> Wrapped) { | |
self = map {x in f(x, z)} ?? z | |
} | |
} | |
// --- Examples ------------------------------------------------------------ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Xcode: Set version and build number from Git | |
# -------------------------------------------- | |
# | |
# This script sets the version number `CFBundleShortVersionString` to one of | |
# | |
# - `1.2.3` -- for the tagged commit `v1.2.3` or a hyphen-separated prerelease, | |
# e.g. `v1.2.3-alpha`, `v1.2.3-alpha.2`, `v1.2.3-beta`, `v1.2.3-rc`. | |
# - `1.2.3-7-gabc1234` -- at commit `abc1234`, 7 commits after `v1.2.3`, | |
# - `1.2.3-7-gabc1234-dirty` -- when there are uncommitted changes, or |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct QuotedString : Printable { | |
var string: String | |
init(_ string: String) { | |
self.string = string | |
} | |
var description: String { | |
let backslash: UnicodeScalar = "\\" | |
let quote: UnicodeScalar = "\"" | |
let hex: [UnicodeScalar] = ["0", "1", "2", "3", "4", "5", "6", "7", | |
"8", "9", "a", "b", "c", "d", "e", "f"] |