swift基础之函数式编程

原创
2021/04/13 11:02
阅读数 28

函数式编程

概念参考:函数式编程初探

示例

//统计词频
let non_words: Set = ["the", "and", "of", "to", "a", "i", "it", "in", "or", "is", "as", "so", "but", "be", "don't"]

//常规方式
func wordFreq(words: String) -> [Dictionary<String, Int>] {
    var wordDict : [String: Int] = [:]
    let wordList = words.split(separator: " ")
    for word in wordList {
        let lowerCaseWord = word.lowercased()
        if !non_words.contains(lowerCaseWord) {
            if let count = wordDict[lowerCaseWord] {
                wordDict[lowerCaseWord] = count + 1
            } else {
                wordDict[lowerCaseWord] = 1
            }
        }
    }
    var sortedResult = [Dictionary<String, Int>]()
    for item in wordDict.sorted { $0.1 > $1.1 } {
        sortedResult.append([item.key: item.value])
    }
    return sortedResult
}

let words = """
The powerful programming language that is also easy to learn Swift is a powerful and intuitive programming language for macOS iOS watchOS tvOS and beyond Writing Swift code is interactive and fun the syntax is concise yet expressive and Swift  includes  modern features developers love Swift code is safe by design yet also produces software that runs lightning-fast Swift is the result of the latest research on programming languages combined with decades of experience building Apple platforms Named parameters are expressed in a clean syntax that makes  APIs
"""
let ret = wordFreq(words: words)
print(ret)
//[["swift": 5], ["programming": 3], ["that": 3], ["syntax": 2], ["code": 2], ["yet": 2], ["powerful": 2], ["also": 2], ["language": 2], ["beyond": 1], ["clean": 1], ["result": 1], ["features": 1], ["on": 1], ["produces": 1], ["concise": 1], ["latest": 1], ["developers": 1], ["for": 1], ["design": 1], ["experience": 1], ["with": 1], ["parameters": 1], ["building": 1], ["macos": 1], ["combined": 1], ["languages": 1], ["lightning-fast": 1], ["tvos": 1], ["interactive": 1], ["safe": 1], ["fun": 1], ["apis": 1], ["easy": 1], ["decades": 1], ["makes": 1], ["by": 1], ["includes": 1], ["expressed": 1], ["love": 1], ["are": 1], ["intuitive": 1], ["named": 1], ["research": 1], ["writing": 1], ["watchos": 1], ["expressive": 1], ["apple": 1], ["platforms": 1], ["learn": 1], ["ios": 1], ["software": 1], ["modern": 1], ["runs": 1]]

//函数式编程方式
func wordFreqTwo(words: String) -> [Dictionary<String, Int>] {
    var wordDict: [String: Int] = [:]
    words.split(separator: " ")
        .map{ $0.lowercased() }
        .filter{ !non_words.contains($0) }
        .forEach{ (word) in
            wordDict[word] = (wordDict[word] ?? 0) + 1
        }
    var sortedResult = [Dictionary<String, Int>]()
    wordDict.sorted{ $0.1 > $1.1 }
        .forEach{ (word) in
            sortedResult.append([word.key: word.value])
        }
    return sortedResult
}

let retTwo = wordFreqTwo(words: words)
print(retTwo)
//[["swift": 5], ["programming": 3], ["that": 3], ["syntax": 2], ["also": 2], ["language": 2], ["powerful": 2], ["yet": 2], ["code": 2], ["love": 1], ["safe": 1], ["watchos": 1], ["ios": 1], ["combined": 1], ["tvos": 1], ["runs": 1], ["are": 1], ["experience": 1], ["design": 1], ["by": 1], ["expressed": 1], ["features": 1], ["result": 1], ["parameters": 1], ["expressive": 1], ["building": 1], ["fun": 1], ["software": 1], ["on": 1], ["research": 1], ["writing": 1], ["produces": 1], ["with": 1], ["clean": 1], ["platforms": 1], ["named": 1], ["concise": 1], ["latest": 1], ["makes": 1], ["interactive": 1], ["languages": 1], ["easy": 1], ["for": 1], ["lightning-fast": 1], ["beyond": 1], ["decades": 1], ["apis": 1], ["learn": 1], ["includes": 1], ["modern": 1], ["apple": 1], ["macos": 1], ["intuitive": 1], ["developers": 1]]
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部