欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

如何使用swift函數(shù)式編程

本篇內(nèi)容主要講解“如何使用swift函數(shù)式編程”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“如何使用swift函數(shù)式編程”吧!

站在用戶的角度思考問題,與客戶深入溝通,找到瑪多網(wǎng)站設計與瑪多網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設計與互聯(lián)網(wǎng)技術結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:成都網(wǎng)站制作、成都網(wǎng)站建設、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、域名與空間、網(wǎng)站空間、企業(yè)郵箱。業(yè)務覆蓋瑪多地區(qū)。

示例

//統(tǒng)計詞頻
let non_words: Set = ["the", "and", "of", "to", "a", "i", "it", "in", "or", "is", "as", "so", "but", "be", "don't"]

//常規(guī)方式
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]]

//函數(shù)式編程方式
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函數(shù)式編程”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關內(nèi)容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!

文章標題:如何使用swift函數(shù)式編程
當前鏈接:http://chinadenli.net/article8/ihodop.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供微信小程序軟件開發(fā)企業(yè)網(wǎng)站制作關鍵詞優(yōu)化網(wǎng)站營銷

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應式網(wǎng)站建設