Skip to content

Instantly share code, notes, and snippets.

extension NSData {
var SHA1: NSData {
var digest = [UInt8](count: Int(CC_SHA1_DIGEST_LENGTH), repeatedValue: UInt8(0))
CC_SHA1(bytes, CC_LONG(length), &digest)
return NSData(bytes: digest, length: Int(CC_SHA1_DIGEST_LENGTH))
}
var hex: String {
var hexString = ""
let hexTable = "0123456789abcdef"
let bytes = UnsafePointer<UInt8>(self.bytes)
@qwzybug
qwzybug / turnstile.swift
Created November 4, 2014 23:47
Experimenting with using variable enums as state machines, inspired by http://github.com/schwa/SwiftStateMachine
// Playground - noun: a place where people can play
import UIKit
enum Turnstile {
case Locked
case Unlocked
mutating func push() -> Bool {
switch self {
@qwzybug
qwzybug / swiftchars.txt
Created October 28, 2014 20:28
Were You Aware? that the Swift Character type is 9 bytes large. (And really likes to segfault if you access its memory.)
'a' 61ffffffffffff7f01 UTF8 61
[sunglasses] f09f988effffff7f01 UTF8 F09F988E
[multiply] c397ffffffffff7f01 UTF8 C397

Keybase proof

I hereby claim:

  • I am qwzybug on github.
  • I am qwzybug (https://keybase.io/qwzybug) on keybase.
  • I have a public key whose fingerprint is 410E E7B4 A821 19CF E0AD 0C9B E1D9 3BEA 9428 91DE

To claim this, I am signing this object:

func &<T>(x: T, op: T -> T) -> T {
return op(x)
}
func &<T>(x: T, op: T -> ()) -> T {
op(x)
return x
}
attributes.transform3D = CATransform3DIdentity
import UIKit
enum Angle {
case Radians(Double)
case Degrees(Double)
var radians: Double {
switch(self) {
case let .Radians(scalar):
return scalar
// Playground - noun: a place where people can play
import Foundation
enum LazyVal<T, U> {
case Raw(T)
case Val(U)
case None
}
@qwzybug
qwzybug / lazy.swift
Created July 9, 2014 21:22
Lazy computed properties in Swift
import Cocoa
class TestClass {
@lazy var lazy: NSDate = {
return NSDate.date()
}()
}
let t = TestClass()
println(t.lazy)
//
// UIApplication+DisplayedLanguage.m
// Crossfader
//
// Created by Devin Chalmers on 4/14/14.
//
//
#import "UIApplication+DisplayedLanguage.m.h"
#!/usr/bin/ruby1.9.1 -Kw
# -*- coding: utf-8 -*-
# Dijkstra's single-source shortest path algorithm, from https://gist.github.com/roganartu/9407316#file-dijkstra-rb
class Edge
attr_accessor :src, :dst, :length
def initialize(src, dst, length = 1)
@src = src