Skip to content

Instantly share code, notes, and snippets.

@geor-kasapidi
Created June 30, 2020 07:20
Show Gist options
  • Select an option

  • Save geor-kasapidi/c620c4c0ad5a3bd26838cf5ef6ed5f32 to your computer and use it in GitHub Desktop.

Select an option

Save geor-kasapidi/c620c4c0ad5a3bd26838cf5ef6ed5f32 to your computer and use it in GitHub Desktop.
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