- Http://www.alexisgallagher.com
View dispatch-sync-safe.swift
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 result() -> A { | |
var result : A = dispatch_sync(resultQueue, { | |
return self.result | |
}) | |
return result | |
} | |
func dispatch_sync<R> (queue: dispatch_queue_t, block: Void -> R ) -> R { | |
var result: R! | |
dispatch_sync(queue) { |
View example.playground
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 Swift | |
import Cocoa | |
// Properties you need as an enum - problem of key value coding is it allows you to type *anything*, typos compile fine. | |
enum SyncObjectPropertyName { | |
case Archived | |
case Title | |
} | |
// Protocol shared both for local and server |
View gist:a6478004c6cee552f225
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
public protocol CountableCollectionType: CollectionType { | |
var count: Int { get } | |
} | |
extension Array: CountableCollectionType {} | |
public protocol CollectionViewCellFactoryType { | |
typealias Item | |
typealias Cell: UICollectionViewCell | |
func cellForItem(item: Item, inCollectionView collectionView: UICollectionView, atIndexPath indexPath: NSIndexPath) -> Cell |
View :(
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 | |
import ImageIO | |
infix operator >>= { associativity left } | |
func >>=<A,B>(l: A?, r: A -> B?) -> B? { | |
if let x = l { | |
return r(x) | |
} | |
return nil |
View Swift Coalesce Function.swift
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
/* | |
* coalesce takes in a list of optional values | |
* and returns the first one that is not nil | |
*/ | |
func coalesce<T>(all: @autoclosure () -> T? ...) -> T? { | |
for f: () -> T? in all { | |
if let x = f() { return x } | |
} | |
return nil |
View oo_to_org.rb
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
#!/usr/bin/env ruby | |
require "rubygems" | |
require "appscript" | |
include Appscript | |
oo = app("OmniOutliner Professional") | |
outline = oo.documents[1].get |
View AnyValidator.swift
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
protocol ValidatorType { | |
typealias ValidatedType | |
func isValid(object: ValidatedType) -> Bool | |
} | |
// You might want to make it a reference type, depending on your case. | |
struct AnyValidator<T>: ValidatorType { | |
private let validatorBlock: (T) -> Bool | |
init<V: ValidatorType where V.ValidatedType == T>(validator: V) { |
View mapcat.clj
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
clojure.core/mapcat | |
(All [c b ...] | |
[[b ... b -> (Option (Seqable c))] (Option (Seqable b)) ... b -> (Seq c)]) |
View Keychain.swift
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 Security | |
public class Keychain | |
{ | |
public class func set(key: String, value: String) -> Bool | |
{ | |
if let data = value.dataUsingEncoding(NSUTF8StringEncoding) | |
{ | |
return set(key, value: data) | |
} |
View gcd.swift
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 | |
// Executes an array of blocks in parallel, but only returns after they're all done. | |
func parallel(blocks: [() -> ()]) { | |
let group = dispatch_group_create() | |
let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) | |
for block in blocks { | |
dispatch_group_async(group, queue, block) | |
} |
OlderNewer