函数类型与闭包

2016-07-08  本文已影响0人  霏誠拜咬o

函数类型

func add(x:Double, y:Double)->Double{
  return x+y
}

func minus(x:Double, y:Double)->Double{
  return x-y
}

func mulitiply(x:Double, y:Double)->Double{
  return x*y
}

func divide(x:Double,y:Double)->Double{
  return x/y
}

class Matrix{
  var row=0.0
  var column=0.0

  init(row:Double ,colomn:Double){
    self.row=row
    self.column=column
}

  func process(x:Double, y:Double)->Double{
    return x*row+y*column
}
  
  static func invoke(x:Double, y:Double)->Double{
    return x*y+y*y
}
}
//函数类型作为变量
var compute:(Double, Double)->Double

compute=add //全局函数
let result1=compute(100,200)

compute=divide
let result2=compute(300,40)

var matrix=Matrix(row:10.0,column:20.0)
compute=matrix.process //实例方法
//compute.object=matrix
//compute.method=&process
compute(80,60)//JMP  compute.method

compute=Matrix.invoke //静态方法
compute(30,40)

//函数类型作为参数
func process(math: (Double,Double)->Double,x:double,y:Double){
  let result=math(x,y)
  print("result:\(result)")  
}

process(multiply,x:30,y:50)

//函数类型作为返回值
func calculateFunction(symbol:String)->(Double,Double)->Double{
  switch(symbol){
  case "+":
           return add
  case "-":
           return minus
  case "*":
           return multiply
  case "/":
           return divide
  default:
      return add
  }
}

var calculate=calculateFunction("-")
let result3 = calculate(300,80)

//嵌套函数
func algorithmFunction(symbol:String)-> (Double, Double)->Double{
  func add(x:Double, y:Double)->Double{
    return x+y
  }

  func minus(x:Double, y:Double)->Double{
    return x-y
  }

  func mulitiply(x:Double, y:Double)->Double{
    return x*y
  }

  func divide(x:Double,y:Double)->Double{
    return x/y
  }
  switch(symbol){
    case "+":
           return add
    case "-":
           return minus
    case "*":
           return multiply
    case "/":
           return divide
    default:
      return add
  }
}

var algorithm=algorithmFuntion("/")
let result4 = algorithm(600,80)
函数类型的内存模型.png

compute=全局函数时,对象指针为空
认识闭包Closure
=====================

class Rectangle {
  var width=0
  var length=0

  init(width:Int, length:Int){
    self.width=width
    self.length=length
  }
}

func <= (left: Rectangle, right: Rectangle) -> Bool {
  return left.width*left.length<=right.width*right.length
}

func compare(first:Rectangle, second: Rectangle)->Bool{
  return first.width*first.length<=second.width*second.length
}

var rects = [Rectangle(width:6,length:7),
             Rectangle(width:5,length:8),
             Rectangle(width:9,length:6)]

//函数类型对象
/*
func sort(@noescape isOrderedBefore: (Self.Generator.Element,Self.Generator.Element) -> Bool) -> [Self.Generator.Element]
*/
//func sort(isOrderedBefore:(Rectangle,Rectangle)->Bool)->[Rectangle]

var predict:  (Rectangle,Rectangle)->Bool
predict=compare
rects.sort(predict)

rects.sort(compare) //不用上面那样绕


//闭包表达式
rects.sort({(first:Rectangle, second:Rectangle)->Bool in

  return first.width*first.length<=second.width*second.length
})


let expression={(first:Rectangle, second:Rectangle)->Bool in
  return first.width*first.length<=second.width*second.length
}
rects.sort(exprssion)//也可以这么写

//自动类型推断
rects.sort({first,second in
  return first.width*first.length<=second.width*second.length
})

//单表达式省略return
rects.sort({first,second in first.width*first.length<=second.width*second.length})

//参数缩略形式
rects.sort({$0.width*$0.length<=$1.width*$1.length})

//操作符缩略式
rects.sort( <= ) //rects.sort(compare)

//尾随闭包
rects.sort{
  first,second in
  first.width*first.length<=second.width*second.length
}

rects.sort{$0.width*$0.length<=$1.width*$1.length}

//自动闭包
var cities = ["Beijing","shanghai","New York","Paris","London"]
print(cities.count)

let filter = {cities.removeLast()}//()->String
print(cities.count)

print("Deleting\(filter())!")
print(cities.count)

闭包表达式

{(参数1,参数2……)->返回值类型 in
语句块
}

函数类型与闭包的变量捕获

var data=100

//捕获实例属性
class Rectangle{
  var width=0
  var length=0

  static var max=100

  init(width:Int,length:Int){
    self.width=width
    self.length=length
  }

  func getComputeHandler()->() -> Int{
    return {
      return self.width*self.length
    }
  }
}
var rect=Rectangle(width:10,length:20)
var handler=rect.getComputeHandler()
handler()

//捕获参数或局部变量
func addHandler(var step: Int) -> () -> Int {
  var sum = 0
  return {
    sum += step
    data++
    Rectangle.max++
    step++
    return sum
  }
/*  func add() ->Int {
    sum+=step

    return sum  
  }
  return add*/
  
}
let addByTen=addHandler(10)

addByTen()
addByTen()
addByTen()
print(data)
print(Rectangle.max)

let addBYSix=addHandler(6)

addBySix()
addBySix()
addBySix()
print(data)
print(Rectangle.max)

/*class AddHelper {
  var sum=0
  var step=0

  func add() -> Int {
    self.sum+=self.step
    return self.sum
  }
}

func addHandlerHelper(step: Int) -> () -> Int {
  let sum=0
  let obj=AddHelper()
  obj.sum=sum
  obj.step=step
  let closuer=obj.add
  return closuer
}

let addByTen=addHandlerHelper(10)

print(addByTen())
print(addByTen())
print(addByTen())

let addByTen=addHandlerHelper(6)

print(addBySix())
print(addBySix())
print(addBySix())*/
捕获值的内存模型.png
上一篇下一篇

猜你喜欢

热点阅读