Swift 函数Count,Filter,Map,Reduce
原创Blog,转载请注明出处
blog.csdn.net/hello_hwc
前言:和OC不同,Swift有非常多全局的函数,这些全局函数对简化代码来说非常实用。眼下Swift出到了2.0,只是我这篇文章还是用Swift1.2写的演示样例代码。
Count-统计数量
文档
func count<T : _CollectionType>(x: T) -> T.Index.Distance
Description
Return the number of elements in x.
O(1) if T.Index is RandomAccessIndexType; O(N) otherwise.
演示样例
let arr = [1,2,3,4]
let dic = [1:2,"a":"b"]
let str = "Wenchenhuang"
println(count(arr))//4
println(count(dic))//2
println(count(str))//12
Filter-条件过滤
文档
func filter<S : SequenceType>(source: S, includeElement: (S.Generator.Element) -> Bool) -> [S.Generator.Element]
Description
Return an Array containing the elements of source, in order, that satisfy the predicate includeElement.
演示样例-过滤长度大于4的字符串
let array = ["Wen","Chen","Huang"]
let filteredArray = filter(array, { (element:String) -> Bool in
return count(element)>4;
})
println(filteredArray)
也能够简化
let filteredArray = filter(array) {count($0)>4}
Map - 映射集合类型,返回数组
文档
func map<C : CollectionType, T>(source: C, transform: (C.Generator.Element) -> T) -> [T]
Description
Return an Array containing the results of mapping transform over source.
演示样例
let array = ["Wen","Chen","Huang"]
let mapedAray = map(array, { (element:String) -> Int in
return count(element)
})
println(mapedAray) //[3,4。5]
相同能够简化
let mapedAray = map(array){count($0)}
Reduce - 把数组结合到一起
文档
func reduce<S : SequenceType, U>(sequence: S, initial: U, combine: @noescape (U, S.Generator.Element) -> U) -U
Description
Return the result of repeatedly calling combine with an accumulated value initialized to initial and each element of `sequence`, in turn.
演示样例
let array = ["Wen","Chen","Huang"]
let reduceResult = reduce(array, "Hello ") { (originValue:String, element:String) -> String in
return originValue + element;
}
println(reduceResult) //Hello WenChenHuang
能够简化
let reduceResult = reduce(array, "Hello ") { $0 + $1}
进一步简化
let reduceResult = reduce(array, "Hello ",+)
Swift 函数Count,Filter,Map,Reduce的更多相关文章
- Python内置函数之filter map reduce
Python内置函数之filter map reduce 2013-06-04 Posted by yeho Python内置了一些非常有趣.有用的函数,如:filter.map.reduce,都是对 ...
- Python2.7学习笔记-定义函数、filter/map/reduce/lambda
我把写的代码直接贴在下面了,注释的不是很仔细,主要是为了自己复习时方便查找,并不适合没有接触过python的人看,其实我也是初学者. #定义函数 def my_abs(x): if x>=0: ...
- Python学习(五)函数 —— 内置函数 lambda filter map reduce
Python 内置函数 lambda.filter.map.reduce Python 内置了一些比较特殊且实用的函数,使用这些能使你的代码简洁而易读. 下面对 Python 的 lambda.fil ...
- Python之匿名函数(filter,map,reduce)
参考博客:Python匿名函数详解--http://blog.csdn.net/csdnstudent/article/details/40112803 Python内建函数之——filter,map ...
- python之有用的3个内置函数(filter/map/reduce)
这三个内置函数还是非常有用的,在工作中用的还不少,顺手,下面一一进行介绍 1.filter 语法:filter(function,iterable) 解释:把迭代器通过function函数进行过滤出想 ...
- Python 函数lambda(), filter(), map(), reduce()
1 filter filter(function, sequence):对sequence中的item依次执行function(item),将执行结果为True的item组成一个List/String ...
- Swift函数编程之Map、Filter、Reduce
在Swift语言中使用Map.Filter.Reduce对Array.Dictionary等集合类型(collection type)进行操作可能对一部分人来说还不是那么的习惯.对于没有接触过函数式编 ...
- 高阶函数 filter map reduce
const app=new Vue({ el:'#app', data:{ books:[{ id:1, name:"算法导论", data: '2006-1', price:39 ...
- Python经常使用内置函数介绍【filter,map,reduce,apply,zip】
Python是一门非常简洁,非常优雅的语言,其非常多内置函数结合起来使用,能够使用非常少的代码来实现非常多复杂的功能,假设相同的功能要让C/C++/Java来实现的话,可能会头大,事实上Python是 ...
随机推荐
- 数据库系统概论(1)——Chap. 1 Introduction
数据库系统概论--Introduction 一.数据库的4个基本概念 数据(data):数据是数据库中存储的基本单位.我们把描述事物的符号记录称为数据.数据和关于数据的解释是不可分的,数据的含义称为数 ...
- 整合Activiti Modeler到业务系统(或BPM平台)
http://www.kafeitu.me/activiti/2013/03/10/integrate-activiti-modeler.html 1. 为什么要整合 Activiti 5.10版本把 ...
- python根据日期返回星期
import time #定义:timedate为时间戳def formattime(timedate,s="%Y-%m-%d %H:%M:%S"): return ...
- 第3节 mapreduce高级:5、6、通过inputformat实现小文件合并成为sequenceFile格式
1.1 需求 无论hdfs还是mapreduce,对于小文件都有损效率,实践中,又难免面临处理大量小文件的场景,此时,就需要有相应解决方案 1.2 分析 小文件的优化无非以下几种方式: 1. 在数据 ...
- 开源敏捷测试管理& 开源BUG跟踪管理软件itest(爱测试) V3.3.0隆重发布
v3.3.0 下载地址 :itest下载 码云源码地址 https://gitee.com/itestwork/itest 开源中国 itest项目地址 https://www.oschina. ...
- [Python3网络爬虫开发实战] 1.3.2-Beautiful Soup的安装
Beautiful Soup是Python的一个HTML或XML的解析库,我们可以用它来方便地从网页中提取数据.它拥有强大的API和多样的解析方式,本节就来了解下它的安装方式. 1. 相关链接 官方文 ...
- 简述HTTP报文请求方法和状态响应码
1. Method 请求方法,表明客户端希望服务器对资源执行的动作: 1.1 GET 向服务器请求资源. 1.2 HEAD 和GET方法的行为类似,但服务器在响应中只返回首部,不会返回实体的主体部分. ...
- *** 红包书用法 及 ubuntu全局配置
使用教程 http://go.wasai.org/sswiki https://home.maysoul.com/wiki/doku.php?id=shadowsocks ubuntu使用教程 htt ...
- vue项目编译配置 用于结合Django项目
- Linux下安装SVN,仓库创建,用户权限管理
Exported from Notepad++ Linux下安装SVN,仓库创建,用户权限管理 1.SVN安装 Ubuntu系统下安装:sudoapt-getinstallsubv ...