函子, 適用函子和單子的Swift簡單實現

Wadler的一句「Monad不就是自函子範疇上的幺半羣嗎?這有什麼難理解的?」讓多少不懂範疇論的FP萌新瑟瑟發抖,但用代碼實現一下就馬上開竅。爲何說「Monad is just a semicolon」,爲何形容它爲pipe,爲何說他是fp stateless的根基,都是在說一個賊簡單的道理:單子就是「你要改變個人值,你告訴我你要幹嗎,我幫你改,改完我告訴你」的無限循環。要我說,Monad就是像polymorphism同樣起了一個欺生的名字。swift

Value & Context 值與封裝

a -> Caapp

class Context<T> {
    var value: T
    init(_ value: T) {
        self.value = value
    }
}

// 驗證
let c = Context(1)
c.value // 1
複製代碼

Functor 函子

(a -> b) -> Fa -> Fbless

class Functor<T>: Context<T> {
    func map<U>(_ f: (T) -> U) -> Functor<U> {
        return Functor<U>(f(value))
    }
}

// 驗證
let f = Functor(1)
let mapped = f.map { "\($0)" }
mapped.value // "1"
複製代碼

Applicative Functor 適用函子

A(a -> b) -> Aa -> Abui

class Applicative<T>: Functor<T> {
    func apply<U>(_ f: Applicative<(T) -> U>) -> Applicative<U> {
        return Applicative<U>(f.value(value))
    }
}

// 驗證
let a = Applicative(1)
let applied = a.apply(Applicative({ "\($0)" }))
applied.value // "1"
複製代碼

Monad 單子

(a -> Mb) -> Ma -> Mbspa

class Monad<T>: Applicative<T> {
    func flatMap<U>(_ f: (T) -> Monad<U>) -> Monad<U> {
        return f(value)
    }
}

// 驗證
let m = Monad(1)
let flatMapped = m.flatMap { Monad( "\($0)" ) }
flatMapped.value // "1"
複製代碼
相關文章
相關標籤/搜索