Skip to content

Instantly share code, notes, and snippets.

@SwiftStudies
Forked from erica/gist:2b7b912d7b7910bdc9cf
Last active August 29, 2015 14:22
Show Gist options
  • Save SwiftStudies/e61c9791fb7531e81a74 to your computer and use it in GitHub Desktop.
Save SwiftStudies/e61c9791fb7531e81a74 to your computer and use it in GitHub Desktop.
//: Playground - noun: a place where people can play
import Cocoa
func f1(x : Int) -> Int? {
return (x == 3) ? 1 : 3
}
func f2(x : Int) -> Int? {
if x < 0 || x > 3 {return nil}
return [3, 0, 1, 0][x - 1]
}
func f3(var x: Int) -> Int? {
return x ^ (1 << 1)
}
func f4(x : Int) -> Int? {
if ((x != 3) && (x != 1)) {return nil}
let s = " 3 1"
let index = advance(s.startIndex, x)
return (s.substringFromIndex(index) as NSString).integerValue
}
func f5(x : Int) -> Int? {
return [1:3, 3:1][x % 4]
}
func f6(x : Int) -> Int? {
return Int(atan(pow(0.0, -(1.0 - Double(x))))) * 2 + 1
}
func f7(x : Int) -> Int? {
return lrint(asin(Double((x - 1) / 2))) + 1
}
func f8(x : Int) -> Int? {
if let i = first("\(x)".utf16) {
return Int(sqrt(fmod(Double(i), 10.0)))
}
return nil
}
func f9(x : Int) -> Int? {
return 4 - x
}
func fa(x : Int) -> Int? {
var count = -1
for i in 0...1 {
count += (x & 1 << i) != 0 ? 1 : 0
}
return Int(cos(Double(count) * Double(M_PI))) + 2
}
func fb(x: Int) -> Int? {
var set = [1, 3] as Set<Int>
set.remove(x)
let y = set.removeFirst()
return isEmpty(set) ? y : nil
}
func fc(x:Int)->Int{
typealias f = ()->Int
var funcs = [Int : f]()
funcs[1] = {3}
funcs[3] = {1}
return funcs[x]!()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment