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
// Functionally equivalent to @synchronize in Objective-C | |
// Beware that this might behave improperly if an exception is thrown | |
func synchronize(obj: AnyObject, block: () -> ()){ | |
objc_sync_enter(obj) | |
block() | |
objc_sync_exit(obj) | |
} | |
// Example | |
synchronize(sharedObject) { |
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 Semaphore { | |
let semaphore: dispatch_semaphore_t | |
init(value: Int = 0) { | |
semaphore = dispatch_semaphore_create(value) | |
} | |
// Blocks the thread until the semaphore is free and returns true | |
// or until the timeout passes and returns false |
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
// Let's start with an example! | |
bind(view.frame.size) { size in | |
println("The area is \(size.width * size.height)") | |
} | |
// Without bind, this code would look like this | |
// let size = view.frame.size | |
// println("The area is \(size.width * size.height)") | |
// Note how bind cleans up code that accesses the same variable |
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
// If you haven't already, check out my implementation of streams in Swift first! | |
// https://gist.github.com/JadenGeller/42bd39b60f6ab2de9852 | |
// Okay, you're back. Now I'm feeling evil, so I created a binary tree sort of stream. | |
// Why? No reason. But it allows us to do some pretty neat stuff. | |
// Here's an example: | |
func EvenOddStream(value: Int) -> BinaryStream<Int> { | |
return BinaryStream(head: value, left: EvenOddStream(value + 1), right: EvenOddStream(value + 2)) | |
} |
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
extension Bool { | |
init<T : IntegerType>(_ integer: T){ | |
self.init(integer != 0) | |
} | |
} | |
// Now you can do this | |
let x = Bool(5) // true | |
let y = Bool(-1) // true | |
let z = Bool(0) // false |
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
// Creates a new function from an inout function | |
// that does not modify the passed in arugment | |
// but returns a modified argument and result | |
func shield<T,U>(action: inout T -> U) -> T -> (T, U) { | |
return { arg in | |
var copy = arg | |
let result = action(©) | |
return (copy, result) | |
} | |
} |
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
// Extension to Array that adds insertion and removal operations that make copies | |
// instead of mutating the array | |
extension Array { | |
func appended(newValue: T) -> Array { | |
var copy = self | |
copy.append(newValue) | |
return copy | |
} |
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 PermutationSequenceGenerator<T> : GeneratorType { | |
var permutationSpaceGenerator: PermutationSpaceGenerator<Int> | |
let elements: [T] | |
init(elements: [T]) { | |
self.elements = elements | |
self.permutationSpaceGenerator = PermutationSpaceGenerator(objects: Array(indices(elements))) | |
} | |
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 map2<S : SequenceType, T : SequenceType, U>(lhs: S, rhs: T, transform: (S.Generator.Element, T.Generator.Element) -> U) -> [U] { | |
return Array(Zip2(lhs, rhs)).map(transform) | |
} |
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 filter<S : SequenceType, T : SequenceType where T.Generator.Element == Bool>(source: S, mask: T, defaultValue: S.Generator.Element) -> [S.Generator.Element] { | |
return Array(Zip2(source, mask)).map({ (value, include) in include ? value : defaultValue }) | |
} |