Created
June 30, 2020 07:20
-
-
Save geor-kasapidi/c620c4c0ad5a3bd26838cf5ef6ed5f32 to your computer and use it in GitHub Desktop.
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 Handler = (Int) -> Bool | |
| var handlers: [Handler] = [] | |
| func iterate(x: Int) { | |
| // commented code stops at first true | |
| // print(handlers.reduce(false, { $0 || $1(x) })) | |
| // this one iterates over all collection | |
| print(handlers.reduce(false, { $1(x) || $0 })) | |
| } | |
| handlers.append { (value) -> Bool in | |
| print(value) | |
| return value == 2 | |
| } | |
| handlers.append { (value) -> Bool in | |
| print(value) | |
| return value == 3 | |
| } | |
| handlers.append { (value) -> Bool in | |
| print(value) | |
| return value == 4 | |
| } | |
| iterate(x: 2) | |
| iterate(x: 3) | |
| iterate(x: 4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment