Implement Mealy Machine using State Monad in real world IOS application
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
Automaton.xcodeproj
Automaton
AutomatonTests
.gitignore
README.md

README.md

Monadic Mealy Machine

Demo for Souzoh iOS Talk 2017: メルカリ アッテを支えるオートマトン

Requirement

Swift 4 / Xcode 9

Summary

State Monad

struct State<S, A> {
    private let run: (S) -> (S, A)

    init(f: @escaping (S) -> (S, A)) {
        self.run = f
    }

    func run(s: S) -> (S, A) {
        return self.run(s)
    }

    func map<B>(g: @escaping (A) -> B) -> State<S, B> {
        return State<S, B> { s in
            let (s1, val) = self.run(s)
            return (s1, g(val))
        }
    }

    func flatMap<B>(g: @escaping (A) -> State<S, B>) -> State<S, B> {
        return State<S, B> { s in
            let (s1, val) = self.run(s)
            return g(val).run(s1)
        }
    }
}

Monadic Mealy Machine

struct MonadicAutomaton<S, A, B> {
    typealias T = (A) -> State<S, B>
    private var f : T

    init(f: @escaping T) {
        self.f = f
    }

    func transition(from: S, by: A) -> (S, B) {
        return f(by).run(s: from)
    }
}