Skip to content

Instantly share code, notes, and snippets.

@darthpelo
Last active October 15, 2015 12:51
Show Gist options
  • Select an option

  • Save darthpelo/bc11c413acc27d68a543 to your computer and use it in GitHub Desktop.

Select an option

Save darthpelo/bc11c413acc27d68a543 to your computer and use it in GitHub Desktop.
Playing with closure. Copy&paste in Playground
typealias closureType = (Int) -> (Int)
let ff = { return $0 * 2 }
let tt = { return $0 - ff($0) }
func ret(idx: Int, closure: closureType) -> NSNumber {
return NSNumber(integer: closure(idx))
}
let _ = (0...100).map { return ret($0, closure: ff) }
let _ = (0...100).map { return ret($0, closure: tt) }
let mapFunc = [ff, tt] // var mapFunc = [closureType]()
let result = mapFunc.map { $0(1) }
struct Calculator: Operation {
func ret(idx: Int, closure: closureType) -> NSNumber {
return NSNumber(integer: closure(idx))
}
}
Calculator().ret(1, closure: tt)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment