Swift 函数查找集合中与谓词匹配的第一个元素?

标签 swift collections

如果xs是一个集合,pred是一个返回Bool的闭包,是否有一个内置函数可以执行以下操作?

xs.filter(pred).first

这将获取与预测匹配的集合的第一个元素,如果没有匹配,则获取 nil 。对索引不感兴趣,而是对元素本身感兴趣。

最佳答案

不,没有,但你可以自己写一个,如下所示:

extension SequenceType {
    func first(@noescape pred: Generator.Element throws -> Bool) rethrows -> Generator.Element? {
        return try filter(pred).first
    }
}

编辑:此版本不是最佳版本,因为 filter 创建了一个全新的数组,即使只需要第一个元素。正如 Martin R 所指出的,lazy.filter 也不起作用。这对于使其与lazy一起工作是必要的:

extension CollectionType {
    func first(pred: Generator.Element -> Bool) -> Generator.Element? {
        return lazy.filter(pred).first
    }
}

因为:

  • @noescape 不能使用,因为 @noescape 意味着闭包无法转义当前函数,而将其传递给惰性 时可能会出现这种情况过滤器(在被要求之前不会评估元素 -> 必须转义谓词)
  • throws 不能使用,因为过滤器是惰性的 -> 错误不会立即抛出,而是在第一次使用时抛出,即调用 first< 时,但 first 不能抛出,因为它是一个属性。有一些关于在 Swift 的 future 版本中使用 getter(和下标)进行抛出的讨论。
  • 必须使用CollectionType,因为只有LazyCollectionType 具有first 属性。

因此,要真正使其变得懒惰并拥有所有 @noescapethrowsSequenceType,您必须使用命令式方法:

extension SequenceType {
    func first(@noescape pred: Generator.Element throws -> Bool) rethrows -> Generator.Element? {
        for elem in self where try pred(elem) {
            return elem
        }
        return nil
    }
}

关于Swift 函数查找集合中与谓词匹配的第一个元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34031754/

相关文章:

ios - 无法设置 (key_name) 用户定义的检查属性。此类与键的键值编码不兼容

php - 具有不同字段的 Symfony CollectionType

c# - 在可为空类型列表中查找索引?

oracle - 在oracle中初始化一个集合

swift - 错误 : Cannot convert value of type '(_) -> ()' to expected argument type '(() -> Void)?'

ios - 类型不匹配(Swift.Dictionary<Swift.String, Any> debugDescription : "Expected to decode Dictionary<String, Any> but found an array instead."

json - 读取本地 JSON 文件并使用它来填充 UITableView

Java - 搜索提供前几个字符的字符串集合

collections - 将Java8 IntStream收集到Deque接口(interface)中

Javascript 端口 var 测试 = { a : Infinity }