Created
January 17, 2020 19:52
-
-
Save y-yu/a8fe3b299232be0b90a9a5e13b664c2e to your computer and use it in GitHub Desktop.
This file contains 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
struct Cont<A, B> { | |
var run: ((A) -> B) -> B | |
var flatMap: (A) -> Cont<A, B> { | |
get { | |
fatalError() | |
} | |
_modify { | |
let that = self | |
var f: (A) -> Cont<A, B> = { (a: A) -> Cont<A, B> in that } | |
yield &f | |
run = { (k: (A) -> B) -> B in that.run({ (a: A) -> B in f(a).run(k) }) } | |
} | |
} | |
} | |
var a = Cont<String, String>( | |
run: { (k) in k( "a↓ ") + "f!" } | |
) | |
a.flatMap = { (x) in | |
Cont( | |
run: { (k) in k(x + "b↓ ") + "e↑ " } | |
) | |
} | |
a.flatMap = { (x) in | |
Cont( | |
run: { (k) in k(x + "c→ ") + "d↑ " } | |
) | |
} | |
func identity<A>(_ a: A) -> A { | |
return a | |
} | |
print(a.run(identity)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment