]> Cypherpunks.ru repositories - gostls13.git/blob - test/sieve.go
test: use testlib in a few more cases.
[gostls13.git] / test / sieve.go
1 // build
2
3 // don't run it - goes forever
4
5 // Copyright 2009 The Go Authors. All rights reserved.
6 // Use of this source code is governed by a BSD-style
7 // license that can be found in the LICENSE file.
8
9 package main
10
11 // Send the sequence 2, 3, 4, ... to channel 'ch'.
12 func Generate(ch chan<- int) {
13         for i := 2; ; i++ {
14                 ch <- i // Send 'i' to channel 'ch'.
15         }
16 }
17
18 // Copy the values from channel 'in' to channel 'out',
19 // removing those divisible by 'prime'.
20 func Filter(in <-chan int, out chan<- int, prime int) {
21         for {
22                 i := <-in // Receive value of new variable 'i' from 'in'.
23                 if i%prime != 0 {
24                         out <- i // Send 'i' to channel 'out'.
25                 }
26         }
27 }
28
29 // The prime sieve: Daisy-chain Filter processes together.
30 func Sieve() {
31         ch := make(chan int) // Create a new channel.
32         go Generate(ch)      // Start Generate() as a subprocess.
33         for {
34                 prime := <-ch
35                 print(prime, "\n")
36                 ch1 := make(chan int)
37                 go Filter(ch, ch1, prime)
38                 ch = ch1
39         }
40 }
41
42 func main() {
43         Sieve()
44 }