原创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的更多相关文章

  1. Python内置函数之filter map reduce

    Python内置函数之filter map reduce 2013-06-04 Posted by yeho Python内置了一些非常有趣.有用的函数,如:filter.map.reduce,都是对 ...

  2. Python2.7学习笔记-定义函数、filter/map/reduce/lambda

    我把写的代码直接贴在下面了,注释的不是很仔细,主要是为了自己复习时方便查找,并不适合没有接触过python的人看,其实我也是初学者. #定义函数 def my_abs(x): if x>=0: ...

  3. Python学习(五)函数 —— 内置函数 lambda filter map reduce

    Python 内置函数 lambda.filter.map.reduce Python 内置了一些比较特殊且实用的函数,使用这些能使你的代码简洁而易读. 下面对 Python 的 lambda.fil ...

  4. Python之匿名函数(filter,map,reduce)

    参考博客:Python匿名函数详解--http://blog.csdn.net/csdnstudent/article/details/40112803 Python内建函数之——filter,map ...

  5. python之有用的3个内置函数(filter/map/reduce)

    这三个内置函数还是非常有用的,在工作中用的还不少,顺手,下面一一进行介绍 1.filter 语法:filter(function,iterable) 解释:把迭代器通过function函数进行过滤出想 ...

  6. Python 函数lambda(), filter(), map(), reduce()

    1 filter filter(function, sequence):对sequence中的item依次执行function(item),将执行结果为True的item组成一个List/String ...

  7. Swift函数编程之Map、Filter、Reduce

    在Swift语言中使用Map.Filter.Reduce对Array.Dictionary等集合类型(collection type)进行操作可能对一部分人来说还不是那么的习惯.对于没有接触过函数式编 ...

  8. 高阶函数 filter map reduce

    const app=new Vue({ el:'#app', data:{ books:[{ id:1, name:"算法导论", data: '2006-1', price:39 ...

  9. Python经常使用内置函数介绍【filter,map,reduce,apply,zip】

    Python是一门非常简洁,非常优雅的语言,其非常多内置函数结合起来使用,能够使用非常少的代码来实现非常多复杂的功能,假设相同的功能要让C/C++/Java来实现的话,可能会头大,事实上Python是 ...

随机推荐

  1. mac webstrom 安装less

    1.检验电脑是否安装less lessc -v 2.如果没有执行全局安装命令 npm install -g less 3.webstrom -> Preferencs-> File Wat ...

  2. iTOP-4412开发板全新升级支持4G全网通模块

    开发板支持4G,GPS,CAN,485,WIFI蓝牙,重力加速度计,陀螺仪等模块. 核心板参数 尺寸:6cm*7cm 高度:连同连接器在内0.26cm CPU:Exynos4412,四核Cortex- ...

  3. centos7 安装后,意外出现Please make your choice from above ['q' to quit | 'c' to continue | 'r' to refresh]

    安装完成centos7后出现如下提示: Initial setup of CentOS Linux 7 (core) 1) [x] Creat user 2) [!] License informat ...

  4. 单页vue路由router

    Vue.js + vue-router 可以很简单的实现单页应用. <router-link> 是一个组件,该组件用于设置一个导航链接,切换不同 HTML 内容. to 属性为目标地址, ...

  5. Java中a=a++ 和 a=++a(转)

    转自https://blog.csdn.net/lovepluto/article/details/81062176 如果问 a++ 和 ++a 的区别,估计很多都能回答上来.a++ 是先取 a 的值 ...

  6. 梦回----32位CPU和64位CPU的通用寄存器

    1 32位Intel的CPU通用寄存器 32位CPU所含有的寄存器有:4个数据寄存器(EAX.EBX.ECX和EDX):2个变址和指针寄存器(ESI和EDI):2个指针寄存器(ESP和EBP):6个段 ...

  7. CSU 2018年12月月赛 G(2219): Coin

    Description 有这样一个众所周知的问题: 你面前有7个硬币,其中有一个劣质的(它比正常的硬币轻一点点),你有一个天平,问需要你需要使用天平多少次能保证找到那个劣质的硬币. 众所周知的算法是: ...

  8. 基于Docker Compose搭建mysql主从复制(1主2从)

    系统环境 * 3 Ubuntu 16.04 mysql 8.0.12 docker 18.06.1-ce docker-compose 1.23.0-rc3 *3 ==> PS  ###我用的是 ...

  9. 利用类装饰器自定制property实现延迟计算

    class LazyProperty: ''' hello,我是非数据描述符(没有定义__set__,不然是大哥数据描述符了--!) ''' def __init__(self, func): pri ...

  10. Python之面向对象新式类和经典类

    Python之面向对象新式类和经典类 新式类和经典类的继承原理: 在Python3中,就只有新式类一种了. 先看Python3中新式类: 类是有继承顺序的: Python的类是可以继承多个类的,也就是 ...