Skip to content

Instantly share code, notes, and snippets.

@JadenGeller
JadenGeller / Swift Synchronize.swift
Last active August 29, 2015 14:17
Swift Synchronize
// 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) {
@JadenGeller
JadenGeller / Semaphore.swift
Last active May 16, 2017 15:51
Swift Semaphore
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
@JadenGeller
JadenGeller / Bind.swift
Last active August 29, 2015 14:17
Swift Bind (Non-Optional Let Expression)
// 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
@JadenGeller
JadenGeller / BinaryStreams.swift
Last active August 29, 2015 14:17
Binary Streams
// 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))
}
@JadenGeller
JadenGeller / Init Bool With Int.swift
Created March 23, 2015 07:48
Cast Int to Bool in Swift
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
@JadenGeller
JadenGeller / Shield.swift
Created March 23, 2015 23:16
Swift Shields
// 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(&copy)
return (copy, result)
}
}
@JadenGeller
JadenGeller / Array Copy.swift
Last active August 29, 2015 14:17
Swift Array Copy By Inserting/Removing
// 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
}
struct PermutationSequenceGenerator<T> : GeneratorType {
var permutationSpaceGenerator: PermutationSpaceGenerator<Int>
let elements: [T]
init(elements: [T]) {
self.elements = elements
self.permutationSpaceGenerator = PermutationSpaceGenerator(objects: Array(indices(elements)))
}
@JadenGeller
JadenGeller / Map2.swift
Last active August 29, 2015 14:17
Swift Global Map2 Function
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)
}
@JadenGeller
JadenGeller / Mask Filter.swift
Created March 24, 2015 01:20
Filter an Array with a Boolean Array as a mask
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 })
}