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是 ...
随机推荐
- C++ 引用、指针
一.引用 1.引用的作用:给变量起一个别名,是c++对c的扩充.原名和别名有相同的地址,根本上就是同一个东西,只是名字不一样.c++的引用机制主要是为了用作函数参数,增强函数传递数据的能力,比如swa ...
- Linux 的 Spinlock 在 MIPS 多核处理器中的设计与实现
引言 随着科技的发展,尤其是在嵌入式领域,高性能.低功耗的处理器成为众多厂商追逐的目标,但是由于技术和工艺的瓶颈,试图在单核处理器上达到这样的目标变得越发困难,于是人们提出了多核处理器的概念.多核处理 ...
- 五、面向切面的spring(1)
spring的依赖注入看完了,接下来是spring中与DI一并重要的AOP了,开始吧,GO. 在软件开发中,散布于应用中多处的功能被称为横切发关注点,通常来讲,这些横切关注点从概念上市与应用的业务逻辑 ...
- 笔试算法题(32):归并算法求逆序对 & 将数组元素转换为数组中剩下的其他元素的乘积
出题:多人按照从低到高排成一个前后队列,如果前面的人比后面的高就认为是一个错误对: 例如:[176,178,180,170,171]中的错误对 为 <176,170>, <176,1 ...
- HTTP实验:分别使用httpd-2.2和httpd-2.4实现
1. 需求描述 1.建立httpd服务,要求: (1) 提供两个基于名称的虚拟主机: www1.stuX.com,页面文件目录为/web/vhosts/www1:错误日志为/var/log/httpd ...
- PHP程序员必须知道的两种日志
前言 作为一名程序员,比码代码还重要那么一点点的东西就是日志的分析和查询.下面列出常见日志及设置方法. php-fpm 慢日志 php慢日志需要在php-fpm.conf设置,如果使用源码包安装默认请 ...
- django找不到报错 ‘zsh: command not found: django-admin.py’
>>python -m django --version >> 1.11.4 说明django安装成功,但是django-admin 报错 ‘zsh: command not ...
- 集训第五周动态规划 F题 最大子矩阵和
Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous s ...
- 冒泡排序 and 选择排序 变量打印斐波拉契数列 and 数组打印斐波拉契数列
1 排序 1.1 冒泡排序 #include <stdio.h> int main() { ]; printf("input six int numbers:\n"); ...
- Vue如何引入icon图标
1.下载icon图标,推荐icomoon网站,里面有大量的矢量图标,也可以自定义,当然你也可以去阿里巴巴矢量图标库下载你所需要的小图标.点击进入icomoon网站点击右上角“IcoM ...