Skip to content

Instantly share code, notes, and snippets.

View ketzusaka's full-sized avatar

james ketzusaka

  • Tools for Humanity
  • San Francisco, CA
  • 21:35 (UTC -07:00)
View GitHub Profile
@ketzusaka
ketzusaka / gist:6198106
Last active December 20, 2015 21:29
Option Checking in Ruby: Hash vs Bitwise Operations This is a simple script that compares how fast it is to build option dictionaries and checking them versus using integers and bitwise flagging
require 'benchmark'
class HashedFlags
def initialize(opts = {})
@silly_prepend = opts.fetch(:silly_prepend, false)
@uppercase = opts.fetch(:uppercase, false)
end
end
SILLY_PREPEND = 0b00000001
@ketzusaka
ketzusaka / gist:12743fa9d6569dbb447d
Created June 6, 2014 03:38
Swift Operator Overloading Adding << On Array
/*
In Swift, you can overload operators. A common operator in Ruby for working with arrays is <<,
which is syntatic sugar for array.push(). Since Array's in Swift don't take bitwise operations, this is
an example demonstrating adding that operator to an array that contains any type.
As an extra tidbit, I've added >> to prepend an array with an object
*/
@infix func << <T>(inout array: Array<T>, item: T) {
array.append(item)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDECodeSnippetCompletionPrefix</key>
<string>swift_dispatch_once</string>
<key>IDECodeSnippetCompletionScopes</key>
<array>
<string>All</string>
</array>
/*
This is a Swift Singleton. Lack of inline class variables right now make this a slightly more difficult task.
Please ignore the oddly named class for the example. viewWillAppear made for good testing ;)
*/
class DetailViewController: UIViewController {
func sharedInstance() -> DetailViewController {
struct Container {
static var instance: DetailViewController?
static var token: dispatch_once_t = 0
@ketzusaka
ketzusaka / gist:acedad5c25e6db1acadd
Created July 16, 2015 17:39
Fancy ObserverContext
import Foundation
public class ObserverContext: NSObject {
public let keyPath: String
public let options: NSKeyValueObservingOptions
public var context = 0
public init(keyPath: String, options: NSKeyValueObservingOptions = nil) {
self.keyPath = keyPath
self.options = options
}
@ketzusaka
ketzusaka / genscoped-variables.swift
Created July 17, 2015 16:44
Generator-scoped variables
func anyGenerator<Element>(initial: Element, body: (inout Element) -> Element?) -> AnyGenerator<Element> {
return {
var i: Element? = initial
return anyGenerator {
var r: Element?
if var x = i {
r = body(&x)
i = x
}
@ketzusaka
ketzusaka / first.swift
Created July 20, 2015 19:20
First in Swift
/**
Find the first element in a sequence passing a test.
:param: source Source sequence to iterate
:param: includeElement Closure to evaluate elements with
:return: First element in the sequence for which includeElement returns true, or nil if none found
*/
public func first<S: SequenceType>(source: S, includeElement: (S.Generator.Element) -> Bool) -> S.Generator.Element? {
var filteredSource = lazy(source).filter(includeElement).generate()
return filteredSource.next()
protocol Watable { }
func arrayTest<Type: Watable>(v: [Type]) {
if let unwrapped = v as? [Watable] {
print("Success!: \(unwrapped)")
} else {
print("Failed :(")
}
}
protocol Watable { }
extension Array: Watable { }
func test<Type: Watable>(v: Type) {
// Is there a way to tell if Type is an array with any type?
}
import CoreData
class WBManagedElement: NSManagedObject {
}
class ElementParameter: NSObject, NSSecureCoding {
private struct Keys {
static let element = "element"