【发布时间】:2017-08-05 14:13:29
【问题描述】:
作为学习练习,我将 Underscore.js 的一些实用功能移植到 swift 中,我从 some 开始,如果集合中的任何元素通过给定的集合块,则返回 true。
天真的实现:
extension Sequence {
func some(_ predicate: (Self.Iterator.Element) -> Bool) -> Bool {
return reduce(false) { $0 || predicate($1) }
}
}
然而,我注意到很多 STL 函数包括 throws 和 rethrows。一个很好的例子是 filter 函数,它的函数签名为:
func filter(_ isIncluded: (Element) throws -> Bool) rethrows -> [Element]
有人可以告诉我如何编写some 函数,同时还利用throw 和rethrows 功能吗?
【问题讨论】:
标签: ios swift functional-programming