示例工程 06-functions-closures

06-functions-closures/Sources/Ch06Demo/App.swift

swift · 1.7 KB

import Ch06Kit
 
@main
struct Ch06Demo {
    static func main() {
        print("[6.1] 默认参数 = \(LabelLab.makeUser(name: "Ada"))")
        print("[6.1] 显式参数 = \(LabelLab.makeUser(name: "Ada", age: 30))")
        print("[6.1] 省略标签 = \(LabelLab.square(5))")
        print("[6.1] 距离平方 = \(LabelLab.distance(from: (0, 0), to: (3, 4)))")
        print("[6.1] 可变参数 = \(LabelLab.total(1, 2, 3, 4))")
 
        var left = 1
        var right = 2
        InoutLab.swapValues(&left, &right)
        print("[6.2] swap 后 = \(left)/\(right)")
        var counter = 0
        InoutLab.increment(&counter, by: 5)
        print("[6.2] increment 后 = \(counter)")
 
        let captured = ClosureLab.captureSnapshotVsReference()
        print("[6.3] 快照/引用 = \(captured.snapshot)/\(captured.reference)")
 
        let count = ClosureLab.makeCounter(step: 2)
        print("[6.3] 计数器 = \(count())/\(count())/\(count())")
 
        print("[6.4] autoclosure 求值 = \(ClosureLab.autoclosureSkipsEvaluation())")
        print("[6.4] eager 求值 = \(ClosureLab.eagerEvaluation())")
 
        let adder = FunctionTypeLab.makeAdder(10)
        print("[6.5] apply = \(FunctionTypeLab.apply(adder, to: [1, 2, 3]))")
        print("[6.5] 排序 = \(FunctionTypeLab.sortedNames(["bob", "al", "cy", "anna"]))")
 
        let owner = Owner(value: 7)
        let handler = ClosureLab.makeHandler(owner: owner)
        print("[6.6] 捕获 owner = \(handler())")
        print("[6.6] 闭包计数 = \([1, 2, 3].map { $0 * $0 })")
        print("[6.7] MemoryLayout<() -> Int> = \(MemoryLayout<() -> Int>.size) 字节")
        print("[6.7] MemoryLayout<(Int) -> Int> = \(MemoryLayout<(Int) -> Int>.size) 字节")
    }
}