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