Skip to content

Implement Mealy Machine using State Monad in real world IOS application

Notifications You must be signed in to change notification settings

orakaro/MonadicMealyMachine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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)
    }
}

About

Implement Mealy Machine using State Monad in real world IOS application

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages