繁体   English   中英

当我在 Swift 中调用它时如何链接函数?

[英]How can I chain functions when i call it in Swift?

我是swift的初学者,我正在做练习来学习。

我必须过滤任何偶数,对数组进行排序,map 将它们字符串化并每行打印一个结果。

问题是,当我调用 function 时,我无法链接闭包,它只打印第三个 function。

import Foundation

let luckyNumbers = [7, 4, 38, 21, 16, 15, 12, 33, 31, 49]

func doImportantWorkChain (first: ([Int]) -> [Int], second: ([Int]) -> [Int], third: ([Int]) -> [String]) {
    first(luckyNumbers)
    second(luckyNumbers)
    third(luckyNumbers)

    third(luckyNumbers).forEach {
        print($0)
    }
}

doImportantWorkChain { number in
    return number.filter {
        return $0.isMultiple(of: 2)
    }
} second: { number in
    return number.sorted {
        return $0 < $1
    }
} third: { number in
    return number.map {
       ("\($0) is the lucky number")
    }
}

Output

7 is the lucky number
4 is the lucky number
38 is the lucky number
21 is the lucky number
16 is the lucky number
15 is the lucky number
12 is the lucky number
33 is the lucky number
31 is the lucky number
49 is the lucky number

Output 我必须得到

4 is the lucky number
12 is the lucky number
16 is the lucky number
38 is the lucky number

我知道我必须使用: luckyNumbers.first { }.second { }

当我尝试这样写时 Xcode 给我错误:类型值 '(([Int]) -> [Int], ([Int]) -> [Int], ([Int]) -> [String]) -> ()' 没有成员 'first'

所以我先擦除并给我这个错误:

“.”后的预期成员名称

顶级语句不能以闭包表达式开头

“(_) -> _”类型的值没有成员“second”

doImportantWorkChain.first{ number in
    return number.filter {
        return $0.isMultiple(of: 2)
    }
}.second{ number in
    return number.sorted {
        return $0 < $1
    }
}.third{ number in
    return number.map {
       ("\($0) is the lucky number")
    }
}

当我在 function 中调用它们时,我也尝试将其链接起来,但也不起作用。

正如第一个答案所建议的那样,问题是闭包的结果应该在下一个闭包中使用。

最快获取您要找的output的方法

func doImportantWorkChain(first: ([Int]) -> [Int],
                          second: ([Int]) -> [Int],
                          third: ([Int]) -> [String]) {

    third(second(first(luckyNumbers))).forEach {
        print($0)
    }
}

然而,这并不是真正的将函数chaining在一起,它只是在做第一个答案,只是减少了你的代码行,但并没有真正的升级。

获得您正在寻找的 output 的正确方法 (IMO)

我相信其中一位评论者建议的链接是直接在luckyNumbers上使用过滤器、排序和 map 函数以获得所需的 output:

luckyNumbers
    .filter { $0.isMultiple(of: 2) }
    .sorted() { return $0 < $1 }
    .map { ("\($0) is the lucky number") }
    .forEach { print($0) }

一种链接方式,会给你想要的 output 但我不知道你为什么会用这个

如果你考虑你需要做什么,为了实现某种链接,你需要创建你自己的higher order函数,如filter, map etc ,具有与你的闭包相同的签名,然后执行闭包。

我通过为Int arrays 创建一个extension来做到这一点

extension Array where Element == Int
{
    func apply(_ operation: ([Int]) -> [Int]) -> [Int]
    {
        return operation(self)
    }
    
    func apply(_ operation: ([Int]) -> [String]) -> [String]
    {
        return operation(self)
    }
}

然后你可以实现链接并获得你想要的结果

func doImportantWorkChain(first: ([Int]) -> [Int],
                          second: ([Int]) -> [Int],
                          third: ([Int]) -> [String]) {
    
    luckyNumbers
        .apply(first)
        .apply(second)
        .apply(third)
        .forEach { print($0) }
}

同样,这可能是锻炼大脑和探索语言及其功能的好练习,但我认为您永远不会真正使用它。

以上三种方式都会给你这个 output:

4 is the lucky number
12 is the lucky number
16 is the lucky number
38 is the lucky number

您没有将先前函数的结果用于下一个。

如果你这样做,那么你最终会在third(luckyNumbers).forEach行上出现错误,因为 function 预期 [Int] 并且你将从third(luckyNumbers)调用中将 [String] 作为 output 传递。

要使用此代码,您可以进行以下修改并运行

 let luckyNumbers = [7, 4, 38, 21, 16, 15, 12, 33, 31, 49]

 func doImportantWorkChain (first: ([Int]) -> [Int], second: ([Int]) -> 
    [Int], third: ([Int]) -> [String]) {
    let first_result = first(luckyNumbers)
    let second_result = second(first_result)
    let third_result = third(second_result)

    for result in third_result {
      print(result)
    }
 }

doImportantWorkChain { number in
  return number.filter {
      return $0.isMultiple(of: 2)
  }
} second: { number in
  return number.sorted {
      return $0 < $1
  }
} third: { number in
  return number.map {
     ("\($0) is the lucky number")
  }
}

暂无
暂无

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

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