首页 > 编程语言 >swift 函数编程

swift 函数编程

时间:2023-03-10 15:32:51浏览次数:32  
标签:them 函数 编程 let words wordDict swift clearnedNames String


读入一个文本,确定所有单词的使用频率并从高到底排序,打印出所有单词以及其频率列表

先不解决频率问题 先看下不使用高阶函数

//: A UIKit based Playground for presenting user interface

import UIKit


//去掉一些语气次 不加入计算
let NON_WORDS:Set = ["the","and","of","to","a","i","it","in","or","is","as","so","but","be"]
//传统解决方法
func wordFreq(words:String)->[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
}
}
}
return wordDict;
}

let words="""
There are moments in life when you miss someone so much that you just want to pick them from your dreams and hug them for real!
"""
print(wordFreq(words: words))
["there": 1, "pick": 1, "them": 2, "your": 1, "for": 1, "dreams": 1, "hug": 1, "when": 1, "real!": 1, "that": 1, "you": 2, "are": 1, "life": 1, "miss": 1, "just": 1, "moments": 1, "from": 1, "much": 1, "want": 1, "someone": 1]

使用高级函数

//: A UIKit based Playground for presenting user interface

import UIKit


//去掉一些语气次 不加入计算
let NON_WORDS:Set = ["the","and","of","to","a","i","it","in","or","is","as","so","but","be"]
//函数编程
func wordFreq(words:String)->[String:Int]{
var wordDict:[String:Int]=[:]
let wordList=words.split(separator: " ")
wordList.map {$0.lowercased()}
.filter{ !NON_WORDS.contains($0)}
.forEach{(word) in
wordDict[word]=(wordDict[word] ?? 0)+1
}
return wordDict;
}

let words="""
There are moments in life when you miss someone so much that you just want to pick them from your dreams and hug them for real!
"""
print(wordFreq(words: words))

排序很简单

  wordDict.sorted {$0.1 > $1.1}

 业务需求

假设我们有一个名字列表,其中一些条目由单个字符构成。现在的任务是,将出去单字符之外的列表内容,放在一个逗号分割的字符串里返回,且每个名字的首字母都要大写

命令式解法

传统for循环:

//: A UIKit based Playground for presenting user interface

import UIKit
let emlpyee = ["neal","s","stu","j","rich","bob","aiden","j","ethan"]

func cleanNamaes(names:Array<String>)->String{
var clearnedNames = ""
for name in names{
if name.count>1{
clearnedNames += name.capitalized + ","
}

}
clearnedNames.remove(at: clearnedNames.index(before: clearnedNames.endIndex))
return clearnedNames
}
let res=cleanNamaes(names: emlpyee)
print(res)

高阶函数

let res2=emlpyee.filter{ $0.count>1 }
.map{ $0.capitalized }
.joined(separator: ",")

结果

Neal,Stu,Rich,Bob,Aiden,Ethan

标签:them,函数,编程,let,words,wordDict,swift,clearnedNames,String
From: https://blog.51cto.com/u_14523369/6113217

相关文章

  • swift 属性观察者 willset didset 类型属性 class static
    willSet会在该值被存储之前被调用didSet会在一个新值被存储后调用如果你实现了一个willSet观察者,新的属性值会以常量形式参数传递。你可以在你的willSet实现中为这个参数......
  • swift 在实例方法中修改属性 mutating
    结构体和枚举是值类型,默认情况下。值类型不能被自身的实例方法修改。你可以在选在在func关键字前放一个mutating关键字来指定可以修改属性//:AUIKitbasedPlaygroundfor......
  • swift逃逸闭包和自动闭包
    当闭包作为一个实际参数传递给一个函数的时候,并且它会在函数返回之后调用我们就说这个闭包逃逸了,当你声明一个接受闭包作为形式参数的函数时,你可以在形式参数前写@escaping......
  • swift 高阶函数
    map对于原始集合里每一个元素,以一个变换后的元素替换之行程一个新的集合filter对于原始集合里面的每一个元素,通过判断来将其丢弃或者放进新的集合reduce对于原始集合里的每......
  • read超时,write,accept,connect超时函数,设置io阻塞模式
    1.设置I/O为阻塞模式  2.设置I/O为阻塞模式  超时函数:  3.读超时4.写超时  5.accept_timeout超时函数   主调函数 ......
  • pugixml XML格式处理库的介绍和使用(面向业务编程-格式处理)
    pugixmlXML格式处理库的介绍和使用(面向业务编程-格式处理)介绍pugixml是一个轻量级的C++XML处理库。它的特点:类似dom的界面,具有丰富的遍历/修改功能非常快速的非......
  • 组合数学课程笔记(三):生成函数
    序离散和连续的不期而遇,抽象与数分的阴阳交融。我将以加与乘的生铁铸就组合的奇迹,这世间都要把你的伟岸与光辉所传颂。$\mathfrak{GeneratingFunction}$生成函数所......
  • Python - 获取调用者的函数名称
    def_is_page(self,locator):"""判断是否到达指定页面"""caller_name=traceback.extract_stack()[-2][2]is_page=self.ele_actions(locator).exists()......
  • 开源量子计算编程框架软件介绍
    1.编程框架简介​在编程领域,软件框架是指一种抽象形式,它提供了一个具有通用功能的软件,这些功能可以由使用者编写代码来有选择的进行更改,从而提供服务于特定应用的软件。可......
  • 网络编程
    网络编程实现网络编程的三个要素:使用IP地址(准确的定位网络上的一台或多台主机);使用端口号(定位主机上的特定的应用);使用网络通信协议(可靠,高效的传输数据);IPv4地址:是一个3......