Swift:使用闭包简化功能 [英] Swift: Reduce Function with a closure

查看:83
本文介绍了Swift:使用闭包简化功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我努力理解的代码:

Below is the code that I'm struggling to understand:

  let rectToDisplay = self.treasures.reduce(MKMapRectNull)  { //1
    (mapRect: MKMapRect, treasure: Treasure) -> MKMapRect in //2

     let treasurePointRect = MKMapRect(origin: treasure.location.mapPoint, size: MKMapSize(width: 0, height: 0)) //3

       return MKMapRectUnion(mapRect, treasurePointRect)
    }

我对reduce函数的理解是:

My understanding of reduce function is:

var people [] // an array of objects
var ageSum = 0
ageSum = people.reduce(0) { $0 + $1.age}

//(0) = initial value
//$0 = running total
//$1 = an object in an array

我对闭包的理解是:

{ (params) -> returnType in
  statements
}

我对最上面的代码的理解是:

My understanding for the code on top is:

//1 =归约函数的初始值设置为(MKMapRectNull)

//1 = initial value for the reduce function is set to (MKMapRectNull)

//2 =而不是运行总计和数组中的对象,而是使用带有两个返回MKMapRect的参数的闭包来传递:

//2 = Instead of a running total and an object in an array, a closure is passed in with two arguments that returns a MKMapRect:

(mapRect: MKMapRect, treasure: Treasure) -> MKMapRect 

//3 =这就是我遇到的问题.使用两个参数origin: treasure.location.mapPointsize: MKMapSize(width: 0, height: 0)

//3 = This is where I'm stuck. A struct, MKMapRect, is called with two parameters origin: treasure.location.mapPoint, and size: MKMapSize(width: 0, height: 0)

问题1: MKMapSize如何计算传入的值是否为0,0?如何获取后续值并将其添加?

Question 1: How will MKMapSize calculate if the values passed in are 0,0 ? How is it getting the subsequent values and add them?

问题2:当此行返回到//2 closure return MKMapRectUnion(mapRect, treasurePointRect)时,它如何成为运行总计,以及如何知道进入self.treasures的下一个元素? >

Question 2: When this line is returned to //2 closure return MKMapRectUnion(mapRect, treasurePointRect) how does it become the running total and how does it know to get to the next element of self.treasures ?

推荐答案

答案2:

第二次(第三,第四次)对闭包的调用的第一个参数是上次对闭包的调用的 result 输出.唯一的例外是第一次调用,没有以前的调用可以继承,这就是为什么reduce将值0作为第二个参数的原因-这是馈入第一次调用的特殊值.

The first argument of the second (third, fourth) call to the closure is the result output by the previous call to the closure. The only exception is the first call, which has no previous call to inherit from, which is why reduce takes the value 0 as the second argument - it's a special value to feed into the first call.

让我们想象一下以下情况-您有一个数字数组:

Let's imagine the following scenario - you have an array of numbers:

let a = [1,2,3]

您想使用reduce求和:

And you want to find the sum using reduce:

let sum = reduce(a,0) { (accumulator, nextValue) in accumulator + nextValue }

让我们看看每次通话会发生什么:

So let's see what happens on each call:

call#    acc    next    result
1        0      1       0+1 -> 1       # the zero is from reduce's second arg
2        1      2       1+2 -> 3       # the 1 is from the previous result
3        3      3       3+3 -> 6

现在我们已经用完了要处理的元素,所以我们返回最终值6,实际上是1,2和3的总和.

Now we have run out of elements to process, so we return the final value 6, which is indeed the sum of 1,2 and 3

让我们想象一个稍微复杂的场景-向字符串添加一系列格式化的数字值.这是减少:

Let's imagine a slightly more complex scenario - adding a series of formatted number values to a string. Here's the reduce:

let sum = reduce(a,"") { (accumulator, nextValue) in accumulator + nextValue }

看起来几乎相同,但是您会注意到 initial 设置为""而不是0.运算符+现在组合了一个String和一个Int.

It looks almost identical, but you'll note that initial is set to "" instead of 0. The operator + now combines a String and an Int.

call#    acc    next    result
1        ""      1      ""+1 -> "1"
2        "1"     2      "1"+2 -> "12"
3        "12"    3      "12"+3 -> "123"

我们完成了,可以打印出字符串了.

And we are done, and can print the string out, or whatever.

现在让我们来看您的第一个问题!

Now let's look at your first question!

在地图上添加一系列点类似于(但不完全相同)将数字相加;它更接近于字符串示例,在该示例中,累加器的类型(字符串/映射)与数组元素(整数/宝藏位置)不同.

Adding a series of points to a map is analogous to (but not quite the same as) adding numbers together; it's closer to the string example, where the accumulator has a different type (string/map) from the array elements (integer/treasure location).

您当然可以在空白地图上添加城堡灰色头骨",然后在上面带有灰色头骨的地图上添加阴影湾",然后在城堡灰色头骨"的地图上添加宝藏位置",

You can certainly add "castle grey skull' to an empty map, and then add "shady cove" to a map with grey skull on it, and then add 'treasure location' to a map with "castle grey skull" and "shady cove" already drawn on it. Then you are done, and you can start the treasure hunt.

您的初始值不是零(或"),而是空的映射.

Your initial value is not zero, (or "") but the empty map.

添加新点(矩形)时,会将其添加到上面具有上一个点的地图中,而不是直接将其添加到上一个点中.

When a new point (rect) is added, it's being added to the map that has the previous point on it, it's not being added to the previous point directly.

上一点保持不变,不变!它只是附近有一个新的地图标记,可以与他人分享故事.

The previous point stays on the map, unchanged! it just has a new map marker nearby, to share stories with.

关于您的零尺寸矩形如何执行任何操作的问题,我认为这与一点相同.

As to your question as to how a size zero rectangle can do anything, I assume that's just the same as a point.

这篇关于Swift:使用闭包简化功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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