Created
November 27, 2017 22:42
-
-
Save bduisenov/f606420c8b6bdb7b5ec2d8693beeb398 to your computer and use it in GitHub Desktop.
Reader monad Swift 4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Reader<E, A> { | |
let g: (E) -> A | |
init(g: @escaping (E) -> A) { | |
self.g = g | |
} | |
func apply(e: E) -> A { | |
return g(e) | |
} | |
func map<B>(f: @escaping (A) -> B) -> Reader<E, B> { | |
return Reader<E, B>{ e in f(self.g(e)) } | |
} | |
func flatMap<B>(f: @escaping (A) -> Reader<E, B>) -> Reader<E, B> { | |
return Reader<E, B>{ e in f(self.g(e)).g(e) } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment