Closures闭包
//: Playground - noun: a place where people can play
import UIKit
/*
闭包
Swift ====> Closures
Ruby OC ====> Block
Python C++11 ====> Lamba
Javescript =====> Anonymous Function(匿名函数)
*/
//冒泡排序
func sortInts(inout data : [Int], function : (Int,Int)->Bool) {
for var i = 0; i < data.count - 1; i++ {
for var j = 0; j < data.count-1-i; j++ {
if function(data[j], data[j+1]) {
swap(&data[j], &data[j+1])
}
}
}
}
func rule(a : Int, b : Int) ->Bool {
return a > b
}
func rule1(a : Int, b : Int) ->Bool {
return a < b
}
var a = [1, 3, 9, 0, 8, 2, 5, 7, 6, 4]
sortInts(&a, rule)
a
sortInts(&a, rule1)
a
//闭包函数 实际上就是函数 是匿名函数
//{
// (parameters)->returnType in
// //语句
// retrun xxx
//}
sortInts(&a, {
// (Int,Int)->Bool
(a : Int, b : Int)->Bool in
return a > b
})
a
func rule2(a : Int, b : Int)->Bool{
return a < b
}
sortInts(&a, {
(a : Int, b : Int)->Bool in
return a < b
})
a
//闭包函数有些事可以省略
//可以把参数类型省略,类型从传入的参数中推断出来
sortInts(&a, {
(a, b)->Bool in
return a < b
})
//可以把返回值省略,采用了追踪返回值类型
sortInts(&a, {
(a, b) in
return a < b
})
//如果只有一条语句, 可以省略return
sortInts(&a, {
(a, b) in a < b
})
//参数名可以不用写,使用默认的$0, $1, $2...来代替
sortInts(&a, { $0 < $1 })
//甚至可以直接省略参数,而直接使用运算符来表示函数的内容
sortInts(&a, >)
a
var i : Int = 10
func setNum(inout a : Int,function : (inout Int) ->()) {
function(&a)
}
setNum(&i, {
//(+5 , -10, 100)
(inout a : Int) in a+=20
})
func add_5(inout n : Int) {
n += 5
}
func add_10(inout n : Int) {
n += 10
}
func addArr(inout array : [Int], function : (inout Int)->() ) {
for var i = 0; i < array.count; i++ {
function(&array[i])
}
}
var array = [1, 2, 3, 4, 5]
addArr(&array, add_5)
array
addArr(&array, add_10)
array
//对字符串进行排序,用的是系统自带的排序函数
var names = ["aaa","cdc","add","bbb"]
sort(&names)
names
//Swift 的排序函数 > 从大到小
sort(&names, { (a, b) -> Bool in
a > b
})
sort(&names, <)
//sorted
//map()
let digitNames = [0:"零",1:"一",2:"二",3:"三",4:"四",5:"五",6:"六",7:"七",8:"八",9:"九"]
let numbers = [16, 58, 510]
//map是数组的方法,可以迭代数组中每一个元素传入闭包函数中执行一次,并执行结果生成一个新的数组返回出来
let strings = numbers.map( {
(var number) ->String in
var output = ""
while number > 0{
output = digitNames[number % 10]! + output
number /= 10
}
return output
}
)
let _is = numbers.map( {
(var number) ->Int in
var a = number + 50
return a
}
)
strings
var aa = 10
numbers
_is
Closures闭包的更多相关文章
- Swift 中的Closures(闭包)详解
Swift 中的Closures(闭包)详解 在Swift没有发布之前,所有人使用OC语言编写Cocoa上的程序,而其中经常被人们讨论的其中之一 -- Block 一直备受大家的喜爱.在Swift中, ...
- JavaScript Closures 闭包
在一些编程语言中, 当我们执行完成function中的局部代码仅在函数执行期间可运行. 但是JS 事不一样的 闭包总结来说, 就是innerFunction 总是有使用outer function 的 ...
- Swift 06.Closures
Closures --闭包 看了好些文章.由于自己也是刚开始学习swift,闭包还是不是很明白.暂时先放放.等看完后面的.加深感触后,在回头总结闭包的概念. 数组中常用的闭包函数 在Swift的数组中 ...
- 闭包函数&回调函数
闭包函数&回调函数 谈到回调函数,不得不提匿名函数;匿名函数,也叫闭包函数,也就是没有名字的函数,它可以单独存在,也可以将其赋值给某一个变量.so,先来看一下闭包函数. 闭包函数 php文档: ...
- PHP - 闭包Closure和lambda function
现在的语言没有闭包简直都不好意思说出来. 想要了解闭包是什么,那么就必须知道匿名函数.其实看起来他们其实差不多一个意思. 见php RFC一句话: End of 2007 a patch was ...
- js closures all in one
js closures all in one setTimeout 闭包,log(i, arr[¡]) var, let, closures, IIFE "use strict"; ...
- Javascript 函数声明、调用、闭包
1 # Javascript 函数声明.调用.闭包 2 # 一.函数声明 3 # 1.直接声明.浏览器在执行前,会先将变量和函数声明进行提升. 4 fn(); 5 function fn () { 6 ...
- Objective-C文章中的生词
Objective-C http://rypress.com/tutorials/objective-c/index C Basics http://rypress.com/tutorials/ ...
- Lua 架构 The Lua Architecture
转载自:http://magicpanda.net/2010/10/lua%E6%9E%B6%E6%9E%84%E6%96%87%E6%A1%A3/ Lua架构文档(翻译) 十 102010 前段时间 ...
随机推荐
- POJ 2586 Y2K Accounting Bug(枚举大水题)
Y2K Accounting Bug Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10674 Accepted: 53 ...
- Do not use the <section> element as a generic container; this is what <div> is for, especially when the sectioning is only for styling purposes.
Do not use the <section> element as a generic container; this is what <div> is for, espe ...
- (最新)各大公司Java后端开发面试题总结
ThreadLocal(线程变量副本) Synchronized实现内存共享,ThreadLocal为每个线程维护一个本地变量. 采用空间换时间,它用于线程间的数据隔离,为每一个使用该变量的线程提供一 ...
- C语言文件读写Demo
CIODemo.c #include <stdio.h> #include <time.h> #define INPUT_BUFFER_SIZE 100 * 1024 int ...
- django rest_framework swagger使用案例
环境准备 环境要求: python3 django2 pip3 模块安装: pip3 install django-rest-framework pip3 install django-rest-sw ...
- poj 3368 Frequent values 解题报告
题目链接:http://poj.org/problem?id=3368 题目意思:给出一段 n 个数的序列你,对于区间 [l, r] 的询问,找出 出现频率最高的数的次数.考虑到序列中的数是非递减的, ...
- codevs矩阵乘法系列
T1:矩阵乘法板子题,练手. #include <map> #include <set> #include <cmath> #include <ctime&g ...
- codeforces 414A A. Mashmokh and Numbers(素数筛)
题目链接: A. Mashmokh and Numbers time limit per test 1 second memory limit per test 256 megabytes input ...
- I.MX6 NXP git 仓库
/************************************************************************* * I.MX6 NXP git 仓库 * 说明: ...
- JavaScript-Tool:wdtree
ylbtech-JavaScript-Tool:wdtree 1.返回顶部 1. 插件描述:wdTree是一个轻量级jQuery插件用于创建一个带有嵌套Check Boxe的树形控件. wdTree是 ...