为什么以及何时在Swift中将lazy与Array一起使用? [英] Why and when to use lazy with Array in Swift?

查看:101
本文介绍了为什么以及何时在Swift中将lazy与Array一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[1, 2, 3, -1, -2].filter({ $0 > 0 }).count // => 3

[1, 2, 3, -1, -2].lazy.filter({ $0 > 0 }).count // => 3

lazy添加到第二条语句的好处是什么.据我了解,使用lazy变量时,内存在使用时会初始化为该变量.在这种情况下有什么意义?

What is the advantage of adding lazy to the second statement. As per my understanding, when lazy variable is used, memory is initialized to that variable at the time when it used. How does it make sense in this context?

试图更详细地了解LazySequence的用法.我在序列上使用了mapreducefilter函数,但从未在lazy序列上使用过.需要了解为什么要使用它吗?

Trying to understand the the use of LazySequence in little more detail. I had used the map, reduce and filter functions on sequences, but never on lazy sequence. Need to understand why to use this?

推荐答案

lazy更改数组的处理方式.不使用lazy时,filter处理整个数组并将结果存储到新数组中.使用lazy时,序列或集合中的值根据需要从下游函数中按需生成.值不存储在数组中;它们只是在需要时生产的.

lazy changes the way the array is processed. When lazy is not used, filter processes the entire array and stores the results into a new array. When lazy is used, the values in the sequence or collection are produced on demand from the downstream functions. The values are not stored in an array; they are just produced when needed.

考虑这个修改后的示例,在该示例中,我使用reduce而不是count,以便我们可以打印出正在发生的事情:

Consider this modified example in which I've used reduce instead of count so that we can print out what is happening:

不使用lazy:

Not using lazy:

在这种情况下,所有项目都将首先被过滤,然后再计算任何内容.

In this case, all items will be filtered first before anything is counted.

[1, 2, 3, -1, -2].filter({ print("filtered one"); return $0 > 0 })
    .reduce(0) { (total, elem) -> Int in print("counted one"); return total + 1 }

filtered one
filtered one
filtered one
filtered one
filtered one
counted one
counted one
counted one

使用lazy:

Using lazy:

在这种情况下,reduce要求要计数的物品,filter会一直工作到找到一个物品,然后reduce会要求另一个物品,而filter会一直工作直到找到另一个物品.

In this case, reduce is asking for an item to count, and filter will work until it finds one, then reduce will ask for another and filter will work until it finds another.

[1, 2, 3, -1, -2].lazy.filter({ print("filtered one"); return $0 > 0 })
    .reduce(0) { (total, elem) -> Int in print("counted one"); return total + 1 }

filtered one
counted one
filtered one
counted one
filtered one
counted one
filtered one
filtered one


何时使用lazy:


When to use lazy:

选项-单击lazy给出了以下说明:

option-clicking on lazy gives this explanation:

讨论中针对lazy:

在链接操作时使用lazy属性:

Use the lazy property when chaining operations:

  1. 防止中间操作分配存储空间

  1. to prevent intermediate operations from allocating storage

当您只需要最终收藏的一部分以避免不必要的计算时

when you only need a part of the final collection to avoid unnecessary computation

我要添加第三个:

当您希望下游流程更快开始并且不必等待上游流程先完成所有工作

when you want the downstream processes to get started sooner and not have to wait for the upstream processes to do all of their work first

例如,如果要搜索第一个正数Int,则要在filter之前使用lazy,因为一旦找到一个正数Int,搜索就会停止,并且会保存不必过滤整个数组,从而省去了为过滤后的数组分配空间的麻烦.

So, for example, you'd want to use lazy before filter if you were searching for the first positive Int, because the search would stop as soon as you found one and it would save filter from having to filter the whole array and it would save having to allocate space for the filtered array.

对于第三点,假设您有一个程序正在使用该范围内的filter显示1...10_000_000范围内的质数.您宁愿在找到素数时就显示素数,而不必等到计算完所有素数后再显示任何东西.

For the 3rd point, imagine you have a program that is displaying prime numbers in the range 1...10_000_000 using filter on that range. You would rather show the primes as you found them than having to wait to compute them all before showing anything.

这篇关于为什么以及何时在Swift中将lazy与Array一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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