函数式编程
函数合成
func add1(_ v1: Int, _ v2: Int) -> Int { v1 + v2 }
func currying<A, B, C>(_ fn: @escaping (A, B) -> C) -> (B) -> (A) -> C {
return { b in
return { a in
return fn(a, b)}
}
}
print(currying(add1)(20)(10))
func add(_ v1: Int, _ v2: Int) -> Int { v1 + v2 }
func sub(_ v1: Int, _ v2: Int) -> Int { v1 - v2 }
func multiple(_ v1: Int, _ v2: Int) -> Int { v1 * v2 }
func divide(_ v1: Int, _ v2: Int) -> Int { v1 / v2 }
func mod(_ v1: Int, _ v2: Int) -> Int { v1 % v2 }
print(currying(add)(20)(100),
currying(sub)(20)(100),
currying(multiple)(20)(100),
currying(divide)(20)(100),
currying(mod)(20)(100))
柯里化(Currying)
prefix func ~<A, B, C>(_ fn: @escaping (A, B) -> C) -> (B) -> (A) -> C {
{ b in { a in return fn(a, b) } }
}
infix operator >>> : AdditionPrecedence
func >>><A, B, C>(_ fn1: @escaping (A) -> B, _ fn2: @escaping (B) -> C) -> (A) -> C {
{ fn2(fn1($0)) }
}
var num = 1
var fn = (~add)(3) >>> (~multiple)(5) >>> (~sub)(1) >>> (~mod)(10) >>> (~divide)(2)
print(fn(num))
print((((num+3)*5 - 1) % 10) / 2)
func add2(_ v1: Int, _ v2: Int, _ v3: Int) -> Int { v1 - v2 + v3 }
prefix func ~<A, B, C, D>(_ fn: @escaping (A, B, C) -> D) -> (C) -> (B) -> (A) -> D {
{ c in { b in { a in return fn(a, b, c) } } }
}
//20
print((~add2)(30)(20)(10)
函子(Functor)
- 像Array、 Optional这样支持map允许的类型,称为函子(Functor)
public func map<T>(_ transform: (Element) throws -> T) rethrows -> [T]
public func map<U>(_ transform: (Wrapped) throws -> U) rethrows -> U?
- Functor is the Type Justlike have this method
func map<T>(_ fn: (Inner) -> T) -> Type<T>
适用函子(Applicative Functor)
- 对人员一个函子F,如果能支持一下运算,该函子就是一个适用函子
//func pure<T>(_ value: T) -> F<T>
//func <*><A, B>(_ fn: F<(A) -> B>, _ value: F<A>) -> F<B>
- Array可以成为适用函子
func pure<T>(_ value: T) -> T? { value}
infix operator <*> : AdditionPrecedence
func <*><A, B>(_ fn: ((A) -> B)?, _ value: A?) -> B? {
guard let f = fn, let v = value else { return nil}
return f(v)
}
var value: Int? = 10
var fn: ((Int) -> Int)? = { $0 * 2 }
Optional(20)
print(fn <*> value as Any)
//Optional可以成为适用函子
func pure<T>(_ value: T) -> [T] { [value] }
func <*><A, B>(_ fn: [(A) -> B], _ value: [A]) -> [B] {
var array: [B] = []
if fn.count == value.count {
for i in fn.startIndex..<fn.endIndex {
array.append(fn[i](value[i]))
}
}
return array
}
//[10]
print(pure(10))
var array = [{ $0 * 2}, { $0 + 10 }, { $0 - 5}] <*> [1, 2, 3]
//[2, 12, -2]
print(array)
单子(Monad)
- 对任意一个类型F,如果嫩支持以下运算,那么就可以称为是一个单子(Monad)
func pure<T>(_ value: T) -> F<T>
func flatMap<A, B>(_ value: F<A>, _ fn: (A) -> F<B>) -> F<B>
- 很显然Array、 Optional都是单子
面向协议编程
■ 面向协议编程( Protocol Oriented Programming ,简称POP )
- 是Swift的一种编程范式,Apple于2015年WWDC提出
- 在Swift的标准库中,能见到大量POP的影子
■ 同时, Swift也是一-门面向对象的编程语言( Object Oriented Programming ,简称OOP ) - 在Swift开发中, OOP和POP是相辅相成的,任何一方并不能取代另一方
■ POP能弥补OOP-些设计上的不足
回顾OOP
■ OOP的三大特性:封装、继承、多态
■ 继承的经典使用场合
- 当多个类(比如A、B、C类)具有很多共性时,可以将这些共性抽取到一个父类中(比如D类) , 最后A、B、C类继承D类
OOP的不足
■ 但是有些问题,使用OOP并不能很好解决
- 如何将BVC、AVC的公共方法run抽取出来?
class BVC: UIViewController {
func run() {
print("run")
}
}
class AVC: UIViewController {
func run() {
print("run")
}
}
■ 基于OOP想到的一些解决方案?
1.将run方法放到另一个对象A中,然后BVC、DVC拥有对象A属性
- 多了一些额外的依赖关系
2.将run方法增加到UIViewController分类中 - UIViewController会越来越臃肿,而且会影响它的其他所有子类
3.将run方法抽取到新的父类,采用多继承? ( C++支持多继承 ) - 会增加程序设计复杂度,产生菱形继承等问题,需要开发者额外解决
POP的解决方案
protocol Runnable {
func run()
}
extension Runnable {
func run() {
print("run")
}
}
class AVC: UIViewController, Runnable {}
class BVC: UIViewController, Runnable {}
POP的注意点
■ 优先考虑创建协议,而不是父类(基类)
■ 优先考虑值类型(struct、enum), 而不是引用类型( class )
■ 巧用协议的扩展功能
■ 不要为了面向协议而使用协议
利用协议实现前缀效果
struct HY<Base> {
let base: Base
init(_ base: Base) { self.base = base }
}
protocol HYCompatible {}
extension HYCompatible {
static var hy: HY<Self>.Type {
get { HY<Self>.self }
set {}
}
var hy: HY<Self> {
get { HY(self) }
set {}
}
}
extension String: HYCompatible {}
extension HY where Base == String {
var numberCount: Int {
var count = 0
for c in base where ("0"..."9").contains(c) {
count += 1
}
return count
}
static func test() {
print("\(Self.self) static")
}
}
var string = "123qwe434"
// 6
print(string.hy.numberCount)
//HY<String> static
print(String.hy.test())
class Person {}
extension Person: HYCompatible {}
extension HY where Base: Person {
func run() {
print("\(self) run")
}
static func test() { print("\(Self.self) static func") }
}
class Teacher: Person {}
extension HY where Base == Teacher {
func teach() {
print("teach")
}
}
//HY<Person> static func
Person.hy.test()
//HY<Teacher> static func
Teacher.hy.test()
let p = Person()
//HY<Person>(base: Adam_20220925_SwiftBase.Person) run
p.hy.run()
let t = Teacher()
//HY<Teacher>(base: Adam_20220925_SwiftBase.Teacher) run
t.hy.run()
给NSString加前缀
extension NSString: HYCompatible {}
extension HY where Base: NSString {
var numberCount: Int {
var count = 0
for c in base as String where ("0"..."9").contains(c) {
count += 1
}
return count
}
}
var string1 = "123XXX"
var string2: NSString = "123XXX"
var string3: NSMutableString = "123XXX"
print(string1.hy.numberCount)
print(string2.hy.numberCount)
print(string3.hy.numberCount)
给NSString加前缀优化
extension String: HYCompatible {}
extension NSString: HYCompatible {}
extension HY where Base: ExpressibleByStringLiteral {
var numberCount: Int {
var count = 0
for c in (base as! String) where ("0"..."9").contains(c) {
count += 1
}
return count
}
}
var string1 = "123XXX"
var string2: NSString = "1234XXX"
var string3: NSMutableString = "12345XXX"
print(string1.hy.numberCount)
print(string2.hy.numberCount)
print(string3.hy.numberCount)
利用协议判断类型
protocol ArrayType {}
extension Array: ArrayType {}
extension NSArray: ArrayType {}
func isArrayType(_ type: Any.Type) -> Bool {
type is ArrayType.Type
}
print(isArrayType([Int].self))
print(isArrayType([Double].self))
print(isArrayType([Any].self))
print(isArrayType(NSArray.self))
print(isArrayType(NSMutableArray.self))
print(isArrayType(String.self))
响应式编程
响应式编程( Reactive Programming ,简称RP )
- 也是y一种编程范式,于1997年提出,可以简化异步编程,提供更优雅的数据绑定
- 一般与函数式融合在一起,所以也会叫做:函数响应式编程( Functional Reactive Programming,简称FRP )
比较著名的、成熟的响应式框架
❇️ ReactiveCocoa :
- 简称RAC ,有Objective-C、 Swift版本
- 官网:,http://reactivecocoa.iol
- github: https://github.com/ReactiveCocoa
❇️ ReactiveX
- 简称Rx ,有众多编程语言的版本,比如RxJava、 RxKotlin、 RxJS. RxCpp、 RxPHP、 RxGo、 RxSwift等等
- 官网:http://reactivex.io/
- github: https://github.com/ReactiveX
RxSwift
■ RxSwift ( ReactiveX for Swift ) , ReactiveX的Swift版本
- 源码: https://github.com/ReactiveX/RxSwift
- 中文文档: https://beeth0ven.github.io/RxSwift-Chinese-Documentation/
■ RxSwift的安装
①Podfile
use_ frameworks!
target 'target_ name' do
pod 'RxSwift', '~> 6'
pod 'RxCocoa', '~> 6'
end
②命令行
pod repo update
pod install
③导入模块
import RxSwift
import RxCocoa
■ 模块说明
- RxSwift : Rx标准API的Swift实现,不包括任何iOS相关的内容
- RxCocoa :基于RxSwift ,给iOS UI控件扩展了很多Rx特性
RxSwift核心类
- Observable:负责发送事件(Event)
- Observer: 负责订阅Observable,监听Observable发送的事件
Event 类型
/// **next\* (error | completed)**
@frozen public enum Event<Element> {
/// Next element is produced.
case next(Element)
/// Sequence terminated with an error.
case error(Swift.Error)
/// Sequence completed successfully.
case completed
}
创建、订阅observable1
// let observable = Observable<Int>.create { observer in
// observer.onNext(1)
// observer.onCompleted()
// return Disposables.create()
// }
// 等价于
// let observable = Observable.just(1)
// let observable = Observable.of(1)
let observable = Observable.from([1])
observable.subscribe{ event in
print(event)
}.dispose()
// let observable = Observable<Int>.create { observer in
// observer.onNext(1)
// observer.onNext(2)
// observer.onNext(3)
// observer.onCompleted()
// return Disposables.create()
// }
// 等价于
// let observable = Observable.of(1, 2, 3)
let observable = Observable.from([1, 2, 3])
observable.subscribe{ event in
print(event)
}.dispose()
observable.subscribe {
print("next", $0)
} onError: {
print("onError", $0)
} onCompleted: {
print("onCompleted")
} onDisposed: {
print("onDisposed")
}.dispose()
// 等价于
observable.subscribe (onNext: {
print("next", $0)
}, onError: {
print("onError", $0)
}, onCompleted: {
print("onCompleted")
}, onDisposed: {
print("onDisposed")
}).dispose()
创建、订阅observable2
let observable = Observable<Int>.timer(.seconds(3),
period: .seconds(1),
scheduler: MainScheduler.instance)
observable
.map{ "数值为\($0)"}
.bind(to: label.rx.text)
.disposed(by: bag)
Disposable
■每当Observable被订阅时,都会返回一个Disposable实例,当调用Disposable的dispose ,就相当于取消订阅
■在不需要再接收事件时,建议取消订阅,释放资源。有3种常见方式取消订阅
let observable = Observable.from([1, 2, 3])
//立即取消订阅(一次性订阅)
observable.subscribe { event in
print (event )
}.dispose()
//当bag销毁(deinit) 时,会自动调用Disposab le实例的dispose
observable.subscribe { event in
print(event)
}.disposed(by: bag)
// self销毁时(deinit) 时,会自动调用Disposable实例的dispose
let _ = observable.takeUntil(self.rx.deallocated).subscribe { event in
print(event)
}
Binder Label
let binder = Binder(label) { label, value in
label.text = value
}
Observable.just(1).map { "数值为\($0)" }.subscribe(binder).dispose()
Observable.from([1, 2, 3]).map { "数值为\($0)" }.bind(to: binder).dispose()
Binder UIButton
let observable = Observable<Int>.timer(.seconds(2),
period: .seconds(1),
scheduler: MainScheduler.instance)
let binder = Binder<Bool>(button) { button, value in
button.isHidden = value
}
observable.map { $0 % 2 == 0}.bind(to: binder).disposed(by: bag)