什么时候使用forEach(_ :)而不是in? [英] When to use forEach(_:) instead of for in?

查看:232
本文介绍了什么时候使用forEach(_ :)而不是in?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如 Array 字典 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


$ b

会给出相同的输出。

为什么 forEach (_:)甚至存在?即在循环中使用它而不是有什么好处?从性能角度来看它们是一样的吗?

作为一个假设,它可能是一个语法糖,特别是在使用函数式编程的时候。
/ p>

解决方案

对于每个都没有性能优势。事实上,如果你看源代码< forEach 函数实际上简单地在中为 - 执行 C>。对于发布版本来说,这个函数的性能开销并不重要,但是对于 - 在中简单地使用是不重要的,尽管对于调试版本它会导致可观察的性能影响。

forEach 的主要优点是在函数式编程时实现,您可以将它添加到函数调用,而不必将以前的结果保存到一个单独的变量中,如果在
- >语法。所以,而不是:

  let objects = array.map {...} 
.filter {... }
$ b $对于对象中的对象{
...
}

您可以保留在函数式编程模式中:

  array.map {...} 
.filter {...}
.forEach {...}

结果是功能性代码更简洁,语法更少。

FWIW, Array Dictionary 序列都提醒我们引入的限制 forEach ,即:
$ b



  1. 不能使用 break continue 语句退出当前的
    调用 body 关闭或跳过后续的调用。

  2. 主体中使用 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(_:) or for 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 the for 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, the forEach function actually simply performing for-in. For release builds, the performance overhead of this function over simply using for-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 used for-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:

  1. You cannot use a break or continue statement to exit the current call of the body closure or skip subsequent calls.

  2. Using the return statement in the body closure will exit only from the current call to body, not from any outer scope, and won't skip subsequent calls.

这篇关于什么时候使用forEach(_ :)而不是in?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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