Closures Closure Package

Source: Internet
Author: User

: Playground-noun:a place where people can play

Import UIKit

/*

Closed Package

Swift ====> Closures

Ruby OC ====> Block

Python c++11 ====> Lamba

Javescript =====> Anonymous function ( anonymous functions )

*/

Bubble Sort

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

a closure function is actually a function that is an anonymous function

//{

(parameters)->returntype in

Statement

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

closure function Some things can be omitted

The parameter type can be omitted, and the type is inferred from the parameters passed in.

Sortints(&a, {

(A, B),Bool in

return a < b

})

you can omit the return value by using the trace return value type

Sortints(&a, {

(A, B) inch

return a < b

})

If there is only one statement, you can omit the return

Sortints(&a, {

(A, B) in a < b

})

parameter names can be used without writing, using the default $ , $, $ ... To replace

Sortints(&a, {$ < $})

you can even omit parameters directly, and directly use operators to represent the contents of a function

Sortints(&a, >)

A

var i: Int = ten

Func setnum (inout A: int, function: (inout Int), ()) {

function (&a)

}

Setnum(&i, {

//(+5, -10, +)

(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

sorting strings, using the system's own sort function

var names = ["AAA","CDC","Add","BBB"]

Sort(&names)

Names

Swift 's sort function > from big to small

Sort(&names, {(A, B), Bool in

A > B

})

Sort(&names, <)

Sorted

Map ()

LetDigitnames = [0:" 0 ",1:" one ,2: " two " 3: " three " ,4: " IV ",5:" five " " six " ,7: " seven ",8:" eight ,9: " nine "

Let numbers = [+, 510]

Map is an array of methods that can iterate every element in an algebraic group into a closure function once, and execute the result to generate a new array to return

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 +

return a

}

)

Strings

var aa = Ten

Numbers

_is

Closures Closure Package

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.