如何使用swift函数式编程
本篇内容主要讲解“如何使用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] { 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 ]() 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 ] { 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 ]() 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函数式编程”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
分享标题:如何使用swift函数式编程
网页路径:http://hbruida.cn/article/ihodop.html