繁体   English   中英

Swift3中的数组过滤器

[英]Array filter in Swift3

我有一段代码。 我不知道这段代码里面有什么内容。 任何人都可以解释一下吗?

 let wordFreqs = [("k", 5), ("a", 7), ("b", 3)]

        let res = wordFreqs.filter(
        {
            (e) -> Bool in

            if e.1 > 3 {
                return true
            } else {
                return false
            }

        }).map { $0.0 }

        print(res)

给出输出:

["k","a"]

如果我们一个接一个地使用这段代码:

let wordFreqs = [("k", 5), ("a", 7), ("b", 3)]

你从一系列元组开始

从Swift文档:

元组类型是以逗号分隔的类型列表,括在括号中。

和:

元组将多个值分组为单个复合值。 元组中的值可以是任何类型。

在这种情况下,元组是2个值的“耦合”,一个是String类型,另一个是Int类型。


        let res = wordFreqs.filter(
        {
            (e) -> Bool in

这部分在数组上应用了一个过滤器。 你可以在这里看到过滤器的闭包需要一个元素e(所以,在我们的例子中,是一个元组),然后返回一个Bool。 使用'filter'函数,返回true表示保留值,而返回false表示将其过滤掉。


            if e.1 > 3 {
                return true
            } else {
                return false
            }

e.1语法返回索引1处元组的值。因此,如果索引1(第二个)的元组值超过3,则过滤器返回true(因此将保留元组); 如果不是,则过滤器返回false(因此从结果中排除元组)。 此时,过滤器的结果将是[("k", 5), ("a", 7)]


        }).map { $0.0 }

map函数基于元组数组创建一个新数组:对于输入数组的每个元素($ 0),它返回索引0处的元组值。所以新数组是["k", "a"]


        print(res)

这会将结果打印到控制台。


这些函数(滤波器,映射,缩减等)在函数式编程中非常常见。 它们通常使用点语法链接,例如, [1, 2, 3].filter({ }).map({ }).reduce({ })

// this creates an array of tuples
let wordFreqs = [("k", 5), ("a", 7), ("b", 3)]

let res = wordFreqs.filter {
    (e) -> Bool in

    // this filters the array
    // it removes any items that have the second part of the tuple
    // of 3 or less
    if e.1 > 3 {
        return true
    } else {
        return false
    }
}.map {
    // this "maps" the array and returns the first part of the tuple
    $0.0
}

print(res)

注意......如果我写这篇文章,我会把它缩短为......

let res = wordFreqs.filter { $0.1 > 3 }
                   .map { $0.0 }

wordFreqstuple数组。

元组

元组是表示为一个值的零个或多个值的组。

例如 (“John”,“Smith”)拥有一个人的名字和姓氏。 您可以使用点(。)表示法后跟值的索引来访问内部值:

var person = ("John", "Smith")

var firstName = person.0 // John
var lastName = person.1 // Smith

现在在你的情况下你有一个类型(String, Int)元组和你正在比较e.1 > 3 filter (这里e是保持带有filter的数组迭代的元组值)意味着second(Int)值更大比3。

之后,您在filter结果上使用map ,并从元组重新调整String($0.0)

    //array of tuples
    let wordFreqs = [("k", 5), ("a", 7), ("b", 3)]

    let res = wordFreqs.filter(
    {
        (e) -> Bool in
        //Comparing the Second Int value of tuple in filter
        if e.1 > 3 {
            return true
        } else {
            return false
        }

    })
    //Mapping the result of filter
    .map { 
         //return the String from the tuple
         $0.0 
    }

您的e对象表示(String, int)类型。 正如你在[("k", 5), ("a", 7), ("b", 3)]中的数组中看到的那样。

首先,您使用的是filter方法,这就是您必须返回truefalse值的原因。 在这种情况下,您检查e.1 (表示int值)是否大于3,否则返回false。 在所有这个过程之后, filter方法返回(String,int)对象的过滤数组。

下一步是map功能。 在你的情况下很简单,它只是将所有数组值映射到元组的第一个对象(String,int)。

为了更好地理解您的代码可能如下所示:

let filteredArray = wordFreqs.filter
({
    (e) -> Bool in
    return e.1 > 3
})// the filteredArray is [("k", 5), ("a", 7)]


let finalValue = filteredArray.map { 
  $0.0
}// here you're creating a new array with String. $0 represents object from filteredArray

print(finalValue) // ["k", "a"]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM