Skip to content

Instantly share code, notes, and snippets.

@pyrtsa
pyrtsa / Profiling.swift
Last active October 28, 2016 18:27
Profile a block of code in Swift 2.0
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()
@pyrtsa
pyrtsa / RocketOperator.swift
Created March 27, 2015 13:35
An object initialisation operator in Swift
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)
@pyrtsa
pyrtsa / orders.html
Created March 19, 2015 10:31
A human-readable, order-preserving serialisation for JSON-like data in JavaScript
<!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;
@pyrtsa
pyrtsa / Random.example.swift
Last active March 23, 2017 11:51
Sampling random numbers in Swift
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
@pyrtsa
pyrtsa / let_else.swift
Last active August 29, 2015 14:16
Foolish syntactic idea for Swift: "let else"
/// 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 }
{
// ...
@pyrtsa
pyrtsa / foldr.swift
Created February 24, 2015 07:55
foldr in Swift
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()
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 {
@pyrtsa
pyrtsa / Semigroupy.swift
Last active December 28, 2015 19:40
Semigroupy grouping magic in Swift
// 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 ------------------------------------------------------------
@pyrtsa
pyrtsa / version.bash
Last active March 23, 2023 06:47
Xcode: Set version and build number from Git
#!/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
@pyrtsa
pyrtsa / QuotedString.swift
Created January 23, 2015 14:50
Quoted strings in Swift
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"]