什么时候使用forEach(_ :)而不是in? [英] When to use forEach(_:) instead of for in?
问题描述
forEach(_:)
实例方法:$ b
以相同的
顺序调用序列中每个元素的给定闭包for-in循环。
然而,改编自序列概述:
序列是一个值列表,一个在
时间。 循环遍历序列
的元素的最常用的方法是使用 for-in循环。
这意味着对于 forEach(_:)或迭代序列>:
let closedRange = 1 ... 3
for closedRange {print(element )} // 1 2 3
closedRange.forEach {print($ 0)} // 1 2 3
或(数组):
let array = [1,2,3]
循环中使用它而不是
for array {print(element)} // 1 2 3
array.forEach {print($ 0)} // 1 2 3
$ c
$ b会给出相同的输出。
为什么
forEach (_:)
甚至存在?即在有什么好处?从性能角度来看它们是一样的吗?
作为一个假设,它可能是一个语法糖,特别是在使用函数式编程的时候。 / p>
解决方案
对于每个都没有性能优势。事实上,如果你看源代码< forEach 函数实际上简单地在$ c $>中为 - 执行
C>。对于发布版本来说,这个函数的性能开销并不重要,但是对于
-在中简单地使用
-是不重要的,尽管对于调试版本它会导致可观察的性能影响。
forEach
的主要优点是在函数式编程时实现,您可以将它添加到函数调用,而不必将以前的结果保存到一个单独的变量中,如果在>语法。所以,而不是:
let objects = array.map {...}
.filter {... }
$ b $对于对象中的对象{
...
}
您可以保留在函数式编程模式中:
array.map {...}
.filter {...}
.forEach {...}
结果是功能性代码更简洁,语法更少。
FWIW, Array , Dictionary 和序列都提醒我们引入的限制
forEach
,即:
$ b
- 在
不能使用
break
或continue
语句退出当前的
调用body 关闭或跳过后续的调用。
主体中使用
闭包将只从return
语句
退出,而不是从任何外层作用域中跳转到body
,而不会跳过
后续调用。
As documented in both Array and Dictionary
forEach(_:)
Instance methods:Calls the given closure on each element in the sequence in the same order as a for-in loop.
Nevertheless, adapted from Sequence Overview:
A sequence is a list of values that you can step through one at a time. The most common way to iterate over the elements of a sequence is to use a for-in loop.
Implying that iterating sequence by
forEach(_:)
orfor in
:let closedRange = 1...3 for element in closedRange { print(element) } // 1 2 3 closedRange.forEach { print($0) } // 1 2 3
Or (Array):
let array = [1, 2, 3] for element in array { print(element) } // 1 2 3 array.forEach { print($0) } // 1 2 3
Would gives the same output.
Why
forEach(_:)
even exist? i.e what is the benefit of using it instead of thefor in
loop? would they be the same from performance point view?As an assumption, it could be a syntactic sugar especially when working with functional programming.
解决方案There is no performance benefit offered by
forEach
. In fact, if you look at the source code, theforEach
function actually simply performingfor
-in
. For release builds, the performance overhead of this function over simply usingfor
-in
yourself is immaterial, though for debug builds, it results in an observable performance impact.The main advantage of
forEach
is realized when you are doing functional programming, you can add it to a chain of functional calls, without having to save the prior result into a separate variable that you'd need if you usedfor
-in
syntax. So, instead of:let objects = array.map { ... } .filter { ... } for object in objects { ... }
You can instead stay within functional programming patterns:
array.map { ... } .filter { ... } .forEach { ... }
The result is functional code that is more concise with less syntactic noise.
FWIW, the documentation for Array, Dictionary, and Sequence all remind us of the limitations introduced by
forEach
, namely:
You cannot use a
break
orcontinue
statement to exit the current call of thebody
closure or skip subsequent calls.Using the
return
statement in thebody
closure will exit only from the current call tobody
, not from any outer scope, and won't skip subsequent calls.
这篇关于什么时候使用forEach(_ :)而不是in?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文