Instantly share code, notes, and snippets.

• Sort options
Last active Aug 29, 2015
Swift Array Remove Matching
View Swift Array Remove Matching.swift
 extension Array { /* * Similiar to filter, but removes the results that match * in addition to returning them */ mutating func removeMatching(removeElement: T -> Bool) -> [T] { var removed = [T]() for index in stride(from: self.endIndex-1, through: 0, by: -1) {
Last active Aug 29, 2015
Swift Ambiguous
View Swift Ambiguous.swift
 // Let's start with an example! // We specify three anbiguous values a, b, c each which can be any integer [1,7] let a = Ambiguous(1,2,3,4,5,6,7) let b = Ambiguous(1,2,3,4,5,6,7) let c = Ambiguous(1,2,3,4,5,6,7) // We try to satisfy on a,b,c pythagorean's theorem, and we print the side lengths is some values a,b,c satisfy if satisfy(a,b,c)({ (c.choice * c.choice == a.choice * a.choice + b.choice * b.choice) && (a.choice > b.choice)}) { println("We can have a triangle with sides lengths \(a.bound!), \(b.bound!), \(c.bound!).")
Last active Aug 29, 2015
Swift Synchronize
View Swift Synchronize.swift
 // 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) {
Last active Aug 29, 2015
Swift Bind (Non-Optional Let Expression)
View Bind.swift
 // 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
Last active Aug 29, 2015
Binary Streams
View BinaryStreams.swift
 // 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 { return BinaryStream(head: value, left: EvenOddStream(value + 1), right: EvenOddStream(value + 2)) }
Last active Aug 29, 2015
 // Note these divide functions both except identical inputs func divide(a: Int, b: Int) -> Int { return a / b } func divide(a: Int, b: Int) -> Float { return Float(a) / Float(b) }
Last active Aug 29, 2015
Swift Array Copy By Inserting/Removing
View Array Copy.swift
 // 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 }
Created Mar 24, 2015
Shuffle Array
View Array Shuffle.swift
 extension Array { func shuffled() -> [T] { var list = self for i in 0..<(list.count - 1) { let j = Int(arc4random_uniform(UInt32(list.count - i))) + i swap(&list[i], &list[j]) } return list }
Created Mar 24, 2015
Filter an Array with a Boolean Array as a mask