Slide 110
Slide 110 text
// Send-Receive ίϧʔνϯϞφυʹΑΔ࣮
let sender = Send.do {
accountA -= money
send(money)
}
let receiver = Receive.do {
money <- receive()
accountB += money
}
runCoroutines(sender, receiver)
// ༨ஊɿrunCoroutine ͷத (NOTE: fatalError ΛΘͳ͍Ͱ͖Δ)
func runCoroutines(sender: Send, receiver: Receive)
-> M<(A, B)> where Monad[M] {
sender.resume.flatMap { sent in
receiver.resume.flatMap { received in
switch (sent, received) {
case let (.done(a), .done(b)): return .unit((a, b))
case let (.done(a), .suspended(fco)): fatalError("sender ended too soon")
case let (.suspended(a, co), .done(b)): return runCoroutines(co, .unit(b))
case let (.suspended(a, co), .suspended(f)): return runCoroutines(co, f(a))
}
}
}
}