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 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) { |
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! | |
// 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!).") |
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
// 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 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
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 }) | |
} |
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 choose<S : SequenceType, T : SequenceType, U : SequenceType where U.Generator.Element == Bool, S.Generator.Element == T.Generator.Element>(onTrue: S, onFalse: T, picker: U) -> [S.Generator.Element]{ | |
return map2(Zip2(onTrue, onFalse), picker, { zipped, choice in choice ? zipped.0 : zipped.1 }) | |
} | |
// Note map2 is defined here: https://gist.github.com/JadenGeller/6f5c5dcb16c39b936d17 |
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
// 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) | |
} |