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

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

问题描述

Array字典 forEach(_:) 实例方法:

<块引用>

在相同的序列中的每个元素上调用给定的闭包作为 for-in 循环排序.

尽管如此,改编自序列概述:

<块引用>

一个序列是一个值列表,你可以在一个时间.迭代序列元素的最常见方式是使用 for-in 循环.

通过 forEach(_:)for in 暗示迭代序列:

让 closedRange = 1...3for element in closedRange { print(element) }//1 2 3closedRange.forEach { print($0) }//1 2 3

或(数组):

让数组 = [1, 2, 3]for element in array { print(element) }//1 2 3array.forEach { print($0) }//1 2 3

会给出相同的输出.

为什么 forEach(_:) 甚至存在?即使用它代替 for in 循环有什么好处?从性能的角度来看,它们会相同吗?

假设,它可能是一种语法糖,尤其是在使用函数式编程时.

解决方案

forEach 没有提供任何性能优势.事实上,如果你查看源代码forEach 函数实际上只是执行for-in.对于发布版本,此函数的性能开销比简单地使用 for-in 自己无关紧要,但对于调试版本,它会导致可观察到的性能影响.

forEach 的主要优点是在您进行函数式编程时实现,您可以将其添加到函数调用链中,而无需将先前的结果保存到您想要的单独变量中如果您使用 for-in 语法,则需要.所以,而不是:

let objects = array.map { ... }.筛选 { ... }对于对象中的对象{...}

你可以改用函数式编程模式:

array.map { ... }.筛选 { ... }.forEach { ... }

结果是功能代码更简洁,语法噪音更少.

FWIW,Array 的文档,字典Sequence 都提醒我们forEach 引入的限制,即:

<块引用>

  1. 您不能使用 breakcontinue 语句退出当前调用 body 闭包或跳过后续调用.

  2. 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(_:) 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(_:) 而不是 for in?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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