0%

// 自定义表达式模式
struct Student {
    var score = 0, name = ""
    
    /// pattern: case 后面内容
    /// value: switch 后面内容
    static func ~=(pattern: Int, value: Student) -> Bool {
        return value.score >= pattern
    }
    
    static func ~=(pattern: Range<Int>, value: Student) -> Bool {
        return pattern.contains(value.score)
    }
    
    static func ~=(pattern: ClosedRange<Int>, value: Student) -> Bool {
        return pattern.contains(value.score)
    }
}

var stu = Student(score: 75, name: "Tom")
switch stu {
case 100:
    print(">=100")
case 90:
    print(">=90")
case 80..<90:
    print("[80, 90]")
case 60...79:
    print("[60, 79]")
case 0:
    print(">=0")
default:
    break
}

extension String {
    static func ~=(pattern: (String) -> String, value: String) -> Bool {
        return pattern(value)
    }
}

func hasPrefix(_ prefix: String) -> ((String) -> Bool) {
    return { $0.hasPrefix(prefix) }
}
func hasSuffix(_ suffix: String) -> ((String) -> Bool) {
    return { $0.hasSuffix(suffix) }
}

var fn = hasPrefix("123")
print(fn("123456"))

var str = "123456"
switch str {
case hasPrefix("123"), hasSuffix("456"):
    print("123开头")
    print("456结尾")
default:
    break
}

func isEven(_ i: Int) -> Bool {
    return i % 2 == 0
}

func isOdd(_ i: Int) -> Bool {
    return i % 2 != 0
}

extension Int {
    static func ~=(pattern: (Int) -> Bool, value: Int) -> Bool {
        return pattern(value)
    }
}

var age = 9
switch age {
case isEven: //函数匹配,函数类型
    print(age,"是个偶数")
case isOdd:
    print(age,"是个奇数")
default:
    break
}

–EOF–

  1. open: 允许在定义实体的模块,其他模块中访问,允许其他模块进行继承,重写(open只能用在类,类成员上)
  2. public: 允许在定义实体的模块,其他模块中访问,不允许继承,重写(模块内可以)
  3. internal: 只允许在定义实体的模块中访问,不允许在其他模块中访问(默认)
  4. fileprivate: 在同一文件中访问
  5. private: 在实体内访问

全局内用private修饰类,变量或扩展,就是用fileprivate

class Test {
    private struct Dog {
        var age = 0 //Test内可用,如果显式用private修饰,则Person内不能使用age
        func run() { }
    }
    
    private struct Person {
        var dog = Dog()
        mutating func walk() {
            dog.run()
            dog.age = 1
        }
    }
}

–EOF–

#include <stdio.h>

#define maxsize 10
#define failure -1
#define success 1
typedef int datatype;

typedef struct {
    datatype data[maxsize];
    int length;
} Seqlist;

typedef Seqlist* List;

///求表长度 o(1)
int length(Seqlist L) {
    return L.length;
}

/// 取表元 O(n)
datatype get(List L, int index) {
    if (index < 1 || index > L->length ) {
        return failure;
    }
    return L->data[index - 1];
}

/// 定位 O(n)
int locate(List L, datatype x) {
// for (int i = 0; i < L->length - 1; i++) {
// if (x == L->data[i]) {
// return i + 1;
// }
// }
// return failure;
    int i = 0;
    while (i < L->length && L->data[i] != x) {
        i++;
    }
    if (i < L->length) {
        return i + 1;
    } else {
        return failure;
    }
}

/// 删除 O(n)
int delete(List L, int index) {
    if (index < 1 || index > L->length) {
        return failure;
    }
    for (int j = index; j < L->length; j++) {
        L->data[j - 1] = L->data[j];
    }
    return success;
}

/// 插入 O(n)
int insert(List L, datatype x, int index) {
    
    if (L->length == maxsize) {
        return failure; //表满
    }
    if (index < 1 || index > L->length + 1) {
        return failure; //位置错
    }
    
    for (int j = L->length; j >= index; j--) {
        L->data[j] = L->data[j - 1];
    }
    L->data[index - 1] = x;
    L->length++;
    return success;
}



int main(int argc, const char * argv[]) {
    List L = (List)malloc(sizeof(Seqlist));
    L->length = 0;
    printf("%d\n", length(*L));
    int result = insert(L, 0, 1);
    printf("%d\n", result);
    printf("%d\n", length(*L));
    
    printf("取表元 %d\n", get(L, 0));
    return 0;
}

–EOF–

import Foundation

//dynamicMemberLookup_keyPath
struct Address {
    let street: String
    let zipCode: String
    let city: String
}

@dynamicMemberLookup
struct Person {
    let firstName: String
    let lastName: String
    private let address: Address

    init(firstName: String, lastName: String, address: Address) {
        self.firstName = firstName
        self.lastName = lastName
        self.address = address
    }
    
    subscript<T>(dynamicMember keyPath: KeyPath<Address, T>) -> T {
        self.address[keyPath: keyPath]
    }

}

let address = Address(street: "青岛街", zipCode: "266700", city: "北京市")
let person = Person(firstName: "五", lastName: "王", address: address)
print(person.firstName)

//property_wrappers
//https://www.avanderlee.com/swift/property-wrappers/
extension UserDefaults {
    
    public enum Keys {
        static let hasSeenAppIntroduction = "has_seen_app_introduction"
    }
    
    var hasSeenAppIntroduction: Bool {
        set {
            set(newValue, forKey: Keys.hasSeenAppIntroduction)
        }
        get {
            bool(forKey: Keys.hasSeenAppIntroduction)
        }
    }
}

UserDefaults.standard.hasSeenAppIntroduction = true
print(UserDefaults.standard.hasSeenAppIntroduction)

@propertyWrapper
struct UserDefault<T> {
    let key: String
    let defaultValue: T
    
    init(_ key: String, defaultValue: T) {
        self.key = key
        self.defaultValue = defaultValue
    }
    
    var value: T {
        get {
            UserDefaults.standard.value(forKey: key) as? T ?? defaultValue
        }
        set {
            UserDefaults.standard.set(newValue, forKey: key)
        }
    }
}

struct UserDefaultsConfig {
    @UserDefault("has_seen_app_introduction", defaultValue: false)
    static var hasSeenAppIntroduction: Bool
}

print(UserDefaultsConfig.hasSeenAppIntroduction)

–EOF–

var nums = [1, -2, 2, -13, 6, -10, 3, 12]

/// O(n^3)
func maxSubSequenceSum1() {
    var maxSum = 0
    for i in 0..<nums.count {
        for j in i..<nums.count {
            var thisSum = 0
            for k in i...j {
                thisSum += nums[k]
            }
            if thisSum > maxSum {
                maxSum = thisSum
            }
        }
    }
    print(maxSum)
}

/// O(n^2)
func maxSubSequenceSum2() {
    var maxSum = 0
    for i in 0..<nums.count {
        var thisSum = 0
        for j in i..<nums.count {
            thisSum += nums[j]
            if thisSum > maxSum {
                maxSum = thisSum
            }
        }
    }
    print(maxSum)
}

/// O(n)
func maxSubSequenceSum3() {
    var maxSum = 0, thisSum = 0
    for i in 0..<nums.count {
        thisSum += nums[i]
        if thisSum > 0 {
            maxSum = thisSum
        } else { //如果thisSum小于0,则不可能使后面的部分和增大,抛弃.
            thisSum = 0
        }
    }
    print(maxSum)
}

maxSubSequenceSum1()
maxSubSequenceSum2()
maxSubSequenceSum3()

算法很重要呐 😄😄

–EOF–