3 ms·
The author has a follow-up post http://cxwangyi.wordpress.com/2012/07/29/chinese-whispers-in-racket-and-go/ http://cxwangyi.wordpress.com/2012/07/29/chinese-whi
by irahul 13y ago
The author has a follow-up post http://cxwangyi.wordpress.com/2012/07/29/chinese-whispers-in-racket-and-go/ http://cxwangyi.wordpress.com/2012/07/29/chinese-whispers-in...
Each thread reads from the channel to its left and writes the processed input into the channel to its right.
Except that the programs he posted are flowing the other way(start with rightmost, read final value from leftmost). The implementation of the author's wording needs a slight change.
Go implementation:
package main
import "fmt"
func setup_channels(left chan int, num int) chan int {
for i := 0; i < num; i++ {
right := make(chan int)
go func(left, right chan int) { right <- 1 + <-left }(left, right)
left = right
}
return left
}
func main() {
leftmost := make(chan int)
rightmost := setup_channels(leftmost, 100000)
leftmost <- 1
fmt.Println(<-rightmost)
}
Racket implementation:
#lang racket
(define (setup-channels left idx num)
(if (>= idx num)
left
(let [(right (make-channel))]
(thread (λ ()
(channel-put right (+ 1 (channel-get left)))))
(setup-channels right (add1 idx) num))))
(let* [(leftmost (make-channel))
(rightmost (setup-channels leftmost 0 100000))]
(channel-put leftmost 1)
(channel-get rightmost))
I also don't understand why is he writing to the leftmost channel in a separate thread. I don't think that's needed.