Last active
October 15, 2015 12:51
-
-
Save darthpelo/bc11c413acc27d68a543 to your computer and use it in GitHub Desktop.
Playing with closure. Copy&paste in Playground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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