Skip to content

Instantly share code, notes, and snippets.

@rcdilorenzo
Last active August 29, 2015 14:02
Show Gist options
  • Save rcdilorenzo/1fdfa95c05684a000144 to your computer and use it in GitHub Desktop.
Save rcdilorenzo/1fdfa95c05684a000144 to your computer and use it in GitHub Desktop.
import Foundation
extension Int {
func timesMap(closure: (Int) -> AnyObject) -> Array<AnyObject> {
var array: AnyObject[] = []
for x in 1...self {
array += closure(x)
}
return array;
}
}
class Fib {
class func at(num: Int) -> Int {
switch num {
case 0, 1, 2:
return 1
default:
return at(num-2) + at(num-1)
}
}
class func sequence(num: Int) -> Array<AnyObject> {
return num.timesMap({ x -> AnyObject in return Fib.at(x) })
}
}
import XCTest
class SwiftPlaygroundTests: XCTestCase {
func testFibSequence() {
XCTAssertEqualObjects(Fib.at(7), 13)
XCTAssertEqualObjects(Fib.sequence(7), [1, 1, 2, 3, 5, 8, 13])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment