Skip to content

Instantly share code, notes, and snippets.

@JadenGeller
JadenGeller / Swift Array Remove Matching.swift
Last active August 29, 2015 14:16
Swift Array Remove Matching
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) {
@JadenGeller
JadenGeller / Swift Ambiguous.swift
Last active August 29, 2015 14:16
Swift Ambiguous
// 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!).")
@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 / 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 / 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
}
@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 })
}
@JadenGeller
JadenGeller / Choose Function.swift
Created March 24, 2015 01:57
Swift Choose Function
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
@JadenGeller
JadenGeller / Return Type Overloading.swift
Last active August 29, 2015 14:17
Swift Overloading on a Function's Return Type
// 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)
}