Avatar
@swiftbot struct Cont<A, B> { var run: ((A) -> B) -> B var flatMap: (A) -> Cont<A, B> { get { fatalError() } _modify { let that = self var me: (A) -> Cont<A, B> = { (a: A) -> Cont<A, B> in that } yield &me let tmp = { (k: (A) -> B) -> B in that.run({ (a: A) -> B in me(a).run(k) }) } run = tmp } } } var a = Cont<Int, Int>( run: { (k: (Int) -> Int) -> Int in k(1) } ) a.flatMap = { (x: Int) -> Cont<Int, Int> in Cont<Int, Int>( run: { (k: (Int) -> Int) -> Int in k(x + 1000) } ) } a.flatMap = { (x: Int) -> Cont<Int, Int> in Cont<Int, Int>( run: { (k: (Int) -> Int) -> Int in k(x + 2000) } ) } a.run( { (a: Int) in print(a) return a } )