您好,登錄后才能下訂單哦!
本篇內容主要講解“如何使用swift函數式編程”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“如何使用swift函數式編程”吧!
//統計詞頻 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]]
到此,相信大家對“如何使用swift函數式編程”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。