filter(_ :).first和first(where :)有什么区别? [英] What is the difference between filter(_:).first and first(where:)?

查看:330
本文介绍了filter(_ :).first和first(where :)有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下字符串数组:

Please consider the following strings array:

let strings = ["str1", "str2", "str10", "str20"]

让我们假设需要获取包含5个字符的第一个元素(字符串),我可以使用

Let's assume that what required is to get the first element (String) which contains 5 characters, I could get it by using filter(_:) as follows:

let filterString = strings.filter { $0.count == 5 }.first
print(filterString!) // str10

,但是在查看了 first(where:) 方法后,我认识到我将能够获得相同的输出:

but after reviewing the first(where:) method, I recognized that I will be able to get the same output:

let firstWhereString = strings.first(where: { $0.count == 5 })
print(firstWhereString!) // str10

那么使用一种代替另一种的好处是什么?仅仅是filter(_:)返回一个序列而first(where:)返回一个元素吗?

So what is the benefit of using one instead of the other? is it only about that the filter(_:) returns a sequence and the first(where:) returns a single element?

更新:

我注意到过滤器(_ :)花费了 5次来完成此过程,而first(where :)花费了 4次:

I noticed that the filter(_:) took 5 times to do such a process, while first(where:) took 4 times:

推荐答案

您正确地观察到 filter(_:) 返回满足谓词且

You are correct in observing that filter(_:) returns all elements that satisfy a predicate and that first(where:) returns the first element that satisfy a predicate.

因此,这给我们带来了一个更有趣的问题,即elements.filter(predicate).firstelements.first(where: predicate).

So, that leaves us with the more interesting question of what the difference is between elements.filter(predicate).first and elements.first(where: predicate).

您已经注意到它们最终都得到相同的结果.不同之处在于他们的评估策略".致电:

As you've already noticed they both end up with the same result. The difference is in their "evaluation strategy". Calling:

elements.filter(predicate).first

将急切"检查所有元素的谓词以过滤元素的完整列表,然后从过滤器列表中选择第一个元素.相比之下,调用:

will "eagerly" check the predicate against all elements to filter the full list of elements, and then pick the first element from the filterer list. By comparison, calling:

elements.first(where: predicate)

将懒惰地"对谓词进行元素检查,直到找到一个满足该谓词的谓词,然后返回该元素.

will "lazily" check the predicate against the elements until it finds one that satisfies the predicate, and then return that element.

作为第三种选择,您可以显式使用"[列表]上的视图,该视图提供通常急切的操作的懒惰实现,例如mapfilter":

As a third alternative, you can explicitly use "a view onto [the list] that provides lazy implementations of normally eager operations, such as map and filter":

elements.lazy.filter(predicate).first

这会将评估策略更改为惰性".实际上,它是如此懒惰,以至于只调用elements.lazy.filter(predicate)不会检查 any 元素的谓词.只有在此懒惰视图上急切地"评估first元素时,它才会评估足够的元素以返回一个结果.

This changes the evaluation strategy to be "lazy". In fact, it's so lazy that just calling elements.lazy.filter(predicate) won't check the predicate against any elements. Only when the first element is "eagerly" evaluated on this lazy view will it evaluate enough elements to return one result.

除了这些替代方案之间的任何技术区别之外,我想您应该使用最清楚地描述您的意图的替代方案.如果您要寻找与条件/谓词匹配的第一个元素,则first(where:)传达最佳的意图.

Separately from any technical differences between these alternatives, I'd say that you should use the one that most clearly describes your intentions. If you're looking for the first element that matches a criteria/predicate then first(where:) communicates that intent best.

这篇关于filter(_ :).first和first(where :)有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆