Last active
March 9, 2020 15:40
-
-
Save donly/64b9a7e31a83c13403135ed50bbf4cd8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
let names = ["Dong", "Chris", "Alex", "Ewa", "Barry", "Baniella"] | |
// 1.Origin | |
func forward(_ s1: String, _ s2: String) -> Bool { return s1 < s2 } | |
var orderNames = names.sorted(by: forward) | |
// 2.Closure writen as entired format | |
var reverseNames = names.sorted (by: { (s1: String, s2: String) -> Bool in | |
return s1 > s2 | |
}) | |
// reverseNames is ["Ewa", "Dong", "Chris", "Barry", "Baniella", "Alex"] | |
// 3.Inferring Type From Context | |
reverseNames = names.sorted(by: { s1, s2 in return s1 > s2 }) | |
//print(reverseNames) | |
// 4.Implicit Returns from Single-Expression Closures | |
reverseNames = names.sorted(by: { s1, s2 in s1 > s2 }) | |
//print(reverseNames) | |
// 5.Shorthand Argument Names | |
reverseNames = names.sorted(by: { $0 > $1 }) | |
//print(reverseNames) | |
// 6.Operator Methods | |
reverseNames = names.sorted(by: >) | |
print(reverseNames) | |
// 7.Trailing Closures | |
func someFunctionThatTakesAClosure(closure: () -> Void) { | |
// function body goes here | |
print("Here is the function body output") | |
closure() | |
} | |
someFunctionThatTakesAClosure(closure: { | |
print("I am a closure") | |
}) | |
someFunctionThatTakesAClosure { | |
print("I am another closure") | |
} | |
reverseNames = names.sorted() { $0 > $1 } | |
// 8. without parentheses | |
reverseNames = names.sorted { $0 > $1 } | |
// 9. Shortest | |
orderNames = names.sorted() | |
// ["Alex", "Baniella", "Barry", "Chris", "Dong", "Ewa"] | |
let digitNames = [ | |
0: "Zero", 1: "One", 2: "Two", 3: "Three", 4: "Four", | |
5: "Five", 6: "Six", 7: "Seven", 8: "Eight", 9: "Nine" | |
] | |
let numbers = [16, 58, 510] | |
let strings = numbers.map { number -> String in | |
var number = number | |
var output = "" | |
repeat { | |
output = digitNames[number % 10]! + output | |
number /= 10 | |
} while number > 0 | |
return output | |
} | |
// strings is inferred to be of type [String] | |
// its value is ["OneSix", "FiveEight", "FiveOneZero"] | |
// Capturing Values | |
func makeIncrementer(forIncrement amount: Int) -> () -> Int { | |
var runingTotoal = 0 | |
func incrementer() -> Int { | |
runingTotoal += amount | |
return runingTotoal | |
} | |
return incrementer | |
} | |
let incrementByTen = makeIncrementer(forIncrement: 10) | |
incrementByTen() // returns a value of 10 | |
incrementByTen() // returns a value of 20 | |
incrementByTen() // returns a value of 30 | |
let incrementBySeven = makeIncrementer(forIncrement: 7) | |
incrementBySeven() // returns a value of 7 | |
incrementBySeven() // returns a value of 14 | |
incrementBySeven() // returns a value of 21 | |
incrementByTen() // returns a value of 40 | |
// Closures are reference types | |
let alsoIncrementByTen = incrementByTen | |
alsoIncrementByTen() | |
// Escaping Closures | |
// the closure needs to escape, to be called later. | |
var completionHandlers: [() -> Void] = [] | |
func functionWithEscapingClosure(completionHandler: @escaping () -> Void) { | |
// completionHandler() | |
completionHandlers.append(completionHandler) | |
} | |
func functionWithNonescapingClosure(closure: () -> Void) { | |
closure() | |
} | |
class SomeClass { | |
var x = 10 | |
func doSomething() { | |
functionWithNonescapingClosure { x = 200 } | |
functionWithEscapingClosure { self.x = 100 } | |
} | |
} | |
let instance = SomeClass() | |
instance.doSomething() | |
print(instance.x) | |
completionHandlers.first?() | |
print(instance.x) | |
var customersInLine = names | |
print(customersInLine.count) // 6 | |
// () -> String is a function with no parameters that returns a string. | |
let customerProvider = { customersInLine.remove(at: 0) } | |
print(customersInLine.count) // 6 | |
print("Now serving \(customerProvider())!") | |
// Prints "Now serving Dong!" | |
print(customersInLine.count) // 5 | |
func serve(customer customerProvider: () -> String) { | |
print("Now serving \(customerProvider())!") | |
} | |
serve(customer: customerProvider) | |
// Prints "Now serving Chris!" | |
serve { () -> String in | |
customersInLine.remove(at: 0) | |
} | |
// Prints "Now serving Alex!" | |
// customersInLine is ["Ewa", "Barry", "Daniella"] | |
func serve1(customer customerProvider: @autoclosure () -> String) { | |
print("Now serving \(customerProvider())!") | |
} | |
serve1(customer: customersInLine.remove(at: 0)) | |
// Prints "Now serving Ewa!" | |
// @escaping and @autoclosure | |
// customersInLine is ["Barry", "Daniella"] | |
var customerProviders: [() -> String] = [] | |
func collectCustomerProviders(_ customerProvider: @autoclosure @escaping () -> String) { | |
customerProviders.append(customerProvider) | |
} | |
collectCustomerProviders(customersInLine.remove(at: 0)) | |
collectCustomerProviders(customersInLine.remove(at: 0)) | |
print("Collected \(customerProviders.count) closures.") | |
// Prints "Collected 2 closures." | |
for cusomerProvider in customerProviders { | |
print("Now Serving \(customerProvider())!") | |
} | |
// Prints: | |
// Now Serving Barry! | |
// Now Serving Baniella! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment