Skip to content

Instantly share code, notes, and snippets.

Jaden Geller JadenGeller

Block or report user

Report or block JadenGeller

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
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) {
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!).")
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) {
@JadenGeller
JadenGeller / Bind.swift
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
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<Int> {
return BinaryStream(head: value, left: EvenOddStream(value + 1), right: EvenOddStream(value + 2))
}
@JadenGeller
JadenGeller / Return Type Overloading.swift
Last active Aug 29, 2015
Swift Overloading on a Function's Return Type
View Return Type Overloading.swift
// 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)
}
@JadenGeller
JadenGeller / Array Copy.swift
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
}
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
}
@JadenGeller
JadenGeller / Mask Filter.swift
Created Mar 24, 2015
Filter an Array with a Boolean Array as a mask
View Mask Filter.swift
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 / Map2.swift
Last active Aug 29, 2015
Swift Global Map2 Function
View Map2.swift
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)
}
You can’t perform that action at this time.