/1workers.go Secret
Last active
July 13, 2019 23:29
Star
You must be signed in to star a gist
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
| func main() { | |
| jobs := make(chan int, 100) | |
| results := make(chan int, 100) | |
| go worker(jobs, results) | |
| for ^100 { | |
| jobs <- i | |
| } | |
| close(jobs) | |
| for ^100 { | |
| print <- results | |
| } | |
| func worker(jobs <-chan int, results chan<- int) { | |
| for n := range jobs { | |
| results <- fib(n) | |
| } | |
| } | |
| func fib(n int) int { | |
| if n <= 1 { | |
| return n | |
| } | |
| return fib(n - 1) | |
| } |
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
| sub MAIN() { | |
| my $jobs = Channel.new; | |
| my $results = Channel.new; | |
| for ^4 { | |
| start worker($jobs, $results); | |
| } | |
| $jobs.send($_) for ^100; | |
| $jobs.close; | |
| react whenever $results.Supply -> $result { | |
| say $result; | |
| } | |
| } | |
| sub worker($jobs, $results) { | |
| react whenever $jobs.Supply -> $job { | |
| $results.send: fib($job); | |
| LAST { $results.close } | |
| } | |
| } | |
| sub fib($n) { | |
| return 1 if $n <= 1; | |
| return fib($n - 1) + fib($n - 2); | |
| } |
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
| sub fib($n) { | |
| return 1 if $n <= 1; | |
| return fib($n - 1) + fib($n - 2); | |
| } | |
| my @jobs = 0..100; | |
| my $results = @jobs.race(batch => 1, degree => 4).map({ fib($_) }); | |
| react whenever $results.Supply -> $result { | |
| $result.say; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment